Category Archives: PowerShell

Using PowerShell with Internet Explorer

PowerShell is a great language, I am often amazed at how far it can actually reach and what you can gather data, write data or use data from.

Mainly I use PowerShell to manage my VMware environment but last night I broke from my comfort zone and took some time to see what I was able to do with Internet Explorer – Did you know you can programmatically browse the web with PowerShell ?  Well you can!

The Reason

First thing I needed was a reason, at the moment there is a poll for top virtualization blogs, last year I surprisingly did very well in this and would like to thank anyone who voted for me.  This year as I was filling it out and selecting some of my favorite blogs from the list.  People like Duncan Epping, Eric Sloof, William Lam, Luc Dekens, Jonathan Medd, Frank Denneman the list goes on and on and then I found my blog tucked away near the bottom amongst the other blogs beginning with V… as this is a virtualization top list you can imagine there were a few beginning with V!

Anyway, I thought there must be an easier way to allow people to vote for me than to hope they find me amongst the V’s and don’t get bored along the way and choose others so here it is:

The below script is not a cheating script, it will not make you vote for me 1000 times it will simply start internet explorer, browse to the survey and select my site for you, one this has done you are free to deselect me if you wish and also select the other 9 participants you would like to vote for.  After this just follow on through the survey and click Submit to finish.

Obviously with PowerShell I could have just added my favorite bloggers to this script and completed the survey for you but that would be cheating and I believe in the freedom to vote!

So if you have not yet voted then simply paste the following code into a PowerShell window and watch it control internet explorer and automatically select Virtu-Al from the list, the other choices are up to you!

$URL = "http://www.surveygizmo.com/s3/786135/Top-VMware-virtualization-blogs-2012"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate($URL) 
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.Document.getElementById("sg_NextButton").click()
$ie.Document.getElementById("sgE-786135-1-27-10906").Click() # Click Virtu-Al

PowerCLI Android and iPhone App

A while back I found an iPhone and iPad app which allowed you to reference the PowerCLI Cmdlets, this is quite useful when you are on the move and need a quick reference.

Today I came across a similar app for the android and also iPhone and wanted to share it.

vPowerCLI5 Reference is an app for Android which can be downloaded from here:

Android: http://droidmill.com/vpowercli5-reference-405917.html#.Txgq7ZiE6-8

iPhone: http://itunes.apple.com/us/app/vpowercli5-reference/id489731144?mt=8

This tool is to be used for referencing the VMware vSphere PowerCLI cmdlets. vSphere PowerCLI is a command-line and scripting tool built on Windows PowerShell, and is used for managing and automating vSphere.

It looks like this is a Android/iPhone friendly version of the online PowerCLI help which can be found here:

http://www.vmware.com/support/developer/PowerCLI/PowerCLI501/html/index.html

image

image

 

 

 

 

 

 

 

 

 

 

This post was updated to include iPhone app.

Using Console2 with PowerShell and PowerCLI

I may be a late comer to this app but there is a cool free app here called Console2.  I am now using it for the quick work I need to do in PowerCLI and PowerShell, the stuff I dont need to save or need an editor for.

Its great as a portable app, I have it in my DropBox folder and its syncd between my PC’s as well as being on a USB key so I can use it from anywhere.

I like the way you can resize the window and it resizes the buffer and the copy and paste is also enhanced from the normal console.

I have packaged my settings and logos up so you too can see what I see, just download the settings file from below and Console2 from here and extract them into the same folder.

{filelink=15}

Eye Candy

Console2

Console2PS

Updating your Image Profile

If you have been using the image builder and auto deploy cmdlets which came with vSphere 5.0 then you will already know how easy it is to completely rebuild all hosts just by replacing a simple auto deploy rule with your new image profile and then rebooting your hosts.

But how do you create your new image profile ?

An Image profile normally consists of a base ESXi image downloaded from VMware and then some custom software packages added in to ensure you have all the third party addons, drivers and utilities needed for the host to work with your infrastructure.

An example of this is the EMC PowerPath Software Package or the HP ESXi Proliant offline bundle, others may include the software package needed for the vCD Agent.

Once these software packages have been added to your image profile you are then able to export it back as an offline zip file, ISO file or use it in you Auto Deploy rule-set.

But when it comes to updating you need to remember which software packages you added to which build of ESX and repeat this procedure, this can be time consuming.

Introducing Update-ESXImageProfile

Did you know that VMware has an online repository of up to date versions of ESXi Software Depots ?  You can easily add this to your Image Builder session with the following line of code:

Add-EsxSoftwareDepot https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml

image

Using this online depot I was able to create a script which would download any newer software packages from the base ESXi Software Depot online and compare these against your current image profile, once this has been done it will add these latest versions to your image profile ensuring you have an up-to-date Image Profile.

Of course you will need to make sure you export this as a zip again so you can use it in the future and also add it to your Auto Deploy rule-set and reboot your hosts.

See it in action

The Script

Function Update-ESXImageProfile ($ImageProfile, $tools){
	if ($ImageProfile.Readonly) {
		Write-Host "Your ImageProfile is read-only and therefore cannot be updated, please use a custom ImageProfile"
	} Else {
		Write "Loading online Software Depot"
		$SD = Add-EsxSoftwareDepot -DepotUrl https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml
		If ($tools) {
			$NEWIP = Get-EsxImageProfile -Vendor "VMware, Inc." | Sort ModifiedTime -Descending | Where { $_.Name -notlike "*tools" } | Select -First 1
		} Else {
			$NEWIP = Get-EsxImageProfile -Vendor "VMware, Inc." | Sort ModifiedTime -Descending | Where { $_.Name -like "*tools" } | Select -First 1
		}
		Write-Host "New Image Profile found called $($NEWIP.Name)..."
		Write-Host "Checking for updated packages which do not exist in $ImageProfile"		
		$Compare = Compare-EsxImageProfile -ReferenceProfile $ImageProfile -ComparisonProfile $NEWIP
		$Updates = ($Compare | Select -ExpandProperty UpgradeFromRef)
		If ($Updates) {
			Foreach ($SP in $Updates) {
				$UpdatedPackage = Get-EsxSoftwarePackage | Where {$_.Guid -eq $SP}
				Write-Host "Adding $($UpdatedPackage.Name) to $ImageProfile"
				$Add = Add-EsxSoftwarePackage -ImageProfile $ImageProfile -SoftwarePackage $UpdatedPackage
			}
		}
		$OnlyInComp = ($Compare | Select -ExpandProperty OnlyInComp)
		If ($OnlyInComp) {
			Foreach ($SP in $OnlyInComp) {
				$UpdatedPackage = Get-EsxSoftwarePackage | Where {$_.Guid -eq $SP}
				Write-Host "Adding $($UpdatedPackage.Name) to $ImageProfile"
				$Add = Add-EsxSoftwarePackage -ImageProfile $ImageProfile -SoftwarePackage $UpdatedPackage
			}
		} 
		If ((-not $OnlyInComp) -and (-not $Updates)) {
			Write-Host "No Updates found for $ImageProfile"
		}
	}
}

Update-ESXImageProfile -ImageProfile "CustomImageProfile" -Tools $true

VMware vShield PowerShell Module

A while back I was asked if we could automate some areas of vShield, VMware Security suite of products.  I was asked to do this for a demo which was to be used at VMworld, having never touched vShield before I was thrown into the world of security at the deep end.  The first thing I found on my quest was the vShield API, there is a great document by VMware which explains the vShield proprietary Web-RPC API (Based on Rest API) and all the calls you would ever need to make to work with the vShield products.

Now I had the API details I knew I could easily write some PowerShell code in the form of an advanced function to work with the API, the first piece of code I wrote was a generic function which allowed me to GET, PUT, DELETE and POST to a proprietary Web-RPC based Restful API. I know PowerShell v3 will include cmdlets for this but I didn’t want to wait or add a dependency on something which wasn’t available as yet.

Continue reading →

VM Start-up script

Back in Jan 2010 I wrote a script which shut down the virtual infrastructure, this was used to ensure your VMs were shut down and then your hosts were shut down in an orderly manner, ideal for things like UPS shutdown scripts etc.

A slight spin on that is a Start-Up script, I was asked by a couple of people if I had one of these, it also becomes more relevant with the vCenter Virtual appliance, no need for HA and DRS rules or to manually search for the vCenter Virtual Appliance if there is a power outage.

The script below will connect to all hosts you define at the start, these could obviously be read from a csv, xml file or a database etc but I just define them in a variable for ease of the script.

The script will firstly connect to each of these hosts in turn, once connected it will search for the first VMs, in my case this is a couple of domain controllers, it could be database servers or any other infrastructure servers which need to come up before vCenter.   Once these VMs have been found the script will start them and wait for their tools status to be ready, this is an easy way to check that the VM is started completely.

Once the infrastructure servers are started it will then search for your vCenter VM, this could be on any of the connected hosts, it will start it and again wait until the tools return as being completely started.

Continue reading →

vCloud Director 1.0 PowerShell Advanced Functions

Its no secret that VMware are working on vCloud Director (vCD) cmdlets, they recently surveyed the PowerShell and VMware community to make sure they had all the information needed to make this as much of a success as the vSphere PowerCLI cmdlets.

But did you know you can already use PowerShell with vCD ?  It should be no surprise to anyone who uses PowerShell to know that PowerShell does a great job with XML manipulation and API calls.

As part of vCD 1.0 there was obviously an API, this was based on REST which is essentially calls to web pages and sending/receiving data, so with the knowledge that this API is available and that PowerShell is a great way to communicate with this API all that was left was for someone to tie all this together and create some advanced functions which can be used against the vCloud API…. enter Jake Robinson.

image

Jake has been doing some great work recently, personally I have spoken to him a couple of times and had the privilege of meeting him at VMworld 2011 Las Vegas, he is a great guy and very knowledgeable about both PowerShell and vCD so the perfect guy to create some advanced functions.

The functions can be downloaded from here:

Download: https://github.com/jakerobinson/vCloud-Powershell

I would also recommend checking out his great video on what he has created and some examples of how these can be used published here on his blog (add to your RSS feed) and also embedded below. Continue reading →

PowerCLI for vCloud Director–Have your say !

VMware have recently announced a survey on the PowerCLI site, this has been setup timageo poll people for what they think would be the best way to introduce cmdlets for vCloud Director, personally I think it’s a great move and proves that VMware really does listen to their customers opinions.

Their questions not only ask how you would like the vCloud cmdlets distributed but also if you would prefer common objects like VMs and Users to be new cmdlets based towards vCloud director or to add these as additional parameters to the existing cmdlets.

Personally when answering these questions I have to remind myself of a few things and ask myself a few questions:

  • vCloud Org users or tenants are going to be interested in different things than a vSphere admin, for example, does a tenant care about the details of the datastore so long as they are getting what they paid for, do they care about things like what host the VM or even what vCenter the VM is hosted in ? With this in mind do they really want to see the full set of “vSphere Properties” for an object or would a new set of properties based on the cloud information be relevant, similar with the cmdlets, do they want to see all the vSphere cmdlets or would a more specific set of cmdlets be better ?
  • Will vCloud Admins want to connect to both the vCloud and the vCenter at the same time ?  How will this work, when connected to both what would they expect to be displayed when running Get-VM? vSphere VMs, vCloud VMs both ? How would the properties be displayed ?
  • Will vCloud admins want to take a cloud VM and perform vSphere actions against the VM ?

Continue reading →

vSphere Distributed Switch PowerCLI cmdlets

For a long time now when presenting at VMworld or VMUGS I have asked the question – What would you like to see from PowerCLI next ?  The standard answer I get at most of these is vSphere Distributed Switch (VDS) cmdlets !

Luc Dekens did a great job with his series of posts showing how you could create your own VDS advanced functions, these allow you to add the functions to your PowerCLI session and work with VDS, find them here and take a look, they are a great example of how you can expand PowerCLI to support areas not yet written.

So what’s new ?

VMware have now released some PowerCLI cmdlets as a fling which work with VDS and allow you to do most of the common VDS tasks in your vSphere environment.  Please note that this is a fling and therefore not officially supported by VMware.  Having said that we are definitely keen for you to download these and try them out – please make sure you leave feedback as this will help with future versions !

Requirements

Currently there are some strict requirements around these cmdlets:

  • Windows required XP or Windows Server 2003 or 2008 is required  (Windows 7 and Vista are not supported currently)
  • VMware PowerCLI 4.1.1 or later is needed

Continue reading →

Auto Deploy and Image Builder profile functions

One of the best and one of my favorite ideas from the PowerCLI team (there are so many to choose from) was to include an initialization script when you launch PowerCLI, this gives us the nice black background, custom prompt and also other things like the Get-VICommand function.

Get-VICommand lists all the VMware related cmdlets added to your session, this is useful for quickly listing the VMware cmdlets you need, most people think this is actually a cmdlet which is part of PowerCLI but if you have ever imported the VMware snapin into a PowerShell session and tried to use Get-VICommand you will quickly realize this is not part of the cmdlet set.

Continue reading →