PowerShell: List the Hyper-V VM VHD Location


The following PS script is to query the VHD location of each VM running on the hosts listed in the C:\scripts\Hyperv_Hosts.txt file. cls cd "c:\Scripts" $HyperVParents = Get-Content Hyperv_Hosts.txt Foreach ($HyperVParent in $HyperVParents) { $VMManagementService = Get-WmiObject -class "Msvm_VirtualSystemManagementService" -namespace "root\virtualization" -ComputerName $HyperVParent $VMs = Get-WmiObject -Namespace "root\virtualization" -ComputerName $HyperVParent -Query "Select * From Msvm_ComputerSystem … Continue reading PowerShell: List the Hyper-V VM VHD Location

PowerShell: Hyper-V VM Inventory


Having a clean inventory of all virtual machines is a good practice to avoid virtual machine sprawl. I created a script to send me to the Monthly VM Inventory automatically, this includes the Guest name, status, OS, vCPU, RAM, host and notes. The results look like this: JC-VM-01STATUS: RunningVCPU: 1RAM: 2048 MOS: Windows Server 2008 … Continue reading PowerShell: Hyper-V VM Inventory

PowerShell: Monitor Hyper-V VM Heartbeat


$hostname = “ServerName” $service = gwmi -class "Msvm_VirtualSystemManagementService" -namespace "root\virtualization" -ComputerName $hostname $VM = gwmi -ComputerName $hostname -namespace "root\virtualization" -query "Select * from Msvm_ComputerSystem where Caption='Virtual Machine'" Foreach ($_ in $VM) { Write-Host $_.ElementName $settings = gwmi -ComputerName $hostname -namespace "root\virtualization" -query ("ASSOCIATORS OF {" + $_.__PATH + "} WHERE resultClass = Msvm_VirtualSystemsettingData") $HB = … Continue reading PowerShell: Monitor Hyper-V VM Heartbeat

PowerShell: Send Email


I scheduled a task to send me the every day report by Email. Here is how I did by using PowerShell: $emailFrom = Monitor@test.com$emailTo = jackie.chen@test.com $subject = "Everyday Report"$body = Get-Content C:\Logs\EverydayReport.txt $smtpServer = "SMTP.test.com"$smtp = new-object Net.Mail.SmtpClient($smtpServer)$smtp.Send($emailFrom, $emailTo, $subject, $body)