PowerCLI oneliner to generate version, build and bios details

Here’s a quick oneliner to generate a nice report about the version, build and bios details on each host.

Get-VMHost | Select-Object -Property Name,Version,Build,@{Name='BIOS Version';Expression={(($_|Select-Object -ExpandProperty ExtensionData).Runtime.HealthSystemRuntime.SystemHealthInfo.NumericSensorInfo | ?{$_.Name -like "*bios*"}).Name.Split(' ')[4]}}

PS: there are no spaces or new lines in the code.

Enjoy!

Advertisements

vSphere ESXi Maintenance Mode with PowerCLI

If you need to put all hosts connected to your vCenter server in maintenance mode here is how you can do it:

Get-VMHost | Set-VMHost -State Maintenance

And to exit the maintenance mode on all hosts use the following command:

Get-VMHost | Set-VMHost -State Connected

Now that is not very useful, since you won’t put all your hosts in maintenance mode very often. That’s where we could make use of glob expansion. Here is an example. Lets say we have hosts from esx1 to esx10, and of those I want to put  esx5, esx6 & esx7 in maintenance mode, this is how you can do it:

Get-VMHost -Name  [esx[5-7]* | Set-VMHost -State Maintenance

The above command will get all hosts which start with either esx5,esx6 or esx7.  Now we could have used the Select-Object, but that would be one more cmdlet to type :-).

Similarly you could exit maintenance mode on selective hosts as well:

Get-VMHost -Name  [esx[57]*.pune.fooibirds.in | Set-VMHost -State Connected

The above command would exit maintenance mode on hosts esx5.pune.foobirds.in and esx7.pune.foobirds.in. Pretty nifty ehh!

For more info on wildcards characters (globs) read it here.

Using PowerCLI to start and stop services on your ESXi host

Yes if you PowerCLI you really don’t need to connect to your ESXi host using SSH. But there is always a situation once in a while when you may want to troubleshoot things by logging to your ESXi host over a SSH connection. Following function tries to enable a specific service using PowerCLI. Again a useful trick if you a person like me who looks the command line. How to use it? Save it to your $Profile.

Of course to be able to use the function you will need to be connected to your vCenter or ESXi host.

### ---------- function: Set-VMHostServiceStatus ---------- ###
function Set-VMHostServiceStatus() {
<#
.SYNOPSIS
This script allows you to start/stop a service on vSphere ESXi 4.1 and above.

.DESCRIPTION
A service specified will only be 'started' if is 'stopped' and 'stopped' only if it is already started.
No action will be taken if the defined service name does not exist on a ESXi host.

Action can be either 'start' or 'stop'.

.EXAMPLE
$VMHost=Get-VMHost esx3.foobirds.in
Set-VMHostServiceStatus.ps1 -VMHost $VMHost -ServiceName 'TSM-SSH' -Action 'start'

.EXAMPLE
$VMHost=Get-VMHost esx3.foobirds.in
Set-VMHostServiceStatus.ps1 -VMHost $VMHost -ServiceName 'TSM' -Action 'stop'

.EXAMPLE
Get-VMHost esx3.foobirds.in | Set-VMHostServiceStatus.ps1 -ServiceName 'TSM' -Action 'stop'

.NOTES
Supports all services that can be modified from the vSphere Client.

.NOTES
The ServiceName is case-sensitive. You get the service status using Get-VMHostService cmdlet.
#>
# get commandline parameters
[CmdletBinding()]
param (
[parameter(Mandatory=$true,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)]
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$VMHost,

[string]$ServiceName = $(Read-Host -prompt "Enter the ServiceName"),

[ValidateSet("Stop","Start")][string]$Action = $(Read-Host -prompt "Enter an Action")
)
Process {
$svc = (Get-VMHost $VMHost | Get-VMHostService | Where-Object {$_.Key -eq $ServiceName})
$CmdletName = $Action + '-VMHostService'

if ((($CmdletName -imatch "stop") -and ($svc.Running -imatch "True")) -or
(($CmdletName -imatch "start") -and ($svc.Running -imatch "False"))){
&$CmdletName -HostService $svc -Confirm:$false
}
else {
Write-Host "Info: Status of Service [$ServiceName] was not changed!"
}
}
}

### ---------- end ---------- ###

How to use it:

Get-Help Set-VMHostServiceStatus -Examples

Example1: Start a specific service on a specifc ESXi Host

$VMHost=Get-VMHost esx3.foobirds.in
Set-VMHostServiceStatus.ps1 -VMHost $VMHost -ServiceName 'TSM-SSH' -Action 'start'

Example2: Stop a specific service on a specifc ESXi Host

$VMHost=Get-VMHost esx3.foobirds.in
Set-VMHostServiceStatus.ps1 -VMHost $VMHost -ServiceName 'TSM' -Action 'stop'

Example3: Start a specific service on all connected ESXi Hosts

Get-VMHost | Set-VMHostServiceStatus.ps1 -ServiceName 'TSM' -Action 'stop'

Using PowerCLI to connect to the Admin console using RDP

Here is a easy trick to connect to your Windows VM, using RDP. Essentially we are using the Get-VMGuest cmdlet to fetch VM’s IP Address. And then using the RDP client to connect to the VM’s admin console in full screen mode.

function Start-VMRdp() {
param (
[string]$Name = $(Read-Host -prompt "Enter the VM Name"),
[string]$Network = $(Read-Host -prompt "Enter the Network")

)
$IP=((Get-VMGuest $VM).IPAddress|Where-Object {$_ -match $Network}); mstsc /v:$IP /admin /f
}

Now I know you can always use the vSphere client and look up the IP and connect to it using the RDP client. However what if you have a multihomed VM, you need find the IP address and remember it. This trivial function helps to easy that pain.

The function is very easy to use, just connect to your vCenter Server. And call the function as mentioned below. I have saved this function to my $profile so it gets loaded everytime I start PowerShell.

Start-VMRdp -Name win7-vm1 -Network '192.168.141.*'

BTW, for this to work you need to have VMware tools installed on your VM.