Refer to the list of Sample Preventive Maintenance Reports with PowerCLI
- Connect to vCenter with PowerCLI
#Import VMware Module
Import-Module VMware.VimAutomation.Core
$vCenter = "10.253.24.16"
$User = "[email protected]"
$Password = "P@ssw0rd"
Connect-VIServer -Server $vCenter -User $User -Password $Password -WarningAction SilentlyContinue
- To get hardware inventory for all VMware ESXi host
$VMHosts = Get-VMHost
Foreach ($VMHost in $VMHosts) {
Get-VMHost -Name $VMHost | Select Name,
Model,
@{N="S/N" ; E={(Get-VMHostHardware -VMHost $VMHost).SerialNumber}},
ProcessorType,
@{N="No of CPU" ; E={(Get-VMHostHardware -VMHost $VMHost).CpuCount}},
NumCPU,
@{N="Total RAM" ; E={[math]::Round((Get-VMHost -name $VMHost).MemoryTotalGB,0)}},
@{N="Network Cards" ; E={(Get-VMHostHardware -VMHost $VMHost).NICCount}},
@{N="FC HBA" ; E={(Get-VMHostHBA -VMHost $VMHost | ? Type -EQ "FibreChannel").count}},
Version,
Build,
@{N="Up Time (Days)" ; E= {New-Timespan -Start (Get-VMHost -Name $VMHost).ExtensionData.Summary.Runtime.BootTime -End (Get-Date) | Select -ExpandProperty Days}} | Export-Csv -Path C:\Temp\VMHostInventory.csv -NoTypeInformation -Append }
- To get the average resource utilization for Processor, RAM, Storage and Network for past 30 days with 24 hours interval for VMware Hosts
$VMHosts = Get-VMHost
Foreach ($VMHost in $VMHosts) {
Get-VMHost -Name $VMHost | Select Name, `
@{N="Up Time (Days)" ; E= {New-Timespan -Start (Get-VMHost -Name $VMHost).ExtensionData.Summary.Runtime.BootTime -End (Get-Date) | Select -ExpandProperty Days}}, `
CPUTotalMhz, `
@{N="CPU Usage (Average), Mhz" ; E={[Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalSecs 86400 | Measure-Object Value -Average).Average),2)}}, `
@{N="Memory Total (GB)";E={[math]::Round($_.MemoryTotalGB)}}, `
@{N="Memory Usage (Average)" ; E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalSecs 86400 | Measure-Object Value -Average).Average),2)}}, `
@{N="Network Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat net.usage.average -Start (Get-Date).AddDays(-30) -IntervalSecs 86400 | Measure-Object Value -Average).Average),2)}},`
@{N="Disk Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddDays(-30) -IntervalSecs 86400 | Measure-Object Value -Average).Average),2)}} `
| Export-Csv C:\Temp\Usage.csv -NoTypeInformation -Append }
- Get the Usage of Datastore
Get-Datastore | ? Type -like "VMFS" | Select Name,
@{N="Capacity(GB)" ; E={[math]::Round(($_.CapacityGB),2)}},
@{N="Free(GB)" ; E={[math]::Round(($_.FreeSpaceGB),2)}},
@{N="Usage %" ; E={[math]::Round((1-($_.FreeSpaceGB/$_.CapacityGB))*100)}},
@{N="No of VM";E={@($_ | Get-VM).Count}} | Export-Csv C:\Temp\DataStore.csv -NoTypeInformation
- To get the average resource utilization for Processor, RAM, Storage and Network for past 30 days with 24 hours interval for Virtual Machines (VMs)
Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name, VMHost, NumCpu, MemoryMB, `
@{N="CPU Usage (Average), Mhz" ; E={[Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalSecs 86400 | Measure-Object Value -Average).Average),2)}}, `
@{N="Memory Usage (Average), %" ; E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalSecs 86400 | Measure-Object Value -Average).Average),2)}} , `
@{N="Network Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat net.usage.average -Start (Get-Date).AddDays(-30) -IntervalSecs 86400 | Measure-Object Value -Average).Average),2)}} , `
@{N="Disk Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddDays(-30) -IntervalSecs 86400 | Measure-Object Value -Average).Average),2)}},
@{N="OS Installed" ; E={(Get-VMGuest -VM $_.name).OSFullName}} |
Export-Csv -Path C:\Temp\VM.csv -NoTypeInformation -Append
- List the usage of individual volumes / partitions for all VM
foreach ($VM in Get-VM) {
$vDisks = (Get-VM -name $VM).Guest.Disks
foreach ($vDisk in $vDisks) {
$VM | Select Name,
@{N="Capacity" ; E={[math]::Round(($vDisk.CapacityGB),2)}},
@{N="Free Space" ; E={[math]::Round(($vDisk.FreeSpaceGB),2)}},
@{N="Usage %" ; E={[math]::Round((1-($vDisk.FreeSpaceGB/$vDisk.CapacityGB))*100)}},
@{N="Path" ; E={($vDisk.Path)}} | Export-Csv -Path C:\Temp\VMDiskUsage.csv -NoTypeInformation -Append
}
}
- List VM with ISO file attached
##### List VM with ISO File Attached
Get-VM | ? { $_ | Get-CDDrive| ? { $_.ConnectionState.Connected -eq "true" -and $_.ISOPath -like "*.ISO*"} } |
Select Name, @{Name=".ISO Path";Expression={(Get-CDDrive $_).isopath }} | Export-Csv C:\Temp\ISOAttached.csv -NoTypeInformation
- List VM with active snapshot created
Get-VM | Get-Snapshot | Select Created, VM, Description,@{Name="Size (GB)";Expression={[math]::Round($_.SizeGB ,2)}} | Sort Created | export-csv C:\Temp\snapshot.csv -NoTypeInformation
- List VM with exceed 90% of Disk Usage
foreach ($VM in Get-VM) {
$vDisks = (Get-VM -name $VM).Guest.Disks
foreach ($vDisk in $vDisks) {
$VM | Select Name,
@{N="Capacity" ; E={[math]::Round(($vDisk.CapacityGB),2)}},
@{N="Free Space" ; E={[math]::Round(($vDisk.FreeSpaceGB),2)}},
@{N="Usage %" ; E={[math]::Round((1-($vDisk.FreeSpaceGB/$vDisk.CapacityGB))*100)}},
@{N="Path" ; E={($vDisk.Path)}} | ? "Usage %" -gt "90" | Export-Csv C:\Temp\VMwith90%DiskUsage.csv -NoTypeInformation -Append
}
}
- List VMs which are running on outdated VMtool
Foreach ($VM in (Get-VM)) {
$VM | Select Name,
@{N="VMtools" ; E={(Get-View $_.id).config.tools.ToolsVersion}},
@{N="Status" ; E={(Get-View $_.id).guest.ToolsVersionStatus}},
@{N="OS" ; E={($_ | Get-VMGuest).OSFullName}} | ? "Status" -NotLike "guestToolsCurrent" |
Export-Csv -Path C:\Temp\VMtool.csv -NoTypeInformation -Append
}
VMtools=2147483647 is refering to the open-vm-tools
- List of Zombie VMDK files
<br />#Initial Array
$Report = @()
$AllVMDK =@()
$ZombieVMDK = @()
#Get all Datastore for VMFS Usage
$DSs = Get-Datastore | ? Type -like "VMFS"
$SearchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
foreach ($DS in $DSs) {
$DSBrowser = Get-View -Id $DS.ExtensionData.browser
$RootPath = "[" + $DS.Name + "]"
$SearchResult = $DSBrowser.SearchDatastoreSubFolders($RootPath, $SearchSpec) | Sort-Object -Property {$_.FolderPath.Length}
foreach ($folder in $SearchResult) {
#list only file with .vmdk
foreach ($file in ($folder.file | ? {$_.path -like "*.vmdk" -and $_.path -notlike "*-flat.vmdk" -and $_.path -notlike "*-ctk.vmdk" })) {
#New object to store all VMDK files
$PSObject = New-Object PSObject -Property @{
folder = $folder.FolderPath
File = $file.path
}
$Report += $PSObject
}
}
}
#Join the Full File name
foreach ($item in $Report) {
$list_VMDK = $item.folder + $item.file
$AllVMDK += @($list_VMDK)
}
#List all the Active VMDK file used by vCenter
foreach ($VM in (Get-VM)) {
$VMDKFiles = (($VM | Get-HardDisk).Filename)
foreach ($VMDKFile in $VMDKFiles) {
$ActiveVMDK += $VMDKFile
}
}
#Search all the Active VMDK files to find the Zombie File
Foreach ($VMDK in $AllVMDK) {
if ($VMDK -notin $ActiveVMDK) {
#New object to store all VMDK files
$PSObject = New-Object PSObject -Property @{
Path = $VMDK.ToString()
Description = "Possibly a Zombie vmdk file!"
}
$ZombieVMDK += $PSObject
}
}
#EXport the Zombie VMDK File to CSV file
$ZombieVMDK | Select Path, Description | Export-Csv -Path C:\Temp\ZombiVM.csv -NoTypeInformation