whenyou want to execute your PowerCli script you have to start the “VMware vSpherePowerCLI” shell before. But, for example Schedule Tasks, it whould be nice thatthe script load the PowerCli environment itself. Then you simply have to startyour script like this
To load the PowerCli script Environent in yourpowershell script add this lines at the top of your script.
1 Add-PSSnapin VMware.VimAutomation.Core
2 Add-PSSnapin VMware.VimAutomation.Vds
3 if(get-item HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\VMware.VimAutomation.Core){
4 . ((get-item HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\VMware.VimAutomation.Core).GetValue("ApplicationBase")+"\Scripts\Initialize-PowerCLIEnvironment.ps1")
5 }
6 else
7 {
8 write-warning "PowerCLI Path not found in registry, please set path to Initialize-PowerCLIEnvironment.ps1 manually. Is PowerCli aleady installed?"
9 . "D:\Programs (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
10 }
11
12 # Connect to vCenter
13 $sVCenterHost="vcenter.subdomain.domain.local"
14 Write-Host -NoNewline " Connecting to vCenter..."
15 Connect-VIServer $sVCenterHost -ErrorAction SilentlyContinue -WarningAction SilentlyContinue |out-null
16 if(!$?){
17 Write-Host -ForegroundColor Red " Could not connect to $sVCenterHost"
18 exit 2
19 }
20 else{
21 Write-Host "ok"
22 }
Thescript reads the PowerCLI installation path from the registry, load the snapinsVMware.VimAutomation.Core and VMware.VimAutomation.Vds and executesInitialize-PowerCLIEnvironment.ps1 in the current shell (the leading “.” isimportent)