Tag Archives: PowerShell

Using Show-Command with PowerCLI

Recently when at the Western Pennsylvania VMUG I was asked by Rob Velarde if I had tried Show-Command with PowerCLI, I know that PowerCLI5.1 R2 now supports PowerShell V3 where the Show-Command cmdlet was introduced so thought I would take a look.

Looking at a cmdlets syntax can be complicated for people who are new to PowerShell and PowerCLI, the below is an example of the New-VM cmdlet…

SNAGHTMLa230967

So what does all this mean?

 

Continue reading

PowerShell Summit–My recorded sessions

imageRecently I was lucky enough to attend the PowerShell Summit at Microsoft HQ in Redmond, this was an awesome event which was focused on PowerShell, it included not only people who are using PowerShell but also some of the people who wrote and designed it.

There were a bunch of great sessions, all of which can be reviewed and slides downloaded here.  I was also asked to present two sessions.  I have included the recordings for these sessions below.  Thanks to Aaron Hoover for recording these.

A couple of comments which I took away from the PowerShell Summit which really surprised me where:

  • A number of people told me they had started out with PowerCLI and then worked back into PowerShell (much like myself).
  • I was surprised that 2/3 of the room when I presented were using VMware, after all this was a Microsoft Conference!

Continue reading

Automating storage with NetApp Workflow Automation

Do you wish you could automate your NetApp Storage infrastructure?

Do you wish your storage admins could give an easy to use custom interface to other areas of the business allowing them to provision or use storage however they need whilst still applying best practice and corporate policies to the configuration?

Continue reading

Relating vCloud Director to vCenter in PowerCLI

I was listening to episode 216 of the PowerScripting Podcast recently, Hal and Jonathan were talking to vCloud Director (vCD) expert Jake Robinson and “meeting expert” (listen it will make sense) Damian Karlson, it was a great show, very funny and I highly recommend you listen here: http://powerscripting.wordpress.com/2013/02/26/episode-216-jake-robinson-and-damian-karlson-talk-powercli/

Anyway, they were talking about relating vCD objects to vCenter objects, on the show they said it couldn’t be done without matching an ID and writing a custom function, this used to be the case but now I wanted to show a few cool things from PowerCLI which allows us to use a parameter called –RelatedObject to take away all the hard work from the matching IDs, Datastore IDs, Network IDs etc. Continue reading

PowerCLI 5.1 R2 Released

PowerCLIVersionVMware have just released PowerCLI 5.1 R2 and with it are the long awaiting cmdlets to work with VDS!

I worked with these a little and although VDS are not 100% fully covered in this release the cmdlets are certainly useful for most of the things I needed to do and they opened up VDS with the .extensiondata property for the rest of the things I wanted to play with.

Two of the cooler cmdlets where the Export-VDSwitch and New-VDSwitch –backupfile which can be used with 5.1 and the new VDS features to backup the VDS into a simple zip file and re-import it when needed.

I have included the new cmdlets and some examples from the help file below.

As well as the VDS cmdlets I am also happy that VMware now supports PowerShell v3 and vCloud Director 5.1 in both their admin version of PowerCLI and their Tenant version, this opens up vCD to automate some of the cooler new features of VCD and also enable the enhancements made by Microsoft in PowerShell v3.

Download it now. Continue reading

Checking Domain Controllers for Secure LDAP connections with PowerShell

imageI wanted to blog this quick bit of PowerShell as I could not find it anywhere else on the web whilst searching.

I needed to check the connected domain on a machine to see if SSL was configured and enabled for LDAP, the following script checks to see if SSL is enabled on one of the domain controllers in the current domain and then tries to make a connection to see if it works.

This can of course be altered to list and check all domain controllers easy enough:

$dc = [System.DirectoryServices.ActiveDirectory.Domain]::getCurrentDomain().DomainControllers | Select -First 1
$LDAPS = [ADSI]"LDAP://$($dc.name):636"
try {
	$Connection = [adsi]($LDAPS)
} Catch {
}
If ($Connection.Path) {
	Write-Host "Active Directory server correctly configured for SSL, test connection to $($LDAPS.Path) completed."
} Else {
	Write-Host "Active Directory server not configured for SSL, test connection to LDAP://$($dc.name):636 did not work."
}

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"
}

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

PowerShell at VMworld 2012 San Francisco

Its that time again! – VMworld, and let me tell you, this year its going to be epic!

I wanted to give you a list of all the PowerShell and PowerCLI sessions listed in this years Session Catalogue for San Francisco, if you don’t have these booked make sure you add them straight away as room is running out fast !

I am personally looking forward to presenting with some superstars this year such as:

  • Luc “The Master” Dekens
  • Jake “Head in the Clouds” Robinson
  • William “API Guru” Lam
  • Eric “PowerTool” Williams
  • Aidan “I am the center of excellence” Dalgliesh

We have some great decks and some awesome demos planned, don’t miss them.  And if you see me come say hi and tell me how you use PowerCLI, you never know I may have a spare sticker, poster and badge to get rid of.

Sessions

INF-VSP1252 – What’s New with vSphere Automation

Click here to add
In this session Technical Marketing automation experts William Lam and Alan Renouf will take you through VMware Automation.You will learn which products are available, which products to use and how Automation fits into the VMware Suite of products. In this session Alan and William will take you through the exciting features available to use when automating VMware products, both beginners and experts will learn how to use new features to make your life easier and more productive. Monday, Aug 27, 2:30 PM – 3:30 PM

Wednesday, Aug 29, 9:30 AM – 10:30 AM

William Lam – Sr. Technical Marketing Engineer, VMware, Inc.

Alan Renouf – Sr. Technical Marketing Architect, VMware, Inc.

INF-BCO2155 – vCloud DR for Oxford University Computing Services – Real World Example

image
Resiliency is a key aspect of any infrastructure—it is even more important in infrastructure-as-a-service solutions. This session will include a tour of a real world vCloud DR solution deployed at Oxford University Computing Services (OUCS). Throughout the session there will be detailed guidance on how both the management and resource clusters were designed, deployed and automated. Furthermore the details of how SRM in conjunction with VMware vSphereTM PowerCLI (PowerCLI) was used to automate the end-to-end recovery of a vCloud Director–based infrastructure. This session will offer a perfect complimentary follow on from the whitepaper regarding the high-level process produced by Duncan Epping and Chris Colotti and the Automation session by Alan Renouf and Aidan Dalgleish. The paper combined with the Automation session will describe the process and the automation principles whereas this session will describe a complete real world implementation. Monday, Aug 27, 3:30 PM – 4:30 PM

Aidan Dalgleish – Consulting Architect, VMware, Inc.

Adrian Parks – Senior Systems Administrator, Oxford University Computing Services

Gary Blake – Senior Consultant, VMware, Inc.

INF-VSP1329 – PowerCLI Best Practices: The Return!

image
Building on the success of our PowerCLI Best Practices sessions from VMworld 2011, we want to show you our next collection of PowerCLI best practices. This session will show and demonstrate PowerCLI in all its aspects managing and automating not just VMware vSphere® but also VMware vCloud Director®, VMware® View™, VMware vShield™ and more Tuesday, Aug 28, 3:00 PM – 4:00 PM

Wednesday, Aug 29, 8:00 AM – 9:00 AM

Alan Renouf – Sr. Technical Marketing Architect, VMware, Inc.

Luc Dekens – Systems Engineer, Eurocontrol Maastricht.

INF-VSP2448 – Automating Bare Metal to the Cloud and Beyond

image
When working in the cloud automation is key, working at the scale of the cloud VMware customers need an easy and reliable method of deployment which gives guaranteed results 100% of the time. Cisco, VMware and Bluelock will show you how to automate the build of your complete vCloud Director Infrastructure from the bare metal up to the cloud and beyond. Learn how PowerShell can be used to create a single easily readable and adaptable script to ensure your system is built quickly and efficiently, watch as a system is built before your very eyes with no magic tricks or rabbits in sight. Tuesday, Aug 28, 12:00 PM – 1:00 PM

Eric Williams – Technical Marketing Engineer, Cisco Systems Inc.

Alan Renouf – Sr. Technical Marketing Architect, VMware, Inc.

Jake Robinson – Solutions Architect, BlueLock

INF-VSP1856 – Become a Rock Star with PowerCLI and vCenter Orchestrator

image
Automation is the future of cloud, and in this session attendees will learn how to identify areas in their environment that are primed for less administrator interaction. The combination of PowerCLI and VMware® vCenter™ Orchestrator puts considerable power in even the greenest users. Attendees will learn how to identify areas for automation and start learning to discern which tool is best suited for the job and when to use them together. Monday, Aug 27, 4:00 PM – 5:00 PM

Josh Atwell – Systems Administrator, Cisco Systems Inc.

INF-VSP2164 – Automation of vCloud Director Disaster Recovery

image
Resiliency is a key aspect of any infrastructure—it is even more important in infrastructure-as-a-service solutions. This session will include guidance, examples and demonstrations of the use of VMware vSphereTM PowerCLI (PowerCLI) to automate the recovery of a vCloud Director–based infrastructure. In particular the session will focus on the automation of the recovery steps for vCloud Director managed vApp workloads, that cannot be recovered with vSphere Site Recovery Manager (SRM). This session will offer a perfect complimentary follow on from the whitepaper regarding the high-level process released by Duncan Epping and Chris Colotti. The paper discussed the process and this session will describe the automation Tuesday, Aug 28, 4:30 PM – 5:30 PM

Alan Renouf – Sr. Technical Marketing Architect, VMware, Inc.

Aidan Dalgleish – Consulting Architect, VMware, Inc.

Group Discussions

GD27 – PowerCLI with Alan Renouf

image
During this group discussion we will talk about what can be automated, discuss the merits of using PowerShell and PowerCLI, show examples and talk about the best methods of achieving integration. We will also discuss tips and tricks you have learned and the best methods for automating the VMware products. Come prepared with examples and questions and remember this is a Group Discussion! Monday, Aug 27, 10:30 AM – 11:30 AM

Thursday, Aug 30, 12:00 PM – 1:00 PM

Alan Renouf – Sr. Technical Marketing Architect, VMware, Inc.

Hands on Labs

HOL-INF-10 – Script and Develop Your Cloud Solution with PowerCLI and the vSphere Web Client SDK

This lab is presented as two 30 minute lightning labs to help you extend your VMware solution. The PowerCLI module covers automating vSphere and vCloud Director. Novice users will learn to use the tool and more advanced users will get familiar with the new functionality available in the latest release of the product. You will walk away with a better understanding of PowerCLI and how it can help you in your day-to-day work. The vSphere Web Client SDK module teaches you several techniques for extending the vSphere Web Client. The goal of this module is to demonstrate the close integration that is possible with the vSphere Web Client SDK, and the ease with which you can integrate your own solutions into the vSphere Web Client. HOL

Attend the self paced Hands on Lab.

Videos

Still cant decide ?  Take a look at some of the session videos where the presenters explain more information…..