Tag Archives: Powercli

Using the VMware Horizon API from c#

horizoniconSo here’s an ask I’ve had for well over a year, which i duly neglected until the mind of the brilliant Sean Massey decided to send me a PM on the vExpert EUC slack channel.

VMware Horizon’s API has been published for well over a year at this point over on code.vmware.com but there’s two challenges with this API in my humble opinion:

A: it’s WAAAAY too developer orientated for a regular PowerShell consumer*

B: while it’s a fully fledged API it seems a bit shortsighted to only document how to use it from Powershell given that the full API is documented.

* oh don’t have such a high opinion of yourself, everyone complained, i have the emails to prove it!

I had attempted this a few times before, but my usual source of help, Remko was too busy to help me or I duly hit a problem and inevitably toddled off to do something else. Not this time! and with no Remko help! *pats self on the back*

Continue reading

Unique portgroup fiddling with #Powercli.

As my peers seem to like to flout convention, particularly naming conventions. I was left with a strange automation challenge this week.

with a new service coming online in my company, I had 12 vlan portgroups to add to over 30 ESXi machines. Simple you say, just pipe your hosts into a new virtualswitch query then into a new virtual port group command.

Did i mention the virtual switch has a different name per host? Its vswitch0 on some, vswitch1 on others, and even vswitch2 on one.Arse.

after a few hours of various queries, below is the little scripty I was left with. I did consider putting this into a function, but I didnt know where to begin when it came to a name…

add-PortGroupInSwitchWhereOtherPortGroupExists? … bleh.

Below the variables are pretty self explanatory and it works flawlessly. Remember to install the powercli commands, import the module and connect to the vi server first!


foreach ($portgroup in get-virtualportgroup | where {$_.name -eq $findportgroupname}){
 get-vmhost -id $portgroup.vmhostid |
  get-virtualswitch -name $portgroup.virtualswitchname |
   new-virtualportgroup -name $NewPortGroupName -Vlanid $NewPortGroupVlanId

VMWare Powercli one liners & simple functions:

I attended the VCP 410 training course last week (install, configure & manage). I found this training very interesting but the real thief of my attention was just how good the vmware vSphere Powercli modules and cmdlets were. Below are a few one-liners I wrote as the week went on out of boredom and curiosity.

Hopefully I soaked in enough of the course through partial listening while reading the powercli help references to pass my exam today!

Retrieve and Apply DRS recommendations:

get-drsrecommendation | Apply-DrsRecommendation

Find all vm’s with e1000 network cards, for upgrade to vmxnet:

get-vm | Get-NetworkAdapter | where {$_.type -match “e1000″} | select-object parent,networkname,name,type

power off all vm’s with “test” in their names:

get-vm test* | stop-vm -confirm:$false

Move all vm’s with “test” in their names:

get-vm | where {$_.name -like “*test”} | move-vm -destination destinationhostname

Remove all vm’s with “test” in their name:

get-vm test* | remove-vm -confirm:$false

retrieve a list of vm’s with thinly provisioned disks:

get-vm | select HardDisks -expandproperty HardDisks | where {$_.storageformat -match “Thin”} | select-object parent,name,capacitykb,filename

A function to report on physical resources vs virtual commitment:

function get-commitment {

 get-vmhost | % {


 get-vm -location $_ | % {
 $intvmcommitcpu += $_.numcpu
 $intvmcommitmemmb += $_.MemoryMB
 }#end vm

 $objOutput = New-Object PSObject -Property @{
 VMHost = $_.name
 VMMaxCpu = $_.NumCpu
 VMCommitCPU = $intvmcommitcpu
 VMMaxMem = $_.MemoryTotalMB
 VMCommitMem = $intvmcommitmemmb
 }#end object
 $objreport += $objoutput 

 return $objreport


A function to report all machines with snapshots:

function get-snapshots {
 $objAllservers = get-vm
 foreach ($server in $objAllservers){
 $intsize = $intsize + $objallservers.length
 Write-Progress -activity "Gathering snapshot report ($server)" -status "Percent complete: " -PercentComplete (($i / $objAllservers.length) * 100)
 $result += get-snapshot $server}

 return $result

get-snapshots | select-object name,created,sizemb,iscurrent,vm,vmid,Id,Uid

pass the vcp exam: (work in progress)

get-coursenotes |
select-object salespitch,minimums,maximums,configurationlimitations |
commit-tobrain | out-exampaper