【发布时间】:2016-03-31 15:54:18
【问题描述】:
第一个帖子,但常客。 我正在尝试拼凑一个 powershell 脚本,以在 .csv 中的 vSphere 机器列表上启动关机。 vSphere 版本为 5.1u2,vcenter 在 2008 R2 上运行。我在 ps 脚本的开头加载了 powercli 的东西。
我找到了一个似乎完全符合我需要的脚本,但它看起来有一些问题。不确定我是否可以发布指向原始链接的链接。
这是我试图让它发挥作用的尝试。使用Powershell,以便在检测到某种状态时,可以通过监控工具启动它。
在这种状态下,它在第 12 行第 44 字符处给出了一个赋值表达式错误。不过,脚本的错误似乎远不止于此,尝试不同的方法会产生各种错误,包括“无法识别术语 import-csv作为 cmdlet”。
感谢任何指点 - 很抱歉插入不可靠的代码!
# Adds the base cmdlets needed to use powershell directly. Note the location of the powercli environment ps1 script - this may change in future releases of vsphere
Add-PSSnapin VMware.VimAutomation.Core
Add-PSSnapin VMware.VumAutomation
. "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
#The next line is only relevant and needs running once if you get an invalid ssl related message
#Set-PowerCLIConfiguration -InvalidCertificateAction Ignore
connect-viserver vc5
#Get the names of VMs from a .csv then shut down gracefully - assumes we have vmware to be effective on all
Import-Csv C:\CLIout\listofcriticalvms.csv |
foreach {
$strNewVMName = $_.name #Generate a view for each vm to determine power state
$vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = $strNewVMName}
#If vm is powered on then VMware Tools status is checked
if ($vm.Runtime.PowerState -ne "PoweredOff") {
if ($vm.config.Tools.ToolsVersion -ne 0) {
Write-Host "VMware tools installed. Graceful OS shutdown ++++++++ $strNewVMName ----"
Shutdown-VMGuest $strNewVMName -Confirm:$false
#For generating email
$Report += $strNewVMName + " --- VMware tools installed. Graceful OS shutdown `r`n"
}
else {
Write-Host "VMware tools not installed. Force VM shutdown ++++++++ $strNewVMName ----"
Stop-VM $strNewVMName -Confirm:$false
$Report += $strNewVMName + " --- VMware tools not installed. Force VM shutdown `r`n"
}
}
}
$Report | export-csv "C:\CLIout\GraceorHardfromcriticaloutput.csv"
【问题讨论】:
标签: powershell vsphere powercli