PoshCode Archive  Artifact [7b16a00e0b]

Artifact 7b16a00e0bcf91e24ccbb9257d367bd7f0834bab3ef8f05865dac6ccc5597989:

  • File VMware-Lab-Manager-4-x.ps1 — part of check-in [2efb859dd5] at 2018-06-10 13:07:57 on branch trunk — Based on poshcode.org/753 – minor mods to support new mandatory authentication parameters in Lab Manager 4.x. (user: oldsienna size: 3804)

# encoding: ascii
# api: powershell
# title: VMware Lab Manager 4.x
# description: Based on poshcode.org/753 – minor mods to support new mandatory authentication parameters in Lab Manager 4.x.
# version: 0.1
# type: function
# author: oldsienna
# license: CC0
# function: Ignore-SslErrors
# x-poshcode-id: 2263
# x-archived: 2015-04-11T08:51:42
# x-published: 2011-09-22T16:27:00
#
# Sample code to demonstrate PowerShell 2.0 integration with Lab Manager APIs. Majority of code can be omitted if properly signed SSL certificates are used.
#
function Ignore-SslErrors {
	# Create a compilation environment
	$Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
	$Compiler=$Provider.CreateCompiler()
	$Params=New-Object System.CodeDom.Compiler.CompilerParameters
	$Params.GenerateExecutable=$False
	$Params.GenerateInMemory=$True
	$Params.IncludeDebugInformation=$False
	$Params.ReferencedAssemblies.Add("System.DLL") > $null
	$TASource=@'
	  namespace Local.ToolkitExtensions.Net.CertificatePolicy {
	    public class TrustAll : System.Net.ICertificatePolicy {
	      public TrustAll() { 
	      }
	      public bool CheckValidationResult(System.Net.ServicePoint sp,
	        System.Security.Cryptography.X509Certificates.X509Certificate cert, 
	        System.Net.WebRequest req, int problem) {
	        return true;
	      }
	    }
	  }
'@ 
	$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
	$TAAssembly=$TAResults.CompiledAssembly

	## We now create an instance of the TrustAll and attach it to the ServicePointManager
	$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
	[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
}

function New-ObjectFromProxy {
	param($proxy, $proxyAttributeName, $typeName)

	# Locate the assembly for $proxy
	$attribute = $proxy | gm | where { $_.Name -eq $proxyAttributeName }
	$str = "`$assembly = [" + $attribute.TypeName + "].assembly"
	invoke-expression $str

	# Instantiate an AuthenticationHeaderValue object.
	$type = $assembly.getTypes() | where { $_.Name -eq $typeName }
	return $assembly.CreateInstance($type)
}

function Connect-LabManager {
	param
    (
        [string] $server, 
        $credential,
        [string] $organizationname = "Default",
        [string] $workspacename = "Main"
    )
        
	# Log in to Lab Manager's web service.
	$server = "https://" + $server + "/"
	$endpoint = $server + "LabManager/SOAP/LabManager.asmx"
	$proxy = new-webserviceproxy -uri $endpoint -cred $credential

	# Before continuing we need to add an Authentication Header to $proxy.
	$authHeader = New-ObjectFromProxy -proxy $proxy -proxyAttributeName "AuthenticationHeaderValue" -typeName "AuthenticationHeader"
	$authHeader.username = $credential.GetNetworkCredential().UserName
	$authHeader.password = $credential.GetNetworkCredential().Password
    $authHeader.organizationname = $organizationname
    $authHeader.workspacename = $workspacename
	$proxy.AuthenticationHeaderValue = $authHeader
	return $proxy
}

# Examples:
# Run this command if your Lab Manager's certificate is untrusted but you want to connect.
Ignore-SslErrors

# Connect to Lab Manager.
$labManager = Connect-LabManager -server "demo.eng.vmware.com" -credential (get-credential)

# Find out what operations there are.
$labManager | gm | where { $_.MemberType -eq "Method" }
# See http://www.vmware.com/pdf/lm30_soap_api_guide.pdf for help on arguments.

# List all library configurations.
$labManager.ListConfigurations(1)

# Find all machines deployed from any library configuration.
$labManager.ListConfigurations(1) | foreach { write-host ("For Configuration " + $_.id + ":"); $labManager.ListMachines($_.id) }