# 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: 2318 # x-archived: 2015-05-09T14:37:28 # x-published: 2011-10-24T11:19: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. # Added a function to connect to the Lab Manager’s internal API and some examples code of how to work with it. The SOAP API provides a VMware supported set of automation functionality however the set is very limited in comparison to what is provided by the VMware unsupported internal which provides far more automation ability. # Lab Manager internal API help files can be obtained here: http://communities.vmware.com/docs/DOC-8603 , and here: http://communities.vmware.com/docs/DOC-10608 . The Lab Manager API community discussion forums can be found here: http://communities.vmware.com/community/developer/forums/labmanager . # 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 } function Get-LabManagerInternal { param ( [string] $server = $(throw "Parameter -Server [System.String] is required."), $credential = $(get-credential), [string] $organizationname = "Global", [string] $workspacename = "Main" ) $labManagerInternalUri = [System.Uri] "https://$server/LabManager/SOAP/LabManagerInternal.asmx" $proxy = New-WebServiceProxy -Uri $labManagerInternalUri -Credential $credential if ($proxy) { # 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) } # Internal API Examples: # Connect to Lab Manager Internal API. $labmanagerinternal = Get-LabManagerInternal -server "demo.eng.vmware.com" -organizationname "Default" -workspacename "Main" -credential (get-credential) # Gets all Media images in Lab Manager. $labmanagerinternal.GetAllMedia()