![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Passing variables to script Hi, Can someone please tell me how I pass variables to a powershell script... thanks, |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Passing variables to script ScriptPath.ps1 arg1 arg2 "arg 3" if the script defines named parameters you can use: ScriptPath.ps1 -testParam1 arg1 -testParam2 arg2 -testParam3 "arg 3" Shay http://scriptolog.blogspot.com > Hi, > > Can someone please tell me how I pass variables to a powershell > script... > > thanks, > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Passing variables to script Declaring parameter in script use param(...) in ur script. The basic syntax in param is the same as in function. best wishes! Edengundam "Anatoli" <Anatoli@discussions.microsoft.com> wrote news:3FA8E45D-B629-47CB-95A5-F9CF8A99F0D7@microsoft.com... > Hi, > > Can someone please tell me how I pass variables to a powershell script... > > thanks, |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Passing variables to script Just to elaborate, there are three methods to provide input to a script. -positional parameters (using special $args[] variable) -named parameters (using param(...) statement) -read-host cmdlet Examples: -positional myscript.ps1 'bar' $foo = $args[0] # $foo evaluates to the string 'bar'. #use $foo... If you have more than one parameter, the user must enter them in the correct order hence positional. Your code usually depends on the correct order of the values in the $args[] array. -named myscript.ps1 -foo bar -bam baz param($foo,$bam) #This line must be the first non-comment line in the script. # $foo evaluates to bar; $bam evaluates to baz #use $foo and $bam... -read-host cmdlet #This method requires user input. myscript.ps1 $foo = read-host -prompt "Please enter foo value: " # $foo evaluates to whatever the user enters in response to the prompt. #use $foo... On Aug 7, 3:01 am, "Edengundam" <feng_e...@163.com> wrote: > Declaring parameter in script use param(...) in ur script. The basic syntax > in param is the same as in function. > > best wishes! > Edengundam > > "Anatoli" <Anat...@discussions.microsoft.com> wrotenews:3FA8E45D-B629-47CB-95A5-F9CF8A99F0D7@microsoft.com... > > > > > Hi, > > > Can someone please tell me how I pass variables to a powershell script... > > > thanks,- Hide quoted text - > > - Show quoted text - |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Passing variables to script Ok, just to be clear. I want to pass $strFirstName, $strMiddleInitial, $strLastName and $strGroup to the script below and want to used named parameters. myscript.ps1 -FistName Joe -MI A. -LastName Schmoe -Group "CN=TestGroup,CN=Users,DC=yefimov,DC=org" param($strFirstName, $strMiddleInitial, $strLastName, $strGroup) ##$strFirstName = "Joseph" ##$strMiddleInitial = "A." ##$strLastName = "Schmoe" $strUserName = $strFirstName.Substring(0,1) + $strLastName $strDisplayName = $strFirstName + " " + $strMiddleInitial + " " + $strLastName $strEmail = $strFirstName + "." + $strLastName + "@yefimov.org" $strUpn = $strUserName + "@yefimov.org" $strSamid = $strUserName ##$strGroup = "CN=TestGroup,CN=Users,DC=yefimov,DC=org" $strCNName = "CN=Users,DC=yefimov,DC=org" $strValue = "CN="+ $strDisplayName + "," + $strCNName $strSamAccount = get-adobject -value $strUserName if ($strSamAccount.samaccountname -eq $strUserName) { Write-Host "The username" $strUserName "already exists! Please change the username by adding another letter from the user's first name" `n`n -foregroundcolor red } else { write-host -noNewLine "SamAccountName available." -foregroundcolor green Write-Host -noNewLine " Creating Windows Account please wait." -foregroundcolor blue for ($i=0;$i -le 50; $i++) { if ($i -lt 50) { Write-Host -noNewLine "." -foregroundcolor blue Start-Sleep -m 50 } else {Write-Host "." -foregroundcolor blue} } dsadd user $strValue -upn $strUpn -samid $strSamid -fn $strFirstName -mi $strMiddleInitial -ln $strLastName -display $strDisplayName -memberof $strGroup -mustchpwd yes } "rogwei@comcast.net" wrote: > Just to elaborate, there are three methods to provide input to a > script. > > -positional parameters (using special $args[] variable) > -named parameters (using param(...) statement) > -read-host cmdlet > > Examples: > -positional > myscript.ps1 'bar' > > $foo = $args[0] # $foo evaluates to the string 'bar'. > #use $foo... > > If you have more than one parameter, the user must enter them in the > correct order hence positional. Your code usually depends on the > correct order of the values in the $args[] array. > > -named > myscript.ps1 -foo bar -bam baz > > param($foo,$bam) #This line must be the first non-comment line in the > script. > # $foo evaluates to bar; $bam evaluates to baz > #use $foo and $bam... > > -read-host cmdlet #This method requires user input. > myscript.ps1 > > $foo = read-host -prompt "Please enter foo value: " > # $foo evaluates to whatever the user enters in response to the > prompt. > #use $foo... > > On Aug 7, 3:01 am, "Edengundam" <feng_e...@163.com> wrote: > > Declaring parameter in script use param(...) in ur script. The basic syntax > > in param is the same as in function. > > > > best wishes! > > Edengundam > > > > "Anatoli" <Anat...@discussions.microsoft.com> wrotenews:3FA8E45D-B629-47CB-95A5-F9CF8A99F0D7@microsoft.com... > > > > > > > > > Hi, > > > > > Can someone please tell me how I pass variables to a powershell script... > > > > > thanks,- Hide quoted text - > > > > - Show quoted text - > > > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Passing variables to script the parameter names are the arguments you send (without the $ sign). myscript.ps1 -strFirstName Joe -strMiddleInitial "A." -strLastName Schmoe -strGroup "CN=TestGroup,CN=Users,DC=yefimov,DC=org" Shay http://scriptolog.blogspot.com > Ok, just to be clear. I want to pass $strFirstName, $strMiddleInitial, > $strLastName and $strGroup to the script below and want to used named > parameters. > > myscript.ps1 -FistName Joe -MI A. -LastName Schmoe -Group > "CN=TestGroup,CN=Users,DC=yefimov,DC=org" > > param($strFirstName, $strMiddleInitial, $strLastName, $strGroup) > ##$strFirstName = "Joseph" > ##$strMiddleInitial = "A." > ##$strLastName = "Schmoe" > $strUserName = $strFirstName.Substring(0,1) + $strLastName > $strDisplayName = $strFirstName + " " + $strMiddleInitial + " " + > $strLastName > $strEmail = $strFirstName + "." + $strLastName + "@yefimov.org" > $strUpn = $strUserName + "@yefimov.org" > $strSamid = $strUserName > ##$strGroup = "CN=TestGroup,CN=Users,DC=yefimov,DC=org" > $strCNName = "CN=Users,DC=yefimov,DC=org" > $strValue = "CN="+ $strDisplayName + "," + $strCNName > $strSamAccount = get-adobject -value $strUserName > if ($strSamAccount.samaccountname -eq $strUserName) > { > Write-Host "The username" $strUserName "already exists! Please > change > the username by adding another letter from the user's first name" `n`n > -foregroundcolor red > } > else > { > write-host -noNewLine "SamAccountName available." -foregroundcolor > green > Write-Host -noNewLine " Creating Windows Account please wait." > -foregroundcolor blue > for ($i=0;$i -le 50; $i++) > { > if ($i -lt 50) > { > Write-Host -noNewLine "." -foregroundcolor blue > Start-Sleep -m 50 > } > else > {Write-Host "." -foregroundcolor blue} > } > dsadd user $strValue -upn $strUpn -samid $strSamid -fn > $strFirstName -mi > $strMiddleInitial -ln $strLastName -display $strDisplayName -memberof > $strGroup -mustchpwd yes > } > "rogwei@comcast.net" wrote: > >> Just to elaborate, there are three methods to provide input to a >> script. >> >> -positional parameters (using special $args[] variable) -named >> parameters (using param(...) statement) -read-host cmdlet >> >> Examples: >> -positional >> myscript.ps1 'bar' >> $foo = $args[0] # $foo evaluates to the string 'bar'. #use $foo... >> >> If you have more than one parameter, the user must enter them in the >> correct order hence positional. Your code usually depends on the >> correct order of the values in the $args[] array. >> >> -named >> myscript.ps1 -foo bar -bam baz >> param($foo,$bam) #This line must be the first non-comment line in the >> script. >> # $foo evaluates to bar; $bam evaluates to baz >> #use $foo and $bam... >> -read-host cmdlet #This method requires user input. myscript.ps1 >> >> $foo = read-host -prompt "Please enter foo value: " >> # $foo evaluates to whatever the user enters in response to the >> prompt. >> #use $foo... >> On Aug 7, 3:01 am, "Edengundam" <feng_e...@163.com> wrote: >> >>> Declaring parameter in script use param(...) in ur script. The basic >>> syntax in param is the same as in function. >>> >>> best wishes! >>> Edengundam >>> "Anatoli" <Anat...@discussions.microsoft.com> >>> wrotenews:3FA8E45D-B629-47CB-95A5-F9CF8A99F0D7@microsoft.com... >>> >>>> Hi, >>>> >>>> Can someone please tell me how I pass variables to a powershell >>>> script... >>>> >>>> thanks,- Hide quoted text - >>>> >>> - Show quoted text - >>> |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Passing variables to script The command named parameters must match exactly the variable names used in the param statement inside the script file except for the prefix, as in: command (named parameters are prefixed with '-'): foo.ps1 -bar xyz -baz abc script file (param variables are prefixed with '$'): # file: foo.ps1 param($bar,$baz) $s1 = $bar $s2 = $baz write-host "$s1`n$s2" Output: PS C:\> C:\foo.ps1 -bar xyz -baz abc xyz abc PS C:\> On Aug 7, 12:02 pm, Anatoli <Anat...@discussions.microsoft.com> wrote: > Ok, just to be clear. I want to pass $strFirstName, $strMiddleInitial, > $strLastName and $strGroup to the script below and want to used named > parameters. > > myscript.ps1 -FistName Joe -MI A. -LastName Schmoe -Group > "CN=TestGroup,CN=Users,DC=yefimov,DC=org" > > param($strFirstName, $strMiddleInitial, $strLastName, $strGroup) > ##$strFirstName = "Joseph" > ##$strMiddleInitial = "A." > ##$strLastName = "Schmoe" > $strUserName = $strFirstName.Substring(0,1) + $strLastName > $strDisplayName = $strFirstName + " " + $strMiddleInitial + " " + $strLastName > $strEmail = $strFirstName + "." + $strLastName + "@yefimov.org" > $strUpn = $strUserName + "@yefimov.org" > $strSamid = $strUserName > ##$strGroup = "CN=TestGroup,CN=Users,DC=yefimov,DC=org" > $strCNName = "CN=Users,DC=yefimov,DC=org" > $strValue = "CN="+ $strDisplayName + "," + $strCNName > > $strSamAccount = get-adobject -value $strUserName > if ($strSamAccount.samaccountname -eq $strUserName) > { > Write-Host "The username" $strUserName "already exists! Please change > the username by adding another letter from the user's first name" `n`n > -foregroundcolor red > } > else > { > write-host -noNewLine "SamAccountName available." -foregroundcolor green > Write-Host -noNewLine " Creating Windows Account please wait." > -foregroundcolor blue > for ($i=0;$i -le 50; $i++) > { > if ($i -lt 50) > { > Write-Host -noNewLine "." -foregroundcolor blue > Start-Sleep -m 50 > } > else > {Write-Host "." -foregroundcolor blue} > } > dsadd user $strValue -upn $strUpn -samid $strSamid -fn $strFirstName -mi > $strMiddleInitial -ln $strLastName -display $strDisplayName -memberof > $strGroup -mustchpwd yes > } > > > > "rog...@comcast.net" wrote: > > Just to elaborate, there are three methods to provide input to a > > script. > > > -positional parameters (using special $args[] variable) > > -named parameters (using param(...) statement) > > -read-host cmdlet > > > Examples: > > -positional > > myscript.ps1 'bar' > > > $foo = $args[0] # $foo evaluates to the string 'bar'. > > #use $foo... > > > If you have more than one parameter, the user must enter them in the > > correct order hence positional. Your code usually depends on the > > correct order of the values in the $args[] array. > > > -named > > myscript.ps1 -foo bar -bam baz > > > param($foo,$bam) #This line must be the first non-comment line in the > > script. > > # $foo evaluates to bar; $bam evaluates to baz > > #use $foo and $bam... > > > -read-host cmdlet #This method requires user input. > > myscript.ps1 > > > $foo = read-host -prompt "Please enter foo value: " > > # $foo evaluates to whatever the user enters in response to the > > prompt. > > #use $foo... > > > On Aug 7, 3:01 am, "Edengundam" <feng_e...@163.com> wrote: > > > Declaring parameter in script use param(...) in ur script. The basic syntax > > > in param is the same as in function. > > > > best wishes! > > > Edengundam > > > > "Anatoli" <Anat...@discussions.microsoft.com> wrotenews:3FA8E45D-B629-47CB-95A5-F9CF8A99F0D7@microsoft.com... > > > > > Hi, > > > > > Can someone please tell me how I pass variables to a powershell script... > > > > > thanks,- Hide quoted text - > > > > - Show quoted text -- Hide quoted text - > > - Show quoted text - |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Passing variables to script Ok, so when I call my script, I need to do the following: myscript.ps1 -strFirstName Joe -strMiddleInitial A. -strLastName Schmoe -strGroup "CN=TestGroup,CN=Users,DC=yefimov,DC=org" Correct? param($strFirstName, $strMiddleInitial, $strLastName, $strGroup) $strUserName = $strFirstName.Substring(0,1) + $strLastName $strDisplayName = $strFirstName + " " + $strMiddleInitial + " " + $strLastName $strEmail = $strFirstName + "." + $strLastName + "@ham.sitel.co.nz" $strUpn = $strUserName + "@ham.sitel.co.nz" $strSamid = $strUserName $strCNName = "OU=MyTestOU,DC=ham,DC=sitel,DC=co,DC=nz" $strValue = "CN="+ $strDisplayName + "," + $strCNName $strSamAccount = get-adobject -value $strUserName if ($strSamAccount.samaccountname -eq $strUserName) { Write-Host "The username" $strUserName "already exists! Please change the username by adding another letter from the user's first name" `n`n -foregroundcolor red } else { write-host -noNewLine "SamAccountName available." -foregroundcolor green Write-Host -noNewLine " Creating Windows Account please wait." -foregroundcolor blue for ($i=0;$i -le 50; $i++) {if ($i -lt 50) {Write-Host -noNewLine "." -foregroundcolor blue Start-Sleep -m 50} else {Write-Host "." -foregroundcolor blue} } dsadd user $strValue -upn $strUpn -samid $strSamid -fn $strFirstName -mi $strMiddleInitial -ln $strLastName -display $strDisplayName -memberof $strGroup -email $strEmail -mustchpwd yes } |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| problem passing args to script 'There is no script engine for file extenstion' | VB Script | |||
| Re: Passing variables into a CMD command | PowerShell | |||
| Include another script, keep variables in included script? | PowerShell | |||
| Passing of variables | VB Script | |||
| passing parameters to an external program using PowerShell variables | PowerShell | |||