-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathsimple_vm_report.ps1
executable file
·79 lines (75 loc) · 2.27 KB
/
simple_vm_report.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
79
param
(
[string]$Server,
[parameter(mandatory=$true)][string]$CSVFile
)
# Check if VMware Snappin is loaded for when ran outside of PowerCli
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
Write-Host "[*] VMware Automation Snapin is not loaded, attempting to load..." -ForegroundColor Green
Try {
Add-PsSnapin VMware.VimAutomation.Core | Out-Null
}
Catch {
$Host.UI.WriteErrorLine("[-] Could not load snapping check that PowerCli is installed.")
exit
}
}
#verify we're connected to a VM host
if ($Server){
Try {
if (get-vmhost -Server $Server -State connected -erroraction "Stop") {
$connected=$True
}
}
Catch {
#Try to connect
Try {
Write-Host -ForegroundColor Green "[*] Connecting to $Server"
$viserver=Connect-VIserver -Server $Server -errorAction "Stop"
$connected=$True
}
Catch {
$msg="[-] Failed to connect to server $Server"
Write-Warning $msg
Write-Warning $error[0].Exception.Message
}
}
}
$vms = Get-VM
$report = @()
Write-Host "[*] Preparing report for VMs on server " -ForegroundColor Blue
foreach ($vm in $vms) {
Write-Host "[*]`tProcessing"$v.Name -ForegroundColor Green
$v = Get-View $vm
$row = "" | Select VMname,VMUsedGB,VMProvisionedGB,HDname,HDformat,HDCapacity,PowerState,IPAddress,HostName,ToolStatus,CDStatus,ESX
$hd = Get-HardDisk $vm
$row.VMname = $v.Name
$row.VMUsedGB = $vm.UsedSpaceGB
$row.VMProvisionedGB = $vm.ProvisionedSpaceGB
$row.PowerState = $vm.PowerState
$row.IPAddress = [string]$vm.Guest.IPAddress
$row.HostName = $vm.Guest.HostName
$row.ToolStatus = $v.Guest.ToolsStatus
$row.ESX = $vm.vmhost
$cd = Get-CDDrive $vm | where {$_.ConnectionState.Connected -eq $true}
if ($cd)
{
$row.CDStatus = $cd.IsoPath
}
else
{
$row.CDStatus = "CD Disconnected"
}
$hds = Get-HardDisk $vm
foreach ($hd in $hds)
{
$row.HDname = $hd.Name
$row.HDformat = $hd.StorageFormat
$row.HDCapacity = $hd.CapacityKB
$report = $report + $row
}
}
Write-Host "[*] Finished" -ForegroundColor Blue
Write-Host "[*] Report saved to"$CSVFile -ForegroundColor Blue
$report | Export-csv $CSVFile -NoTypeInformation -UseCulture