Quick and dirty PowerCLI cmdlets


I am preparing for my VCAP-DCA exam and having to automate more and more of my daily tasks within our VMware environment at work — as a result, I am using PowerCLI constantly. As I result, I thought I would share a couple quick and dirty little scripts that I have had to use lately.

Mileage will vary, but they’ve proven useful for me at work (some variables values have been edited).

Changing Network Label

> $i = 1
> $VLAN = "VLAN1012"
> while ($i -le 20) {
> $VMName = "prefix" + $i + ".site"
> Get-VM -Name $VMName | Get-NetworkAdapter | Set-NetworkAdapter -Portgroup $VLAN -Confirm:$false | Out-Null
> $i++
> }
>

Deleting and Adding a New Disk

> $i = 1
> while ($i -le 20) {
> $VMName = "prefix" + $i + ".site"
> Get-HardDisk -VM $VMName | Remove-HardDisk -Confirm:$false | Out-Null
> New-HardDisk -VM $VMName -CapacityGB 525 -Datastore "DRSCluster1" -Confirm:$false | Out-Null
> $i++
> }
>

Changing Allocated RAM

> $i = 1
> while ($i -le 20) {
> $VMName = "prefix" + $i + ".site"
> Set-VM -VM $VMName -MemoryGB 32 -Confirm:$false | Out-Null
> $i++
> }
>