I am running a script and configured to prompt me for vCenter name and user credentials. I am looking to key in along with the script, something like below
Script.ps1 vCentername username password
Or Use the switch method
Script.ps1 –vCenter:vCentername –username:username –password:password
Hey,
so what's your question? How to do it? ![]()
MS technet show you how: http://technet.microsoft.com/en-gb/magazine/jj554301.aspx
Tim
Both formats will work, the difference is if you want to define the parameter in your script or not.
In the 1st line format, you will be able to address the values as $args[0] and $args[1].
In the 2nd line format, you will need to specify the parameters in a Param statement.
At the beginning of your script add
Param(
[string]$Username,
[string]$Password
}
The 2nd version also allows you to use the values as in line 1, but then you use them as positional parameters.
In that case the order you define the parameters, defines their positional value.
Your 2nd line is using what is known as named parameters.
Needless to say that using a Param statement is preferred.
You can do additional things, like for example validating the passed values.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I am using a XLSx function to create a a nice excel, now should I declare these parameter in the function
[string]$vCenterName,
[string]$username,
[string]$password
Is there any other place I would need to include?
The Export-Xlsx function has all it's parameters defined in a Param statement inside the function.
So in the function itself you don't have to add anything.
But if you don't want to hard-code the vCenter name inside your script, then you can better pass it as a parameter
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I dont see the username and password param defined , also when I define vCenter name in parameter, should we need change anything on actual script for Connect-VIServer. I need to get an output file with vCenter name in it, so I am checking what changes to be made
Attaching the script which I am currently running
You need to add a Param statement to your .ps1 file.
And then you use the parameters on the Connect-ViServer cmdlet.
To call the script you then can do
./script.ps1 -vCenterName MyvCenter -Username MeMyselfAndI -Password '12345abcdef'
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
