RSS

Category Archives: powershell

PowerCLI: Script to reboot each ESXi hosts

I realized that having done this for a few year, I never really nailed down a script to do this properly. It is very common as a vSphere admin to have to reboot your ESXi hosts after a configuration change or for patching.

Below is a first draft, not pretty code-wise but its a working operational script. I hope to improve on it over time. The basic flow of the script is as follows:

  • You establish a connection to the vCenter server first before running the script
  • You submit an input file which is list of ESXi host names
  • The script reads the list and does the following for each host
    • Sets the host in maintenance mode and counts down to 30 minutes. If the hosts does not get into maintenance mode by then the script terminates and you need to figure out why.
    • If the host goes into maintenance mode, it then force reboots the hosts and waits for 30 minutes again. Again if the host doesn’t come up by 30 minutes, the script terminates and you need to fix the host issue.
    • Lastly, once the host is back online, it sets the host to connected state and works on the next host in the loop.

Read the rest of this entry »

Advertisements
 
Leave a comment

Posted by on November 5, 2017 in powershell, vmware

 

Tags: , ,

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=$_.name; Get-VM -Location $Clus |
% { "$($_.name)`t$($_.id)`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
– VLAN ID

Get-VMHostNetworkAdapter -vmkernel | 
% { $vnet=$_ ; get-vdportgroup -vmhostnetworkadapter $vnet | 
%{"$($vnet.vmhost.parent)`t$($vnet.vmhost)`t
$($vnet.name)`t$($vnet.ip)`t$($vnet.subnetmask)`t
$($_.name)`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$($_.name)`t
$($_.ip)`t$($_.subnetmask)`t$($pg.name)`t
$($pg.virtualswitch.name)`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:

host1:FIELD1=value1
host1:FIELD2=value2
host1:FIELD4=value3
host1:FIELD5=value4

host2:FIELD1=value1
host2:FIELD2=value2
host2:FIELD3=value3
host2:FIELD4=value4

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

 

Tags:

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.

http://www.virten.net/2012/10/stackoverflowexception-with-update-manager-powercli-and-wmf-v3/

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}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1


$psversiontable (Before running in version 2)

Name                           Value
----                           -----
WSManStackVersion              3.0
PSCompatibleVersions           {1.0, 2.0, 3.0}
SerializationVersion           1.1.0.1
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:

param(
[string]$Name,
[string]$Server,
[switch]$SkipHost
)
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:

param(
[string]$Name,
[string]$Server,
[switch]$SkipHost
)
.\script1.ps1 -Name $name -SkipHost:$SkipHost
.\script2.ps1 -Name $name -Server $server
 
Leave a comment

Posted by on September 21, 2015 in powershell, Scripts

 

Tags:

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: ,