0

So first of all i still dont get the exact difference between normal Powershell and PowerCLI with ESXCLI. 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. The Task for me is to make a pop-up windows notification for all vm's.

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. 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.

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.

1 Answers1

1

Clearing some things up:

  • PowerShell: cross-platform scripting language and associated shell
  • PowerCLI: A set of PowerShell modules created and maintained by VMware to interact with VMware products, including vCenter and ESXi hosts
  • 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. If it helps at all, these are most likely perl based.

PowerShell (by using PowerCLI) can call ESXCLI, but ESXCLI cannot make PowerShell calls.

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.

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.

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.

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. There's no direct network connectivity to the VM required. Docs on Invoke-VMScript

Kyle Ruddy
  • 1,886
  • 1
  • 7
  • 5
  • Thanks for your help and clarification on that topic. I already tried it like you suggested and there i get an Error that says that "the authentication theme is not Kerberos or the client computer is not in the domain." The client computer is in the domain so i think its the Kerbers autentification.... but i have no clue that it means exactly. Do you know anything about it ? – HirnlosCoding Apr 06 '21 at 12:05