Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- param (
- [string]$hypervclustername,
- [string]$VMName
- )
- Function WriteXmlToScreen ([xml]$xml) {
- $StringWriter = New-Object System.IO.StringWriter;
- $XmlWriter = New-Object System.Xml.XmlTextWriter $StringWriter;
- $XmlWriter.Formatting = "indented";
- $xml.WriteTo($XmlWriter);
- $XmlWriter.Flush();
- $StringWriter.Flush();
- Write-Output $StringWriter.ToString();
- }
- # Make the script dynamic by getting the node list from the cluster
- $clusternodes = Get-ClusterNode -Cluster $hypervclustername | select name
- #add the list of VMs to an array
- $details = @()
- foreach($clusternode in $clusternodes) {
- $details += Get-VM -ComputerName $clusternode.name | select VMName, State, ComputerName, Uptime, CPUUsage, MemoryAssigned, MemoryDemand
- }
- #filter the array for the VM we asked for
- $details = $details | ?{$_.VMName -eq $VMName}
- #create the XML
- $XML = "<prtg>"
- foreach($detail in $details) {
- $VMName = $detail.VMName
- $VMState = $detail.State
- if($detail.state -eq "Running") {
- $CPUUsage = $detail.CPUUsage
- $cluster = $detail.ComputerName
- $MemoryUsage = ("{0:p0}" -f ($detail.MemoryDemand / $detail.MemoryAssigned)).Replace("%","")
- $XML += "<result>"
- $XML += "<channel>CPU Usage</channel>"
- $XML += "<value>$CPUUsage</value>"
- $XML += "<unit>CPU</unit>"
- $XML += "</result>"
- $XML += "<result>"
- $XML += "<channel>Memory Usage</channel>"
- $XML += "<value>$MemoryUsage</value>"
- $XML += "<unit>Percent</unit>"
- $XML += "</result>"
- $XML += "<text>Node: $cluster</text>"
- }elseif($VMState -eq "Off") {
- $XML += "<error>1</error>"
- $XML += "<text>VM Offline</text>"
- }
- }
- $XML += "</prtg>"
- #output the XML
- WriteXmlToScreen $XML
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement