繁体   English   中英

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

[英]powerCLI with ESCXLI to run a command with script for windows notification

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

我正在尝试做的主要任务:vCenter 通过 POWERCLI 执行 a.ps1 脚本,它已经可以重新启动虚拟机了。 我的任务是为所有虚拟机制作一个弹出式 windows 通知。

通知本身如下所示:

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, $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,因为您不需要直接管理主机,只需要管理虚拟机。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM