Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.
Modify a PowerShell script to accept parameters.
January 6, 2017
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:UsersAdministratorDocuments> '[email protected]' | .FindADUserSID.ps1S-1-5-21-218700803-3075295566-3609972863-1109PS C:UsersAdministratorDocuments> .FindADUserSID.ps1 honeypot@savilltech.netS-1-5-21-218700803-3075295566-3609972863-3228PS C:UsersAdministratorDocuments> .FindADUserSID.ps1cmdlet FindADUserSID.ps1 at command pipeline position 1Supply values for the following parameters:user: [email protected]
You May Also Like