带有 ESCXLI 的 powerCLI 运行带有 Windows 通知脚本的命令

问题描述

所以首先我仍然没有得到普通 Powershell 和带有 ESXCLI 的 PowerCLI 之间的确切区别。我真的试着理解它是如何工作的等等。所以是的,我做了一些研究。 现在我的问题

我要完成的主要任务: vCenter 通过 POWERCLI 执行 .ps1 脚本,它已经可以正常重启虚拟机。我的任务是为所有虚拟机制作一个弹出窗口通知

通知本身如下所示:

Add-Type -AssemblyName System.Windows.Forms
$global:balmsg = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balmsg.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balmsg.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balmsg.BalloonTipText = "Die VM startet in 30 Sekunden neu"
$balmsg.BalloonTipTitle = "Achtung!"
$balmsg.Visible = $true
$balmsg.ShowBalloonTip(1)

我通过网上找到的指南安装了 ESXCLI,我只需要重新启动 .ini 即可安装它。 然后我在我的代码中尝试了一些东西,但我所做的一切都会给我一个基于语法的错误......

$esxcli = Get-EsxCli -VMHost vCenter -V2

esxcli storage nfs list
Invoke-Command -ComputerName $vm -Credential xxx.local\$vm -ScriptBlock { Get-Culture }
$esxcli.shell.cmd("Invoke-Command -ComputerName VM-xxx -ScriptBlock { Get-Culture }",$null,$null)



$esxcli.shell.cmd("Invoke-Command -ComputerName $vm -Credential xxx.local\$vm -ScriptBlock { Get-Culture }")
        
Invoke-Command -ComputerName VM-xxx -ScriptBlock {
    Add-Type -AssemblyName System.Windows.Forms
    $global:balmsg = New-Object System.Windows.Forms.NotifyIcon
    $path = (Get-Process -id $pid).Path
    $balmsg.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
    $balmsg.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
    $balmsg.BalloonTipText = "Die VM startet in 30 Sekunden neu"
    $balmsg.BalloonTipTitle = "Achtung!"
    $balmsg.Visible = $true
    $balmsg.ShowBalloonTip(1)
}

这就是把所有东西放在一起看看我尝试的任何东西是否有效。但遗憾的是它没有。 我得到的错误都是基于语法的,因为我无法理解它们,所以这就是为什么我认为我不太了解 ESXCLI 是如何工作的。

如果你们能帮助我甚至只是一个链接一个关于我渴望完成的特定任务的好教程的链接,那就太好了。

解决方法

清理一些事情:

  • PowerShell:跨平台脚本语言和相关的 shell
  • PowerCLI:一组由 VMware 创建和维护的 PowerShell 模块,用于与 VMware 产品交互,包括 vCenter 和 ESXi 主机
  • ESXCLI:一组命令,可通过专用于管理 ESXi 主机的最常见 shell 执行,也可在主机本身上使用。如果有帮助的话,这些很可能是基于 perl 的。

PowerShell(通过使用 PowerCLI)可以调用 ESXCLI,但 ESXCLI 不能调用 PowerShell。

您没有提到您使用的是哪个版本的 vSphere,但通常首选是使用他们的 vCenter Server Appliance...这不会使您能够以受支持的方式安装或使用 PowerShell .

退一步说,如果我是你的话,我会获取正在调用的 ps1 脚本并修改它以包含你的通知代码。

它可能看起来像:

# Connect to vCenter Server
Connect-VIServer    

# Gathers the VMs to be restarted 
$vms = Get-VM

# Create a foreach loop to interact with each VM that was found in the prior step 
foreach ($vm in $vms) {
  
  # Using PowerShell to connect to the VM through the OS,run your invoke-command notification/alert
  Invoke-Command -ComputerName $vm.name -ScriptBlock {
    Add-Type -AssemblyName System.Windows.Forms
    $global:balmsg = New-Object System.Windows.Forms.NotifyIcon
    $path = (Get-Process -id $pid).Path
    $balmsg.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
    $balmsg.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
    $balmsg.BalloonTipText = "Die VM startet in 30 Sekunden neu"
    $balmsg.BalloonTipTitle = "Achtung!"
    $balmsg.Visible = $true
    $balmsg.ShowBalloonTip(1)
  }

  # Sleep for 30 seconds
  Start-Sleep -Seconds 30

  # Restart virtual machine
  Restart-VMGuest -VM $vm
}

# Disconnect from the vCenter server
Disconnect-VIServer

使用上述内容,没有真正的理由引用 ESXCLI,因为您不需要直接管理主机,只需管理 VM。

或者,还有一个名为 Invoke-VMScript 的 PowerCLI cmdlet,它允许您通过 VMware Tools 而不是使用 Invoke-Command 在虚拟机上运行脚本,例如警告通知。不需要与 VM 的直接网络连接。 Docs on Invoke-VMScript