Advertisement
Guest User

Untitled

a guest
Oct 13th, 2020
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param (
  2.     [string]$hypervclustername,
  3.     [string]$VMName
  4. )
  5.  
  6. Function WriteXmlToScreen ([xml]$xml) {
  7.     $StringWriter = New-Object System.IO.StringWriter;
  8.     $XmlWriter = New-Object System.Xml.XmlTextWriter $StringWriter;
  9.     $XmlWriter.Formatting = "indented";
  10.     $xml.WriteTo($XmlWriter);
  11.     $XmlWriter.Flush();
  12.     $StringWriter.Flush();
  13.     Write-Output $StringWriter.ToString();
  14. }
  15.  
  16. # Make the script dynamic by getting the node list from the cluster
  17. $clusternodes = Get-ClusterNode -Cluster $hypervclustername | select name
  18.  
  19. #add the list of VMs to an array
  20. $details = @()
  21. foreach($clusternode in $clusternodes) {
  22.     $details += Get-VM -ComputerName $clusternode.name | select VMName, State, ComputerName, Uptime, CPUUsage, MemoryAssigned, MemoryDemand
  23. }
  24.  
  25. #filter the array for the VM we asked for
  26. $details = $details | ?{$_.VMName -eq $VMName}
  27.  
  28. #create the XML
  29. $XML = "<prtg>"
  30. foreach($detail in $details) {
  31.     $VMName = $detail.VMName
  32.     $VMState = $detail.State
  33.     if($detail.state -eq "Running") {
  34.         $CPUUsage = $detail.CPUUsage
  35.         $cluster = $detail.ComputerName
  36.         $MemoryUsage = ("{0:p0}" -f ($detail.MemoryDemand / $detail.MemoryAssigned)).Replace("%","")
  37.    
  38.         $XML += "<result>"
  39.         $XML += "<channel>CPU Usage</channel>"
  40.         $XML += "<value>$CPUUsage</value>"
  41.         $XML += "<unit>CPU</unit>"
  42.         $XML += "</result>"
  43.  
  44.         $XML += "<result>"
  45.         $XML += "<channel>Memory Usage</channel>"
  46.         $XML += "<value>$MemoryUsage</value>"
  47.         $XML += "<unit>Percent</unit>"
  48.         $XML += "</result>"
  49.         $XML += "<text>Node: $cluster</text>"
  50.     }elseif($VMState -eq "Off") {
  51.         $XML += "<error>1</error>"
  52.         $XML += "<text>VM Offline</text>"
  53.     }
  54. }
  55. $XML += "</prtg>"
  56.  
  57. #output the XML
  58. WriteXmlToScreen $XML
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement