zoukankan      html  css  js  c++  java
  • PowerCLI 对vm批量关机

    https://docs.vmware.com/cn/VMware-vSphere/index.html  参考文档

    获取 引用 库

    Save-Module -Name VMware.PowerCLI -Path D:PowerCLI 

    $ENV:PSModulePath  查看 ps模块路径

    把下载的 模块 复制到 ps模块路径下(如把PowerCLI里的内容移到到C:Program FilesWindowsPowerShellModules内)

     

    建立一个 powershell 文件,代码如下,

    应注意  修改下 EsxiHost  为Esxi 的IP,或vCenter的IP或安装VMware Workstation 的IP。

    还要相应的密码EsxiPassWd 。

    思路:

    1:链接前,先禁用无证书 提示(有证书的忽略此处)。

    2:获取所有 加电的主机

    3:遍历主机的ToolsStatus ,也就是 tools 状态

    4:如果tools 状态不为 toolsNotInstalled 的,则关闭客户机否则关机。

    5:5秒后,获取所有加电主机。

    6:如果数量不为0,则返回第2步,否则 向主机发送关机指令。

     

    $TextUtf8 = New-Object -typename System.Text.UTF8Encoding
    Write-Host
    Write-Host "      安装tools的关闭系统,没安装tools的关闭电源"
    Write-Host "      vCnter关机要 4-6 分钟"
    Write-Host
    pause
     
    $EsxiHost = "127.0.0.1"
    $EsxiUser = "root"
    $EsxiPassWd = "Passwd@yhc18"
    Write-Host " 正在连接 服务器 。。。 。。。"
    $ConnIgnore = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -confirm:$false
    $ConnectVIServer = Connect-VIServer -Server $EsxiHost -username $EsxiUser -Password $EsxiPassWd
     
    while(1 -eq 1){   
        $PowerOnVm = get-vm | where {$_.PowerState -eq "PoweredOn"}
        $PowerOnNum=$PowerOnVm.count
        if($PowerOnVm.count -gt 0){        
            cls
            Write-Host "ID" . "vmName" "ToolsStatus" "(正在运行中)"
            $index = 0       
            $PowerOnVm | foreach{
                $index++
                 if($_.extensiondata.guest.ToolsStatus -ne "toolsNotInstalled"){
                    Write-Host $index.    $_.Name    "    on"
                 }else{
                    Write-Host $index.    $_.Name    "    off"
                 }
            }
            Write-Host "------------------------------------------"
            $index2=0
            $PowerOnVm | foreach{              
                if($_.extensiondata.guest.ToolsStatus -ne "toolsNotInstalled"){
                    $index2++;
                    Write-Host "$index2/$PowerOnNum"  "关闭客户机" $_.Name
                    $stopGuest = Get-Vm -Name $_.Name |stop-VMGuest -confirm:$false -ErrorAction SilentlyContinue
                    Start-Sleep -Seconds 3
                }else{
                    $index2++;
                    Write-Host "$index2/$PowerOnNum"  "关闭机器" $_.Name
                    $stopGuest = Get-Vm -Name $_.Name |stop-VM -confirm:$false -ErrorAction SilentlyContinue
                    Start-Sleep -Seconds 3                
                }
            }
        }else{
            $PowerOnHosts = Get-VMHost| Where {$_.Name -eq $EsxiHost} | Stop-VMHost -confirm:$false -Force:$true
            cls
            echo "虚拟机已关机完毕,发送Esxi关机指令,等待Esxi主机关机"
            pause
            break #跳出循环       
        }
        Start-Sleep -Seconds 5
    }
     
  • 相关阅读:
    python while循环语句
    Python if条件语句
    推荐一款java的验证码组件——kaptcha
    Linux硬链接和符号链接(转)
    推荐一款好用的java反编译软件——JavaDecompiler
    数据库性能优化之冗余字段的作用
    通过设置代理,解决服务器禁止抓取,报“java.io.IOException: Server returned HTTP response code: 403 for URL”错误的方法
    浏览器在同域名下有并发加载的限制
    XCODE快捷键和功能汇总篇(不断更新)
    队列应用场景,自己实现队列
  • 原文地址:https://www.cnblogs.com/likehc/p/11439544.html
Copyright © 2011-2022 走看看