PowerCLI 4.1 namespace changes

Output type changes in PowerCLI 4.1In PowerCLI 4.1 VMware changed the namespaces of the output types. According to VMware, this was done to improve the internal structure and enable other VMware teams to write cmdlets for the products they develop. You can read all about it in the vSphere PowerCLI blog article Output type changes in PowerCLI 4.1

Today when building a script I had to declare a variable using the full namespace path. Since I was building and testing the script in a PowerCLI 4.0 environment, all went fine until I ran the script in a PowerCLI 4.1 test environment. The script failed because of the namespace mismatch.

VMware provided a full listing of the namespace changes and a PowerShell script to check for scripts which use incompatible types.  I didn’t want to edit all my scripts one at a time, so I decided to build a little function called Fix-Namespace to do the hard work for me.

The function accepts a FileInfo object from the pipeline so it can be used in combination with the Get-ChildItem cmdlet

  PS> Get-ChildItem myScript.ps1 | Fix-Namespace
  PS> Get-ChildItem -recurse -Include '*.ps1', '*.psm1', '*.ps1xml' | Fix-Namespace

 or you can pass a filename as a parameter

  PS> Fix-Namespace myScript.ps1

The script also downloads the full listing of namespace changes from VMware if the file is not found in the current directory. You can use the -forceDownload parameter to force a fresh download of the file. Use it at your own risk.

function Fix-Namespace {
<#
.SYNOPSIS
  Replaces old pre-4.1 namespaces to 4.1 namespaces
.DESCRIPTION
  This function replaces the old pre-4.1 namespace with their 4.1 counterparts
.NOTES
  Author:  Arnim van Lieshout
.PARAMETER FILE
  The powershell scriptfile to fix
.EXAMPLE
  PS> Get-ChildItem myScript.ps1 | Fix-Namespace
.EXAMPLE
  PS> Get-ChildItem -recurse -Include '*.ps1', '*.psm1', '*.ps1xml' | Fix-Namespace
.EXAMPLE
  PS> Fix-Namespace myScript.ps1
#>
	[CmdletBinding()]
	Param (
	[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true,
		ValueFromPipelineByPropertyName=$true,
		HelpMessage = "Enter a Powershell scriptfile")]
        [Alias("PsPath")]
        [string]$filename,
	[Parameter()][Switch]$forceDownload)

	Begin {
		#download file
		$baseUrl = "http://blogs.vmware.com/files/"
		$nsFilename = "typemapping---powercli-4.0.1-to-4.1-1.txt"

		# Download file if not present or if forced download
		if(!(Test-Path $nsFilename) -or $forceDownload){
			$web = New-Object net.WebClient
			$web.downloadfile($baseUrl+$nsFilename,$nsFilename)
		}

		#create hashtable
		$nameSpace = @{}
		$content = Get-Content $nsFilename
		$content | %{,$_.split()} | %{
			if ($_[0] -eq "Datastore"){
				$nameSpace.add("[Datastore]",$_[2])
			}
			else {
				$nameSpace.add($_[0],$_[2])
			}
		}
	}

	process {
		Set-Content $filename (
			Get-Content $filename | % {
				foreach ($key in $nameSpace.keys) {
					$_ = $_.replace($key, $nameSpace[$key])
				}
				$_
			}
		)
		$filename
	}
}

Related posts:

  1. PowerShell / PowerCLI linkage Tweet Since I started looking into PowerShell and PowerCLI, I gathered a couple of links which I found interesting and useful. I needed a way for them to be accessible...
  2. PowerCLI: Get WMI info from isolated guests Tweet A few weeks back I posted an article on matching Windows and VMware disks. Unfortunately this would work only if you could remotely query WMI information from that VM....
  3. PowerCLI: Match VM and Windows harddisks – Part 2 Tweet This is a follow up on a post I did a couple of weeks ago to create a mapping table between Windows- and VMware hard disks. In another previous...
  4. Setting custom attributes with VMware PowerCLI Tweet Last week I wanted to extend my vCenter with some extra custom attributes on my VMs. This would extend the usability of the Export List feature for reporting purposes....
  5. Match VM and Windows harddisks using PowerCLI Tweet Today I was asked to extend a disk of a Windows virtual machine. Normally this is a standard procedure and finished within minutes. The hardest part of the procedure...

0 Comments on “PowerCLI 4.1 namespace changes”

Leave a Comment