Category Archives: Scripts

Scripting in action

PowerCLI: Listing VMs from a bunch of vCenters

I need to grab all the VMs in a list of vCenters, but for each line of VM, I need to know which cluster and ESXi host they sit in. The original script was this;

Get-content .\vc.txt |
% {$vc=$_ ; connect-viserver $vc ; Get-VM |
Select Name, Vmhost, @{Name="Cluster";
Expression={ ((get-vmhost $_.vmhost).parent)}}, Folder,
@{Name="vCenter"; Expression= {$vc}} |
export-csv .\$vc.txt -NoTypeInformation ;
disconnect-viserver $vc –force –confirm:$false }

Even thought the code was straight forward with just a single get-VM execution, it was very slow. The reason was the Cluster field code block, which executes get-vmhost to get the cluster name for each VM, took time to execute.

@{Name="Cluster"; Expression={ ((get-vmhost $_.vmhost).parent)}}

A faster one-line calls each cluster first, then calls each VM in turn.

Get-content .\vc.txt |
% {$vc=$_ ; connect-viserver $vc ; Get-Cluster |
% { $clus=$; Get-VM -Location $Clus |
% { "$($`t$($`t$($_.folder)`t$($_.vmhost)`t$Clus`t$vc" |
out-file .\$vc.txt -append }} ;
disconnect-viserver $vc –force –confirm:$false }
Leave a comment

Posted by on May 19, 2017 in powershell, vmware


Tags: , , ,

PowerCLI: Getting VMHosts, PortGroups and switches info

I needed to extract a list of hosts in vCenter and with them, get all the portgroups and switch info. Unfortunately, there is no one one-liner script that can do this. Mainly because you have standard and distributed switches and their properties are different. In order to do this you need to separate one-liner, both feeding into the same output file.

The first script output the hosts against their VDSwitches. The script creates a tab-delimited text file that you can easy copy and paste onto a spreadsheet. Both scripts output the following:
– Clustername
– Hostname
– virtual network adapter name
– IP
– subnet mask
– port group name
– switch name

Get-VMHostNetworkAdapter -vmkernel | 
% { $vnet=$_ ; get-vdportgroup -vmhostnetworkadapter $vnet | 
$($`t$($_.vdswitch)`t$($_.vlanconfiguration)" | 
out-file .\temp\output.txt -append } }

The second script output the hosts against their standard switch, as you can see the order of the function is very different. The main reason is that VMHostNetworkAdapter object cannot be pipped into the VirtualPortGroup but it works the other way around.

You can also see from the second script the the names for virtualswitch and vlanID different from the above. Actually .vdswitch and .vlanconfiguration are objects themselves, but they are nice enough to output the values I want without needing to specify the actual object, i.e. switch name and VLAN ID.

Get-VirtualPortgroup -standard | 
% { $pg=$_ ; get-vmhostnetworkadapter -portgroup $pg | 
% { "$($_.vmhost.parent)`t$($_.vmhost)`t$($`t
$($`t$($pg.vlanid)" |
 out-file .\temp\output.txt -append }}
Leave a comment

Posted by on March 31, 2017 in powershell, vmware


Tags: , , , ,

PS1: Transposing a vertical list of records

Honestly, I don’t really know what term to call this other than “a vertical list of records”. What I mean a set of records that are listed vertically instead of having its field values in columns. Below table is what it looks like:



This output comes out from a really old CLI which extracts the inventory of hosts. The problem with the CLI is that it can only extract one host at a time and it displays in this vertical list format. To extract 100 host, you need to call the CLI 100 times and dump the output into a file, which is what it ended up looking like in the above. Read the rest of this entry »

Leave a comment

Posted by on October 14, 2016 in powershell



PowerCLI VUM 5.5: Get-baseline stackoverflow error

Update: PowerCLI VUM 6.0 seemed to have solved this problem

I ran in the following error when trying to get baseline via PowerCLI VUM

get-baseline -entity "clustername"

Powershell will then crash with stackoverflow error

A bit of search doesn’t reveal any solution or fixed to it, but one post seems to solve the problem for me, even though its talking about Windows 7/8 and I am running powershell on a Windows 2008R2 server. Apparently, this behaviour has been on-going since VUM 5.1.

The solution is to run powershell in version 2 compatible mode. However, if you try to amend the PowerCLI shortcut, you will find that it already has maxed out the number of character allowed and you cannot add anymore parameters. The solution is simply to create a CMD file, copy and paste the PowerCLI launch commands and add “-version 2” to the parameters. Remember to add “-version 2” immediately after the EXE and not at the end of the rest of the parameters.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -version 2 -psc "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -c ". \"C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\""

Here is my powershell environment and after running in version 2

I have PowerCLI 5.8 installed with VUM 5.5 U2 PowerCLI.:

  • VMware-UpdateManager-Pscli-5.5.0-1302474.exe
  • VMware-PowerCLI-5.8.0-2057893
Get-PSSnapin | ft
Name                           PSVersion            Description
----                           ---------            -----------
Microsoft.PowerShell.Diagno... 3.0                  This Windows PowerShell sna...
Microsoft.PowerShell.Core      3.0                  This Windows PowerShell sna...
Microsoft.PowerShell.Utility   3.0                  This Windows PowerShell Sna...
Microsoft.PowerShell.Host      3.0                  This Windows PowerShell sna...
Microsoft.PowerShell.Manage... 3.0                  This Windows PowerShell Sna...
Microsoft.PowerShell.Security  3.0                  This Windows PowerShell Sna...
Microsoft.WSMan.Management     3.0                  This Windows PowerShell sna...
VMware.VimAutomation.Core      2.0                  This Windows PowerShell sna...
VMware.VimAutomation.Vds       2.0                  This Windows PowerShell sna...
VMware.VumAutomation           2.0                  This Windows PowerShell sna...
VMware.VimAutomation.License   2.0                  This Windows Powershell sna...
VMware.DeployAutomation        2.0                  Cmdlets for Rule-Based-Depl...
VMware.ImageBuilder            2.0                  This Windows PowerShell sna...
VMware.VimAutomation.Storage   2.0                  This Windows PowerShell sna...

$psversiontable (After running in version 2)

Name                           Value
----                           -----
CLRVersion                     2.0.50727.5485
BuildVersion                   6.1.7601.17514
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
PSRemotingProtocolVersion      2.1

$psversiontable (Before running in version 2)

Name                           Value
----                           -----
WSManStackVersion              3.0
PSCompatibleVersions           {1.0, 2.0, 3.0}
BuildVersion                   6.2.9200.16398
PSVersion                      3.0
CLRVersion                     4.0.30319.1026
PSRemotingProtocolVersion      2.2

Leave a comment

Posted by on September 24, 2015 in powershell, Scripts, vmware


Tags: ,

Powershell and splatting: passing switch parameter to another script

I came across the term splatting previously, but never really paid heed to it until recently when I needed to pass a switch parameter as a variable to another script, like this:

if ($SkipHost) {$s = "-SkipHost"} else {$SkipHost = ""}
.\script1.ps1 -Name $name $s
.\script2.ps1 -Name $name -Server $server

The line at script1.ps1 obviously don’t work and will cause an error when executed.

Some folks may ask: “why do I need to do this?” Well, splatting is most useful when you have a main script which calls other external scripts. This is where splatting comes in, its the ability to pass parameters into a script using variables. I don’t need to explain the mechanics about it as there are many posts elsewhere devoted to this already. Suffice to say, despite all the fancy ways in which various authors had devoted their time to describe the use of @PSBoundParameter, they are only useful if you are passing ALL the parameters at one go. If one need to pass only one or two of the parameters, @PSBoundParameter probably makes the script more complex.

One way to make this work without @PSBoundParameter is via the Powershell version 1 method:

.\script1.ps1 -Name $name -SkipHost:$SkipHost
.\script2.ps1 -Name $name -Server $server
Leave a comment

Posted by on September 21, 2015 in powershell, Scripts



PS1: Mashing UCS host to MAC address via vlookup

This post is more a note to self, but is useful for folks using Cisco UCS enclosure when you need to general a list of hosts against its MAC address on eth0 NIC.

Cisco UCS is notoriously flexible yet complex. If you want a list of physical host names against its MAC address, there is no direct means of exporting that list. You need to get the source from two exports, one from the server export and one from the MAC Identity Management tab in the LAN tab. Then you need perform a vlookup to match each host to its MAC address using “Assigned To” field in th MAC export and “Name” field in the server export. Read the rest of this entry »

Leave a comment

Posted by on September 21, 2015 in powershell, Scripts


Tags: ,

PowerShell: Setting JVM heap sizes for vCenter server 5.x

One of the changes from vCenter 5.0 to 5.1 that affect java memory allocation is vFabric tomcat server. This server effective decouples the java heap size memory management to the various vCenter server components, when this used to be a single setting in the tomcat configuration. An effect of this is that you many need to adjust the JVM heap size of your vCenter after upgrading (or even fresh install), even though you have specific the correct sizing during install.

From my experience, since we had upgraded vCenter from 5.0 to 5.5, the JVM heap size may not be sized correctly after the upgrade. Especially the Inventory servces, which can impact performance is set too low. Read the rest of this entry »

Leave a comment

Posted by on July 10, 2015 in powershell, Scripts, vmware


Tags: , ,