forked from GavinTownsend/Audit-Compliance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-HyperV_MachineList.ps1
79 lines (61 loc) · 2.17 KB
/
get-HyperV_MachineList.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<#
.SYNOPSIS
Exports a basic list of VM machines (for Hyper-V)
.NOTES
Script Name: get-HV_VirtualMachineList.ps1
Created By: Gavin Townsend
Date: August 2019
.DESCRIPTION
The script performs the follow actions:
- Exports a list of baisc machine data from Hyper-V cluster (does not need VMM)
Helps discover non-domain joined machines, or those in DMZ environments
.EXAMPLE
.\get-HV_VirtualMachineList.ps1
.REQUIREMENTS
WinRM enabled on target machines (for WMI)
Local administrator on target machines
NB. Some WMI scans do not work on all operating systems (particularly older ones)
.VERSION HISTORY
1.0 Aug 2019 Gavin Townsend Original Build
#>
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
$Log = "C:\Temp\Audit\$HyperVServer Server VM List $(get-date -f yyyy-MM-dd).txt"
$VMs = gwmi -namespace root\virtualization Msvm_ComputerSystem -computername $HyperVServer -filter "Caption = 'Virtual Machine'"
$OnlineCount = 0
$OfflineCount = 0
$table = @{}
foreach ($VM in [array] $VMs) {
$query = "Associators of {$VM} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_KvpExchangeComponent"
$Kvp = gwmi -namespace root\virtualization -query $query -computername $HyperVServer
$xml = ($Kvp.GuestIntrinsicExchangeItems | ? {$_ -match "OSName"})
$entry = $xml.Instance.Property | ?{$_.Name -eq "Data"}
if ($entry.Value){
$value = $entry.Value
$OnlineCount++
}
elseif ($VM.EnabledState -ne 2){
$value = "Offline"
$OfflineCount++
}
else {
$value = "Unknown"
}
if ($table.ContainsKey($value)){
$table[$value] = $table[$value] + 1
}
else {
$table[$value] = 1
}
}
$table.GetEnumerator() | Sort-Object Name | Format-Table -Autosize
$VMs | Export-Csv $Log -notype
write-Host ""
write-Host "---------------------------------------------------"
write-Host "Script Output Summary - Hyper-V Virtual Machines $(Get-Date)"
write-Host ""
write-Host "Online count: $OnlineCount"
write-Host "Offline count: $OfflineCount"
write-Host ""
write-Host "---------------------------------------------------"
write-Host ""
Write-Host "Hyper-V scanning tests concluded. Please review log $Log"