Author Archives: Alan

About Alan

Alan Renouf has a role of Automation Frameworks Product Manager at VMware responsible for providing the architects and operators of the cloud infrastructure with the toolkits/frameworks and command-line interfaces they require to build a fully automated software-defined datacenter. Alan is a frequent blogger at http://blogs.vmware.com/vipowershell and has a personal blog at https://virtu-al.net. You can follow Alan on twitter as @alanrenouf.

SSH PowerShell tricks with plink.exe

image

Recently I needed to perform some actions in PowerCLI from the ESXi Shell, as you may know there are currently no cmdlets from VMware to allow you to run shell commands but one option which is popular within the communities is using a 3rd party tool called plink.exe to run the commands via SSH.

Examples of this can be seen here and here.

Download plink.exe via PowerShell

The script I was writing was for someone who I knew didn’t already have plink installed and I wanted them to have little to no effort when using this script so the first trick I wanted to share was the ability to check for plink.exe and download it if it wasn’t in the same directory as the script, this can be seen below:

$myDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$PlinkLocation = $myDir + "\Plink.exe"
If (-not (Test-Path $PlinkLocation)){
	Write-Host "Plink.exe not found, trying to download..."
	$WC = new-object net.webclient
	$WC.DownloadFile("http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe",$PlinkLocation)
	If (-not (Test-Path $PlinkLocation)){
		Write-Host "Unable to download plink.exe, please download from the following URL and add it to the same folder as this script: http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe"
		Exit
	} Else {
		$PlinkEXE = Get-ChildItem $PlinkLocation
		If ($PlinkEXE.Length -gt 0) {
			Write-Host "Plink.exe downloaded, continuing script"
		} Else {
			Write-Host "Unable to download plink.exe, please download from the following URL and add it to the same folder as this script: http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe"
			Exit
		}
	}	
}

Accept plink.exe host key automatically

Secondly when connecting to a host for the first time you will need to accept the host key and allow plink.exe to connect to the host, the message is similar to the one below:

image

At the moment there is no option in plink.exe to skip the host key checking, however after some searching and messing around I came upon a solution.

It turns out you can actually send a Y to the script you are running by simply piping “echo Y” to plink.exe, this sends a Y accepting the host key when the question is asked from the command line, this can be scripted like the below example:

Echo Y | Plink.exe…..

Hopefully someone will find these useful in the future as I did with this script.

Listing remote Date and Time with PowerShell

Recently I needed to find the remote time of multiple windows servers on the network and compare these, I wrote a quick function that uses WMI to pull this information and return it in a DateTime object format:

image

The Code

function Get-Time {
	<#
		.SYNOPSIS
			Gets the time of a windows server

		.DESCRIPTION
			Uses WMI to get the time of a remote server

		.PARAMETER  ServerName
			The Server to get the date and time from

		.EXAMPLE
			PS C:\> Get-Time localhost

		.EXAMPLE
			PS C:\> Get-Time server01.domain.local -Credential (Get-Credential)

	#>
	[CmdletBinding()]
	param(
		[Parameter(Position=0, Mandatory=$true)]
		[ValidateNotNullOrEmpty()]
		[System.String]
		$ServerName,

		$Credential

	)
	try {
			If ($Credential) {
				$DT = Get-WmiObject -Class Win32_LocalTime -ComputerName $servername -Credential $Credential
			} Else {
				$DT = Get-WmiObject -Class Win32_LocalTime -ComputerName $servername
			}
	}
	catch {
		throw
	}

	$Times = New-Object PSObject -Property @{
		ServerName = $DT.__Server
		DateTime = (Get-Date -Day $DT.Day -Month $DT.Month -Year $DT.Year -Minute $DT.Minute -Hour $DT.Hour -Second $DT.Second)
	}
	$Times

}

#Example of using this function
$Servers = "localhost", "dc01.domain.local"

$Servers | Foreach {
	Get-Time $_
}

Checking for time skew

We can also use this function to easily check for time skew between two machines, the below code is an example where I check my time between a remote host and the local server to see if it is within 30 seconds…

$RemoteServerTime = Get-Time -ServerName "dc01.domain.local"
$LocalServerTime = Get-Time -ServerName "localhost"

$Skew = $LocalServerTime.DateTime - $RemoteServerTime.DateTime

# Check if the time is over 30 seconds
If (($Skew.TotalSeconds -gt 30) -or ($Skew.TotalSeconds -lt -30)){
	Write-Host "Time is not within 30 seconds"
} Else {
	Write-Host "Time checked ok"
}

Learning PowerCLI–What does it take ?

One of the most asked questions to me at VMworld this year was “what does it take to learn PowerCLI”, lots of people have seen the fantastic things you can do with PowerCLI, where it is drawing your infrastructure out into Visio or checking your VMware environment for potential issues.

I always tell people that if I can learn PowerCLI then anyone can, one of the great strengths of PowerCLI and PowerShell is that it was written for System Administrators, for the people who need to get the job done, get it done fast and move on to the next thing.

With that in mind it is not only easy to understand what something like New-VM does but also very easy to learn as all the information you need is often very easy to read, easy to work out what’s going on and also right there in the PowerCLI console.

One of my favorite learning resources is the help built in to PowerCLI.

From within the console you can easily access not only the full help for each cmdlet but also examples of how you might use this cmdlet, these are great because they not only show examples of how to use the cmdlets but also cover some of the top use cases for that cmdlet.  A great deal of thought goes into the help files and these examples to make them relevant and useful.

To access the help and the examples for a cmdlet there are multiple ways, firstly you can see the help as part of the online documentation for PowerCLI here, secondly you can run the Get-Help cmdlet and also use the –examples parameter to gain more information, see below for an example:

PowerCLI Example

I still don’t understand what it takes to learn PowerCLI ?!

If you still don’t get it and need a full on action video with cool music and stunts to get this simple feature then check out the below:

What does it take to learn PowerCLI ? from Alan Renouf on Vimeo.

Updated VMware Knowledge Portal iPad App

Following my recent post showing the VMware Knowledge Portal iPad App, I just wanted to add a quick note to say firstly, If you do not have this then download it straight away.  The information on here is very useful and includes videos as well as documentation for the vCloud Suite of products.

Secondly, the app was updated at the start of this week to include yet more information, as you can see from the below screenshot. So head over to the App store and download this FREE app now and start learning about the new 5.1 features.

Click here to download the app.

vMKP

Using PowerShell v3.0 CIM cmdlets with VMware ESXi Hosts

I noticed that in PowerShell V3.0 some CIM cmdlets were introduced which allowed PowerShell to be able to interact with CIM providers and gather information, when importing the CIM Cmdlets into my session you can see we have a number of new cmdlets to work with:

image

Even though I know PowerCLI 5.1 R1 (Current release on 29th Oct 2012) does not support PowerShell v3.0 I thought I would check out what I could do with these and what information I could receive using the CIM cmdlets.

As a reminder, Carter Shanklin did some work in this area a long time ago and he ended up writing a function to be able to pull CIM information from ESX hosts, this can be found here, he also includes some links to documentation on the CIM providers the VMware hosts can provide: http://blogs.vmware.com/vipowershell/2009/03/monitoring-esx-hardware-with-powershell.html

As you will see from the video below the new cmdlets cut out most of the hard work Carter had to do and allow us to pull CIM information from the ESXi hosts with ease.

Using PowerShell v3.0 CIM Cmdlets with VMware ESXi Hosts from Alan Renouf on Vimeo.

For your reference, here is the code I used during this demo:

import-module CimCmdlets
$ipaddress = "10.20.177.15"
$HostUsername = "root"
$CIOpt = New-CimSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck -Encoding Utf8 -UseSsl
$Session = New-CimSession -Authentication Basic -Credential $HostUsername -ComputerName $Ipaddress -port 443 -SessionOption $CIOpt
Get-CimInstance -CimSession $Session -ClassName CIM_Fan

VMware Mobile Knowledge Portal–iPad App

Do you find it hard to locate the latest VMware documentation ? Wouldn’t it be great if there was an iPad where the documentation and videos could be synchronized to your iPad enabling you to take it with you and learn on the train or wherever you need it.  Want no more !

The VMware Mobile Knowledge Portal is now available and ready to download from the app store here: http://itunes.apple.com/us/app/vmware-mobile-knowledge-portal/id566387182

Once launched you can easily find the information relevant to you:

IMG_0203

You can select the information you need to be synchronized to your device or watch it online when you have an internet connection:

IMG_0204

Take the documentation with you:

IMG_0206

Watch the technical videos and learn about the new 5.1 features:

IMG_0205

vMotion and SvMotion Details with PowerCLI

Today I was asked if there was a way to list the vMotions and SvMotions which had occurred in an infrastructure, not only this but they needed to know which hosts the VMs had moved to, the reason for this was licensing.

Firstly they needed to confirm that certain VMs were firstly setup with DRSAffinityrules as disabled, this one was straight forward in PowerCLI:

Get-VM | Select Name, DRSAutomationLevel

image

Secondly they wanted a list of the vMotions and SvMotions which had taken place over the last week and specifically the source and destination hosts, a post on the VMware PowerCLI Blog gives us most of this information and was easy to adjust to include the source and destination hosts…

image

The adjusted script can be found below:

Function Get-MotionDuration {
    $events = Get-VIEvent -Start (Get-Date).AddDays(-7)
    $relocates = $events |
        where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.migrate" -or $_.Info.DescriptionId -eq "VirtualMachine.relocate"}
    foreach($task in $relocates){
        $tEvents = $events | where {$_.ChainId -eq $task.ChainId} |
            Sort-Object -Property CreatedTime
        if($tEvents.Count){
            New-Object PSObject -Property @{
                Name = $tEvents[0].Vm.Name
                Type = &{if($tEvents[0].Host.Name -eq $tEvents[-1].Host.Name){"svMotion"}else{"vMotion"}}
                StartTime = $tEvents[0].CreatedTime
                EndTime = $tEvents[-1].CreatedTime
                Duration = New-TimeSpan -Start $tEvents[0].CreatedTime -End $tEvents[-1].CreatedTime
				SourceHost = $tEvents[0].Host.Name
				DestinationHost = $tEvents[-1].Host.Name
            }
        }
    }
}


Connect-VIServer MyViServer –User Administrator –Password “Pa$$w0rd”

Get-MotionDuration | FT -AutoSize

vCloud Suite 5.1– SSO Resources

vCenter Single Sign On (SSO) is a new feature in vSphere 5.1, as with any new feature people need to know where it fits in the stack, why its there and how its going to make things easier?

VMware have done a great job in giving lots of information on this feature, firstly the overview videos are a great place to start, see how SSO fits into the vCloud Suite and what advantages it offers you and how to easily manage it below, following that there are some great blog posts and documentation links I have added to this post for your reference.

SSO Videos

SSO Documentation

Install vCenter Single Sign On as Part of a vCenter Server Simple Install

Install vCenter Single Sign On as a New Installation

Install vCenter Single Sign On, vCenter Inventory Service, and vCenter Server Separately

SSO Frequently asked questions

List of great Knowledge Base articles from Justin King

vCloud Suite 5.1–Whats New?

Are you still trying to catch up with what’s new in the 5.1 release of VMware ? perhaps a quick overview is what you need, from then on you can look into the different enhancements based on your interest focus.

I have found two great resources for people wanting to get a high level overview of 5.1 and what’s new.

Firstly, a great video shot by VMware here:

Secondly a pdf giving you an overview of the new release details which can be found here: http://www.vmware.com/files/pdf/products/vsphere/vmware-what-is-new-vsphere51.pdf

image

Quick Host Profile Reports

Today I needed to grab some quick information on my hosts, which hosts had host profiles enabled, if they were compliant and if not what where the issues ?

With the below script I was easily able to grab this information, as can be seen in the screenshot below:

image

The Script

$HPDetails = @()
Foreach ($VMHost in Get-VMHost) {
	$HostProfile = $VMHost | Get-VMHostProfile
	if ($VMHost | Get-VMHostProfile) {
		$HP = $VMHost | Test-VMHostProfileCompliance
		If ($HP.ExtensionData.ComplianceStatus -eq "nonCompliant") {
			Foreach ($issue in ($HP.IncomplianceElementList)) {
				$Details = "" | Select VMHost, Compliance, HostProfile, IncomplianceDescription
				$Details.VMHost = $VMHost.Name
				$Details.Compliance = $HP.ExtensionData.ComplianceStatus
				$Details.HostProfile = $HP.VMHostProfile
				$Details.IncomplianceDescription = $Issue.Description
				$HPDetails += $Details
			}
		} Else {
			$Details = "" | Select VMHost, Compliance, HostProfile, IncomplianceDescription
			$Details.VMHost = $VMHost.Name
			$Details.Compliance = "Compliant"
			$Details.HostProfile = $HostProfile.Name
			$Details.IncomplianceDescription = ""
			$HPDetails += $Details
		}
	} Else {
		$Details = "" | Select VMHost, Compliance, HostProfile, IncomplianceDescription
		$Details.VMHost = $VMHost.Name
		$Details.Compliance = "No profile attached"
		$Details.HostProfile = ""
		$Details.IncomplianceDescription = ""
		$HPDetails += $Details
	}
}
$HPDetails