您的位置:首页 > 运维架构 > Shell

VMware: PowerCli Scripts in native Powershell

2016-07-18 14:24 676 查看
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
%Systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe-File YourPowerCliScript.ps1
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  powercli powershell