Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Passing variables to script

Reply
 
Old 08-07-2007   #1 (permalink)
Anatoli


 
 

Passing variables to script

Hi,

Can someone please tell me how I pass variables to a powershell script...

thanks,

My System SpecsSystem Spec
Old 08-07-2007   #2 (permalink)
Shay Levi


 
 

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 SpecsSystem Spec
Old 08-07-2007   #3 (permalink)
Edengundam


 
 

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 SpecsSystem Spec
Old 08-07-2007   #4 (permalink)
rogwei@comcast.net


 
 

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 SpecsSystem Spec
Old 08-07-2007   #5 (permalink)
Anatoli


 
 

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 SpecsSystem Spec
Old 08-07-2007   #6 (permalink)
Shay Levi


 
 

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 SpecsSystem Spec
Old 08-07-2007   #7 (permalink)
rogwei@comcast.net


 
 

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 SpecsSystem Spec
Old 08-07-2007   #8 (permalink)
Anatoli


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46