Tag Archives: PowerShell

vCheck updated to 6.10

Thanks to all the people who sent me a plugin for vCheck 6.0, these have now been published as part of vCheck 6.10 and zipped into a single download file which can be downloaded from below.  I also love the enhancements which have been sent to me for the core vCheck script, these include a script which times and reports on how long the plugins take – great for troubleshooting !

[wpdm_file id=17]

As always you can find more information about vCheck and how to use this by visiting this page.

Change log

There are now over 70 plugins, some fantastic stuff, below is a list of items which have changed or been fixed since 6.0:

# v 6.10 – Fixed multiple spelling mistakes and small plugin issues
# v 6.9 – Fixed VMKernel logs but had to remove date/Time parser due to inconsistent VMKernel Log entries
# v 6.8 – Added Creator of snapshots back in due to popular demand
# v 6.7 – Added Multiple plugins from contributors – Thanks!
# V 6.6 – Tech Support Mode Plugin fixed to work with 5.0 hosts
# V 6.5 – HW Version plugin fixed due to string output
# V 6.4 – Added a 00 plugin and VeryLastPlugin for vCenter connection info to separate the report entirely from VMware if needed.
# V 6.3 – Changed the format of each Plugin so you can include a count for each header and altered plugin layout for each plugin.
# V 6.2 – Added Time to Run section based on TimeToBuild by Frederic Martin
# V 6.1 – Bug fixes, filter for ps1 files only in the plugins folder so other files can be kept in the plugins folder.

Reminder

If a plugin is not needed in your environment remove it from the plugins folder as it will speed up the execution of your script.

Removing the vCD Agent from hosts

When re-installing my vCD home lab I had an issue when I went to re-add some hosts which had already been used in a vCD environment, this was because the hosts had already been “prepared” by vCD and therefore had the “vcloud-agent” vib installed.  Of course you would normally unprepare these from the vCD web interface before deleting your vCD instance but I forgot to do this.

I was then pointed to Chris Colotti’s post here which showed how to manually remove the vCD agent from hosts in this situation.

As I had a few of them to do I didn’t want to repeat myself and therefore wrote a quick PowerCLI Script to remove the agents for me.

Another thing I couldn’t see in Chris’s post which I had to do before it was fully removed was reboot the host so I added that into the script too.

Checking for hosts with the vCD Agent installed

We can easily see the hosts which have the vCD agent installed by running the following PowerCLI code:

Connect-VIServer MyVIServer -User Administrator –Password “Pa$$word”
Get-VMHost | Sort Name | Foreach {
    $ESXCLI = Get-EsxCli -VMHost $_ -ErrorAction SilentlyContinue
    $ESXCLI.software.vib.list() | Where { $_.Name -like "*vCloud*"} | Select @{N="VMHost";E={$ESXCLI.VMHost}}, Name, Version
}

We would receive the following output:

image

Removing the vCD Agents

Due to an issue with the way PowerCLI calls ESXCLI I have had to make the script slightly more complicated than it needs to be but it still does the job.  It will go through each host and remove the “vcloud-agent” vib and then reboot them ready to be added into the new vCD instance.

# The following two variables are used to define the esx hosts local login details
$HostUser = "root"
$HostPass = "localpa$$"

$vCenter = Connect-VIServer MyviServer -User Administrator –Password “Pa$$word”
Get-Cluster | Get-VMHost | Sort Name | Foreach {
    $ESXCLI = Get-EsxCli -VMHost $_ -ErrorAction SilentlyContinue
     $vCDHosts = $ESXCLI.software.vib.list() | Where { $_.Name -like "*vCloud*"} | Select @{N="VMHost";E={$ESXCLI.VMHost}}, Name, Version
    $vCDHosts
}

$vCenter | Disconnect-VIServer -Force -Confirm:$false

# Because of a bug in PowerCLI with ESXCLI we need to connect to each host individually and not through vCenter
$vCDHosts | Foreach {
    $VMHCon = Connect-VIServer ($_.VmHost) -User $HostUser -Password $HostPass
    Write-Host "Removing vCloud Agent for $($_.VMHost)"
    $ESXCLI = Get-EsxCli -ErrorAction SilentlyContinue
    $ESXCLI.software.vib.remove($false, $true, $false, $true, $_.Name)
    # During testing I found a reboot was needed
    Write-Host "Rebooting host $($_.VMHost)"
    Restart-VMHost -VMHost $_.VmHost -Confirm:$false
    $VMHCon | Disconnect-VIServer -Confirm:$false
}

vCheck 6.0 released

One of the most popular scripts on my site with 26,966 downloads of v5 has been the vCheck script, for those of you who haven’t seen this yet here is a quick summary of this script:

Summary

vCheck is a vCenter checking script, the script is designed to run as a scheduled task before you get into the office to present you with key information via an email directly to your inbox in a nice easily readable format.

This script picks on the key known issues and potential issues of the virtual infrastructure and reports it all in one place so all you do in the morning is check your email.

One of they key things about this report is if there is no issue in a particular place you will not receive that section in the email, for example if there are no datastores with less than 5% free space (configurable) then the disk space section will not show in the email, this ensures that you have only the information you need in front of you when you get into the office.

This script is not to be confused with an Audit script, I don’t want to remind you that you have 5 hosts and what there names are and how many CPU’s they have each and every day as you don’t want to read that kind of information unless you need it, this script will only tell you about problem areas with your infrastructure.

Continue reading →

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

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 →

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 →