hypervgethost
Hyper-V Get Host for VM
Summary: How to get the host for a particular VM in Hyper-V 2.
Date: Around 2020
Refactor: 1 March 2025: Checked links and formatting.
Use the following script to schedule a daily report to get the host for a particulat VM. The email functionality is imported from a default functions script as explained in Automate compliance in Active Directory.
Note that the script uses the SCCM Hyper-V commandlets, which are available after you install the Virtual Machine Manager (VMM) management server
The Script
# Script Variables $scriptname = [System.IO.Path]::GetFilenameWithoutExtension($MyInvocation.MyCommand.Path.ToString()) $scriptlocation = Split-Path $myinvocation.mycommand.path # Start transcript for full logging capabilities start-transcript -path "$scriptlocation\logtranscript.txt" # Date & Time Variables $timestamp = Get-Date -format "yyyyMMdd-HH.mm" $readdate = Get-Date -format "d MMM yyyy" $weekdate = Get-Date -uformat %V $monthdate = Get-Date -format "MMMM-yyyy" # Reporting Variables ### Email Variables $mailserver = "smtp1" $toemail = "s.hooft_getshifting.com" $ccmail = "s.hooft_getshifting.com" $fromemail = "$scriptname_getshifting.com" # Management Domain Variables $shiftuser = 'shift\sjoerd' $shiftpass = Get-Content "$scriptlocation\..\ICTscripts\shiftcreds.txt" | ConvertTo-SecureString $shiftcreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $shiftuser,$shiftpass # Hyper-V Variables $vmmserver = "vmm.shift.com" $mqvm = "mqserver" $gb = 1073741824 ### Start Functions ### # Import functions from global function file . "$scriptlocation\..\ICTscripts\Functions.ps1" # Connect to SCCM Virtual Machine Manager Get-SCVMMServer $vmmserver -Credential $shiftcreds # Get Host and hardware info where MQ VM is running $mqhost = Get-VMHost | where {$_.VMs -contains "$mqvm"} | select name,CPUManufacturer,CPUArchitecture,CPUModel,PhysicalCPUCount,CoresPerCPU,CPUSpeed,@{N="TotalMemoryInGB";E={[Math]::Round(($_.TotalMemory/$gb) ,2)}} $mqhost $hostname = $mqhost.name $hostcpuman = $mqhost.CPUManufacturer $hostcpumodel = $mqhost.cpumodel $hostpcpu = $mqhost.PhysicalCPUCount $hostcores = $mqhost.CoresPerCPU $hostspeed = $mqhost.CPUSpeed $hostram = $mqhost.TotalMemoryInGB # Setup email $info = "Dear $toemail , <br>" $info += "<br>" $info += "The server $mqvm is running on host $hostname <br>" $info += "$hostname has $hostpcpu $hostcpuman $hostcpumodel processors with each $hostcores cores with a speed of $hostspeed Mhz<br>" $info += "$hostname has $hostram GB memory <br>" # Set the subject and combine the email body collecting all information $subject = "MQ License Check for $readdate" # Send email Send-Email $subject $info # Stop Logging stop-transcript
hypervgethost.txt · Last modified: by 127.0.0.1