List VMs according to memory and CPU usage

For internal billing purpose I needed a way list all Windows VMs for a given subsidiary and their CPU and memory configuration.

Connect-VIServer -Server vcenter.corp.lan

$var = get-vm -location Subsidiary1 | Where{ $_.Guest.OSFullName -like '*windows*' }  | select numcpu, memorygb | Group-Object numcpu,memorygb

function get-numOfVms
{
	param
	(
		[parameter(Mandatory = $true)]
		[pscustomobject]$VMs
	)

	$results = foreach ($row in $var)
	{
		$cpu, $mem = $row.Name -split ',', 2
		[pscustomobject]@{
			NumOfVMs = $row.Count
			NumOfCPUs   = $cpu
			MemoryGB = $mem.Trim()
		}
	}
	
	return $results
}
$total = get-numOfVms -VMs $var
$total | Export-Csv -Path totalvms.csv -NoTypeInformation

 

Example of totalvms.csv. It gives you a number of each specific CPU and memory configuration.

"NumOfVMs","NumOfCPUs","MemoryGB"
"1","2","8"
"12","1","4"
"4","4","8"
"2","4","4"
"9","1","8"
"5","4","12"
"22","4","32"
"6","4","16"
"2","1","12"
"1","4","24"
"1","1","16"
"1","4","6"
"1","1","6"
"1","24","32"
"1","4","25"
"3","2","16"
"1","8","6"
"1","1","3"

Migrate VMs between clusters with powecli

A short script to migrate VMs to a new cluster or host. Migrates one vm at a time to save network bandwith. After migration it upgrades Vmware tools to match current host version.

Takes a CSV file as input with VMs to migrate.

Param (
	[Parameter (Mandatory = $True)]
	$file
)
$vms = Import-Csv $file

foreach ($vm in $vms)
{
	Write-Host "Migrating VM" $vm.name
	Move-VM -VM $vm.name -Destination Cluster01
	Write-Host "Updating Vm-tools on " $vm.name
	Update-Tools -VM $vm.name -NoReboot
}

Fix VMware tools not running on Windows servers

I had some trouble with VMwaretools stopping on some machines and decided to automate restart och vmware tools. I wrote this script which takes a view parameter to view current status. If you start the script whitout the view parameter it will try to restart vmware tools.

fix-toolsnorunning.ps1

param
(
[parameter(Mandatory = $false)]
[switch]
$view
)

#Connect to Vcenter
$cred = Get-Credential
Connect-VIServer vcenter -Credential $cred | Out-Null

Write-Host -ForegroundColor Green "Collecting VMs"
$toolsvm=get-vm | where { $_.GuestId -like "*Windows*" -and $_.Extensiondata.Summary.Guest.ToolsStatus -like "toolsnotrunning" -and $_.Powerstate -eq "PoweredOn"}

#Stop script if no VMs reports tools not running
if ($toolsvm -eq $null)
{
Write-Host "Nothing to fix"
Disconnect-VIServer vcenter -Confirm:$false
exit
}

if ($view)
{
Write-Host "VMs whitout tools running"
foreach ($vm in $toolsvm)
{
Write-Host $vm
}
}
Else
{

foreach ($vm in $toolsvm)
{

if ($vm -match "^(?<Name>\S*)\s.*$") { $vm_name = $Matches.Name }
else { $vm_name = $vm }

$vm_hostname = $vm_name.name +".domain.local"

if (Test-Connection -Computername $vm_hostname -BufferSize 16 -Count 1 -Quiet)
{
Invoke-Command -Computername $vm_hostname -ScriptBlock { stop-Service -name vmvss; start-service vmvss } -Credential $cred
Write-Host "Service restarted on $vm_name"
}
Else
{
Write-Host -ForegroundColor Red "Unable to contact $vm_name"
}

}
}
#Disconnect from Vcenter.
Disconnect-VIServer vcenter -Confirm:$false