【问题标题】:Powershell script to shut down VMS - erros用于关闭 VMS 的 Powershell 脚本 - 错误
【发布时间】: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


    【解决方案1】:

    您是否尝试删除该行并手动输入?因为据我所知,第 12 行一切正常(这里是 VMware 官方帮助中针对此命令的代码:

    $vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = "VM"}
    

    所以也许有一些复制\粘贴问题?另外,您是否尝试在 ISE 中对其进行调试?你得到什么错误?

    【讨论】:

    • 嗨 - 我手动输入了相同的结果。 $strNewVMName - 它需要用引号引起来吗?这样做会删除原始错误,但脚本会抱怨无法识别术语“Import-Csv C:\myfile\here.csv”以及 cmdlet、函数、脚本文件或可运行程序的名称。 Export-Csv 然后做了显而易见的事情并抱怨一个空对象。我的 csv 文件看起来像这样“名称”“旧测试机器”
    【解决方案2】:

    而不是

    Import-Csv C:\CLIout\listofcriticalvms.csv
    

    试试

    $csv = "C:\CLIout\listofcriticalvms.csv"
    Import-Csv -Path $csv
    

    或者只是

    $csv = "C:\CLIout\listofcriticalvms.csv"
    Import-Csv $csv
    

    或者参考这个 https://github.com/gajuambi/vmware/blob/master/vTool/vToolMenus/MainMenu/vCenterMenu/vdsMenu/AddDpg.ps1 正确使用 csv。

    【讨论】:

      【解决方案3】:

      想通了。 'Import-Csv C:\myfile\here.csv' 无法识别且 cmdlet 的名称

      原因总是如此简单。有问题的 cmdlet 无法在 powershell 1 上运行,而我正在测试的服务器是默认安装。修复丢失的引号和中提琴后更新到版本 3!

      我勾选了 4c74356b41 的答案,因为添加缺少的引号导致我出现下一个错误,这导致我意识到我使用的是版本 1。

      现在看看我是否可以让它在列表中的每个服务器上异步运行,然后等待并关闭所有剩下的东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-15
        • 2019-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-19
        • 1970-01-01
        相关资源
        最近更新 更多