Modify a script to accept parameters

Modify a script to accept parameters

Q. How can I modify a script to accept a parameter?

A. Enabling a script to accept a parameter requires only a single line to added to script:

Param([Parameter(ValuefromPipeline=$true,Mandatory=$true)][string]$user)

In this example the value passed to the script is stored in variable $user and must be passed and can also be passed along the pipeline. Below is an example use:

Param([Parameter(ValuefromPipeline=$true,Mandatory=$true)][string]$user)
$AdObj = New-Object System.Security.Principal.NTAccount($user)
$strSID = $AdObj.Translate([System.Security.Principal.SecurityIdentifier])
$strSID.Value

Once saved as FindADUserSID.ps1 it can be used in a number of ways:

PS C:\Users\Administrator\Documents> '[email protected]' | .\FindADUserSID.ps1
S-1-5-21-218700803-3075295566-3609972863-1109

PS C:\Users\Administrator\Documents> .\FindADUserSID.ps1 [email protected]
S-1-5-21-218700803-3075295566-3609972863-3228

PS C:\Users\Administrator\Documents> .\FindADUserSID.ps1
cmdlet FindADUserSID.ps1 at command pipeline position 1
Supply values for the following parameters:
user: [email protected]
S-1-5-21-218700803-3075295566-3609972863-1109

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish