I have a large number of scripts that I would like to execute in batches in PowerCLI. I found a way to call Powershell scripts from a batch file such as the following (courtesy of Jan Egil Ring), which works fine except for PowerCLI scripts where a connection to a VC or ESX server must be established before the script can be executed:
powershell -command "&C:\MyScripts\script1.ps1"
powershell -command "&C:\MyScripts\script2.ps1"
powershell -command "&C:\MyScripts\script3.ps1"
If a VC connection is established within PowerCLI using Connect-VIServer, then the scripts can each be run from the prompt without entering further connection information and credentials, however, if the scripts are placed in a DOS batch file, it is necessary to place a Connect-VIServer command in each script, which will prompt for credentials each time, so if I have 10 scripts, I will have to enter the credentials 10 times. Is there a way to either establish the connection and then run a set of Powershell scripts, or is there way to securely embed the credentials in the script so that it will connect to the VI server without prompting?
Thanks,
Ed Z
2 options, 1 persist the credential to disk, you can find code on poshcode.org to do this.
Option 2 is to use -Session when using Connect-VIServer. The session is stored in the $defaultVIServer variable. If you write it to a file you can load that file and re-use that session, assuming it is still valid (i.e. it hasn't timed out.)
2 options, 1 persist the credential to disk, you can find code on poshcode.org to do this.
Option 2 is to use -Session when using Connect-VIServer. The session is stored in the $defaultVIServer variable. If you write it to a file you can load that file and re-use that session, assuming it is still valid (i.e. it hasn't timed out.)
Isn't there a 3rd option ?
You run the CMD file with an account that has the required permissions on the VC and then you can do the Connect-ViServer using passthrough authentication without specifying credentials nor session tokens.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you for the answers. Once I read them, it started me thinking about other possibilities of how to handle this. It looks like one of them is so simple, that I didn't think it would work:
Alternate Solution - Seems to work fine
1. Open PowerCLI command prompt.
2. Connect to the VI server using Connect-VIServer, and enter credentials when prompted.
3. Run a powershell script from within a script - for example:
Call_Report_Scripts.ps1 ***
Report1.ps1
Report2.ps1
Where Report1.ps1 and Report2.ps1 contain PowerCLI commands but do not have any Connect-VIServer commands, since it assumes the connection is already established.
4. Execute Call_Report_Scripts.ps1 from the current PowerCLI command prompt.
5. The scripts execute from within a script without prompting. Progress bars are visible also.
I've tried it and it seems to work fine. Now, I'm wondering is this supported? Can you call a Powershell script from within a Powershell script without unexpected consequences? Will this support be deprecated at some point? Is this the correct way to call a script from within a script?
Finally, another possible solution, which I have not tried, and which seems overly complex and prone to errors, is to call a Powershell script from a batch file passing arguments within the batch file. I only list it here in case this line of thought would be useful. The arguments within the batch file could then be passed to Powershell such as the following - which will not work as written - it would have to be modified to call the PowerCLI environment and extensions instead of a standard Powershell session, and that is another problem with this approach:
Call_Report_Scripts.bat ***
powershell -command "&C:\Scripts\Report1.ps1 %1 %2"
The batch file Call_Report_Scripts.bat could be called as follows
Then the individual Powershell scripts could be modified to include the paramaters passed to the batch file to be passed as arguments as follows
Report1.ps1 **
Connect-VIServer -server $args[0] -password $args[1]
...do stuff...
Cheers,
Ed Z
There are no side-effects to running a script from within a script.
Just take note of the following "special" cases:
1) The pathname contains a blank.
You have to use the & operator to run the script
& "C:\Script Folder\My Script.ps1"
2) Dot sourcing
When you run a script the variables from the script will not be known after execution in the calling script.
This is due to the scoping rules of PowerShell.
If you want to force the variables from the script to be known in the calling script you can dot source the script
. C:\Scripts\script1.ps1
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
How do you pass through credentials with connect-viserver ?
TIA
Graham
I may not understand the question, but I will try to answer. I open a PowerCLI command prompt, then use the connect-viserver command to authenticate, then all scripts executed within that session will use the credentials already supplied (if none are specified in the script).
Cheers,
Ed