Power Shell scripts for Virtual machines

List of each of your vms:

Get-VM | Where {$_.PowerState -eq "PoweredOn"} | 
Select Name, Host, NumCpu, MemoryMB, <code>
 @{N=&quot;Cpu.UsageMhz.Average&quot;;E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}}, </code>
 @{N=&quot;Mem.Usage.Average&quot;;E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}} `
 | Export-Csv c:\Temp\Temp.csv

VMs per Resource Pool:

Get-ResourcePool | Where {$_.Name -ne “Resources“ }| Select Name, @{N=“NumVM“;E={($_ | Get-VM).Count}} | Sort Name|Export-Csv -NoTypeInformation c:\Temp.csv

 

SVMotion via PowerCLI :

Get-VM “Test“ |Move-VM -datastore (Get-datastore “MyDatastore“)

 

The Move-VM Cmdlet covers a multiple of sins, lets check some out, you want VMotion:

Get-VM -Name “Test“ |Move-VM -Destination (Get-VMHost MyHost)

 

Moving a VM to a new folder:

Move-VM -VM (Get-VM -Name MyVM)-Destination (Get-Folder -Name Production)

 

Moving a VM to a new resource pool:

Move-VM -VM (Get-VM -Name MyVM)-Destination (Get-ResourcePool -Name “Important“)

VM guest Disk size:



ForEach ($VM in Get-VM ){($VM.Extensiondata.Guest.Disk | Select @{N="Name";E={$VM.Name}},DiskPath, @{N="Capacity(MB)";E={[math]::Round($_.Capacity/ 1MB)}}, @{N="Free Space(MB)";E={[math]::Round($_.FreeSpace / 1MB)}}, @{N="Free Space %";E={[math]::Round(((100* ($_.FreeSpace))/ ($_.Capacity)),0)}})}


PowerShell Scripts for Esxi

List all ESX Hosts and their Service console information:

Get-VMHost | Get-VMHostNetwork | Select Hostname, ConsoleGateway, DNSAddress -ExpandProperty ConsoleNic | Select Hostname, PortGroupName, IP, SubnetMask, ConsoleGateway, DNSAddress, Devicename

 

Processor types:

Get-VMHost | Sort Name | Get-View | Select Name, @{N=“Cluster“;E={Get-Cluster -VMHost (Get-VMHost $_.Name)}},@{N=“CPU“;E={$_.Hardware.CpuPkg[0].Description}} | Export-Csv c:\Test.csv

 

NTP:

Get-VMHost |Sort Name|Select Name, @{N=“NTPServer“;E={$_ |Get-VMHostNtpServer}}, @{N=“ServiceRunning“;E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq “ntpd“}).Running}}

 

Host Hardware :

Get-VMHost |Sort Name |Get-View |
Select Name, 
@{N=“Type“;E={$_.Hardware.SystemInfo.Vendor+ “ “ + $_.Hardware.SystemInfo.Model}},
@{N=“CPU“;E={“PROC:“ + $_.Hardware.CpuInfo.NumCpuPackages + “ CORES:“ + $_.Hardware.CpuInfo.NumCpuCores + “ MHZ: “ + [math]::round($_.Hardware.CpuInfo.Hz / 1000000, 0)}},
@{N=“MEM“;E={“” + [math]::round($_.Hardware.MemorySize / 1GB, 0) + “ GB“}} | Export-Csv c:\Test.csv

 

VMs per Resource Pool:

Get-ResourcePool | Where {$_.Name -ne “Resources“ }| Select Name, @{N=“NumVM“;E={($_ | Get-VM).Count}} | Sort Name|Export-Csv -NoTypeInformation c:\Test.csv

 

Check VM nic speed:

Connect-VIServer MYVISERVER
 
$VMhosts = Get-VMHost | Get-View
Foreach ($vmhost in $vmhosts){
 Write-Output $vmhost.Name
 $pnic = 0
 Do { 
 $Speed = $VMhost.Config.Network.Pnic[$pnic].LinkSpeed.SpeedMb 
 Write "Pnic$pnic $Speed" 
 $pnic ++
 } Until ($pnic -eq ($VMhost.Config.Network.Pnic.Length))}

 

PowerShell Scripts for Datastore

Number of VMs in Datastore:

Get-Datastore | Select Name, @{N="NumVM";E={@($_ | Get-VM).Count}} | Sort Name

The following script was used to add the datastore’s from a csv file :

$CSVFile = "C:\Temp\Test.csv" ## Where Test.csv is the csv file from where you want to copy from 
$MYESXHost = "Esxi.local" 
$VMHBA = "vmhba2:0:"

Connect-VIServer VCENTERSERVER ##vCenter Server 
$CSV = Import-CSV $CSVFile ## Importing CSV 
$ESXHost = Get-VMHost $MYESXHost 
$ESXHost |Get-VMHostStorage -RescanAllHBA 
Foreach ($Item in $CSV){ 
 $HostID = $Item.HostId 
 $LunID = $Item.LunID 
 Write "Creating SAN_HOST$($HostID)_LUN$($LunID)…" 
 $ESXHost |New-Datastore -Vmfs -Name "SAN_HOST$($HostID)_LUN$($LunID)" -Path "$($VMHBA)$($HostID)" 
}

List all the VMDK files for VMs stored on your local storage:

Get-Datastore |where {$_.Name -match “local“} |Get-VM |Get-HardDisk |select Filename |Export-Csv c:\Test.csv

You can also add multiple names for your local storage by added them with a | .

Get-Datastore |where {$_.Name -match “store|local|storage“} |Get-VM |Get-HardDisk |select Filename |Export-Csv c:\Test.csv