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)
%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)
相关文章推荐
- Powershell 创建炫丽美观的Html报表
- 从Powershell ***脚本学到的如何执行后台runspace~
- PowerShell Script Analyzer, Script browser 和 Pester
- PowerShell打开或关闭光驱
- PowerShell移动目录中指定文件的方法(非全部文件)
- Powershell小技巧之创建短网址
- PowerShell实现查询打开某个文件的默认应用程序
- PowerShell设置文件只读、隐藏属性的方法
- Windows Powershell扩展类型系统
- PowerShell脚本源码输出到文件的最佳写法
- Windows Powershell 通过函数扩展别名
- PowerShell脚本实现创建桌面快捷方式的方法
- PowerShell重启服务命令Restart-Service详细介绍
- PowerShell中执行Javascript的方法示例
- 探索PowerShell(十三) WMI对象介绍
- Powershell小技巧之使用Copy-Item添加程序到开机启动
- Windows Powershell创建对象
- Windows Powershell 访问数组
- Powershell小技巧之使用WS-Man来调用PowerShell命令