简体   繁体   English

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

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

So first of all i still dont get the exact difference between normal Powershell and PowerCLI with ESXCLI.所以首先我仍然没有得到普通 Powershell 和带有 ESXCLI 的 PowerCLI 之间的确切区别。 I really tried unterstanding it how it works etc. So yeah i did some research.我真的试着理解它是如何工作的等等。所以是的,我做了一些研究。 Now to my problem现在我的问题

MAIN TASK IM TRYING TO DO: The vCenter excecutes a.ps1 script via POWERCLI it already can restart the vm's as it should.我正在尝试做的主要任务:vCenter 通过 POWERCLI 执行 a.ps1 脚本,它已经可以重新启动虚拟机了。 The Task for me is to make a pop-up windows notification for all vm's.我的任务是为所有虚拟机制作一个弹出式 windows 通知。

The notification itself looks like this:通知本身如下所示:

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)

I installed ESXCLI via a guide i found online i only had to restart the.ini for it to be installed.我通过网上找到的指南安装了 ESXCLI,我只需要重新启动.ini 即可安装它。 I then tried some stuff in my code but everything i do gets me an syntax-based error...然后我在我的代码中尝试了一些东西,但我所做的一切都会给我一个基于语法的错误......

$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)
}

Thats everything thrown together to see if anything i try works.这就是把所有东西放在一起,看看我尝试的任何东西是否有效。 But sadly it didnt.但遗憾的是没有。 The errors i get are all syntax-based as far as i could unterstand them so thats why i think that i didnt quite understand how ESXCLI is working.据我所知,我得到的错误都是基于语法的,所以这就是为什么我认为我不太了解 ESXCLI 是如何工作的。

It would be nice if you guys could help me even just a link to a good tutorial on that specific task im tyring to to would be awesome.如果你们能帮助我,即使只是一个关于我想要完成的特定任务的好教程的链接,那就太好了。

Clearing some things up:清理一些东西:

  • PowerShell: cross-platform scripting language and associated shell PowerShell:跨平台脚本语言和相关的 shell
  • PowerCLI: A set of PowerShell modules created and maintained by VMware to interact with VMware products, including vCenter and ESXi hosts PowerCLI:一组由 VMware 创建和维护的 PowerShell 模块,用于与 VMware 产品(包括 vCenter 和 ESXi 主机)交互
  • ESXCLI: a set of commands that can be executed through most common shells that are dedicated to managing ESXi hosts, and is also available on the hosts themselves. ESXCLI:一组命令,可以通过专用于管理 ESXi 主机的最常见的 shell 执行,也可在主机本身上使用。 If it helps at all, these are most likely perl based.如果它有帮助,这些很可能是基于 perl 的。

PowerShell (by using PowerCLI) can call ESXCLI, but ESXCLI cannot make PowerShell calls. PowerShell(通过使用 PowerCLI)可以调用 ESXCLI,但 ESXCLI 不能进行 PowerShell 调用。

You don't happen to mention what version of vSphere you're using, but generally the preference is to be using their vCenter Server Appliance... Which would not give you the ability to install or use PowerShell in a supported manner.您没有提到您使用的是哪个版本的 vSphere,但通常首选是使用他们的 vCenter Server Appliance ......这不会让您能够以受支持的方式安装或使用 PowerShell。

Taking a step back, if I were in your shoes, I would take the ps1 script that's being called and modify it to include your notification code.退后一步,如果我站在您的立场上,我会采用被调用的 ps1 脚本并对其进行修改以包含您的通知代码。

It would probably look something like:它可能看起来像:

# 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

Using the above, there's no real reason to reference ESXCLI since you don't need to manage the hosts directly, only the VMs.使用上述内容,没有真正的理由引用 ESXCLI,因为您不需要直接管理主机,只需要管理虚拟机。

Alternatively, there's also a PowerCLI cmdlet known as Invoke-VMScript which will allow you to run scripts, such as your warning notification, on the VM through VMware Tools instead of using Invoke-Command .或者,还有一个称为Invoke-VMScript的 PowerCLI cmdlet,它允许您通过 VMware Tools 而不是使用Invoke-Command在 VM 上运行脚本,例如警告通知。 There's no direct network connectivity to the VM required.不需要到 VM 的直接网络连接。 Docs on Invoke-VMScript Invoke-VMScript 上的文档

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

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