# Step 1: Suppose I have a foo.ps1 file as below,and saved as
C:\foo.ps1
#---- Begin
script foo.ps1 ----
"`nOption 1:
"
Write-Host "Num
Args:" $args.Length;
foreach ($arg
in $args)
{
Write-Host "Arg: $arg";
}
#---- End script foo.ps1 ----
<#Use the
code below to replace the code in option 1, save it and call it in the same
way.
"`nOption 2:
"
$ServerName=$args[0]
$Location=$args[1]
"Server Name: $ServerName"
"Location: $Location"
#>
<# Use the
code below to replace the code in options 1 and 2, save it and call it in the
same way.
"`nFor Option 3:
You need to comment out the code above as Param needs to be the 1st line of the
code. Othewise, you will get an error as below: "
param ($ServerName1, $Location2)
"Server Name: $ServerName1"
"Location: $Location2"
#>
# Step 2: Then I call the script as below
PS C:\>
.\foo.ps1 myServer
myLocation
# Step 3: I would get the result as below:
<#
Option 1:
Num Args: 2
Arg: myServer
Arg: myLocation
Option 2:
Server Name: myServer
Location: myLocation
For Option 3: You need to comment out the code above as Param
needs to be the 1st line of the code. Othewise, you will get an error as below:
param : The term 'param' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was
included, verify that the path is correct and try again.
At C:\foo.ps1:17 char:1
+ param ($ServerName1, $Location2)
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (param:String) [],
CommandNotFoundException
+
FullyQualifiedErrorId : CommandNotFoundException
Server Name:
Location:
#>