2012-07-10

Running VMware PowerCLI scripts with right click and "Run with Powershell"


Running VMware PowerCLI scripts with right click and "Run with Powershell"


 

------ PowerCLI environment preparation (optional and only needed to set once in the computer)

If you haven't set this before, set it now to avoid multiple warnings

 

Open a PowerCLI console and enter:

Set-ExecutionPolicy bypass

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore

------ end of preparation

 

 


-------- Modification in your script

And now, the first command on your file.ps1 should be this
 
Add-pssnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
 
 
This will load the PowerCLI stuff so what started as a Powershell script runs now as PowerCLI

------- end of modification
 


If you have problems running the script, open a PowerShell console (not PowerCLI) and run your script from there as ".\file.ps1"
Once you remove all errors, try again to run it with right click.


And here you have two examples that I use to do some repros/testing with VSA 1.0


--------- repro-add-hosts.ps1 -------------
 
# Copyright @ Ruben Miguelez Garcia
 
# Load PowerCLI
add-PSSnapIn  -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue;
 
# Connect to vCenter
Connect-VIServer -server vc -user administrator -password secret
 
# Get folder and Datacenter if exists
$folder=get-folder -norecursion
 
$d=""
$d=Get-Datacenter -Name DC -ErrorAction SilentlyContinue;
if (-not $?){$d = new-datacenter -Location $folder -Name DC ; }
 
# Add hosts
Add-VMHost 10.10.10.10 -location $d -user root -password vmware123 -force
Add-VMHost 10.10.10.20 -location $d -user root -password vmware123 -force
Add-VMHost 10.10.10.30 -location $d -user root -password vmware123 -force
 
 
--------- repro-remove-hosts.ps1 -------------
 
# Copyright @ Ruben Miguelez Garcia
 
# Load PowerCLI
add-PSSnapIn  -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue;
 
# Connect to vCenter
Connect-VIServer -server vc -user administrator -password secret
 
# Remove hosts
Remove-VMHost -VMHost 10.10.10.10 -Confirm:$false
Remove-VMHost -VMHost 10.10.10.20 -Confirm:$false
Remove-VMHost -VMHost 10.10.10.30 -Confirm:$false
 
-----------------------------------------------