VMware Cloud Community
aravinds3107
Virtuoso
Virtuoso

Provide server name and credentials with the script

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

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
Reply
0 Kudos
6 Replies
schepp
Leadership
Leadership

Hey,

so what's your question? How to do it? Smiley Wink

MS technet show you how: http://technet.microsoft.com/en-gb/magazine/jj554301.aspx

Tim

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
aravinds3107
Virtuoso
Virtuoso

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?

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
aravinds3107
Virtuoso
Virtuoso

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

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos