PoshCode Logo PowerShell Code Repository

Connect-LabManager (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/753"></script>download | new post

If you have VMware Lab Manager, this script makes it easier than ever to connect to and automate Lab Manager actions.

  1. function Ignore-SslErrors {
  2.         # Create a compilation environment
  3.         $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
  4.         $Compiler=$Provider.CreateCompiler()
  5.         $Params=New-Object System.CodeDom.Compiler.CompilerParameters
  6.         $Params.GenerateExecutable=$False
  7.         $Params.GenerateInMemory=$True
  8.         $Params.IncludeDebugInformation=$False
  9.         $Params.ReferencedAssemblies.Add("System.DLL") > $null
  10.         $TASource=@'
  11.           namespace Local.ToolkitExtensions.Net.CertificatePolicy {
  12.             public class TrustAll : System.Net.ICertificatePolicy {
  13.               public TrustAll() {
  14.               }
  15.               public bool CheckValidationResult(System.Net.ServicePoint sp,
  16.                 System.Security.Cryptography.X509Certificates.X509Certificate cert,
  17.                 System.Net.WebRequest req, int problem) {
  18.                 return true;
  19.               }
  20.             }
  21.           }
  22. '@
  23.         $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
  24.         $TAAssembly=$TAResults.CompiledAssembly
  25.  
  26.         ## We now create an instance of the TrustAll and attach it to the ServicePointManager
  27.         $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
  28.         [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
  29. }
  30.  
  31. function New-ObjectFromProxy {
  32.         param($proxy, $proxyAttributeName, $typeName)
  33.  
  34.         # Locate the assembly for $proxy
  35.         $attribute = $proxy | gm | where { $_.Name -eq $proxyAttributeName }
  36.         $str = "`$assembly = [" + $attribute.TypeName + "].assembly"
  37.         invoke-expression $str
  38.  
  39.         # Instantiate an AuthenticationHeaderValue object.
  40.         $type = $assembly.getTypes() | where { $_.Name -eq $typeName }
  41.         return $assembly.CreateInstance($type)
  42. }
  43.  
  44. function Connect-LabManager {
  45.         param($server, $credential)
  46.  
  47.         # Log in to Lab Manager's web service.
  48.         $server = "https://" + $server + "/"
  49.         $endpoint = $server + "LabManager/SOAP/LabManager.asmx"
  50.         $proxy = new-webserviceproxy -uri $endpoint -cred $credential
  51.  
  52.         # Before continuing we need to add an Authentication Header to $proxy.
  53.         $authHeader = New-ObjectFromProxy -proxy $proxy -proxyAttributeName "AuthenticationHeaderValue" -typeName "AuthenticationHeader"
  54.         $authHeader.username = $credential.GetNetworkCredential().UserName
  55.         $authHeader.password = $credential.GetNetworkCredential().Password
  56.         $proxy.AuthenticationHeaderValue = $authHeader
  57.         return $proxy
  58. }
  59.  
  60. # Examples:
  61. # Run this command if your Lab Manager's certificate is untrusted but you want to connect.
  62. # Ignore-SslErrors
  63.  
  64. # Connect to Lab Manager.
  65. # $labManager = Connect-LabManager -server "demo.eng.vmware.com" -credential (get-credential)
  66.  
  67. # Find out what operations there are.
  68. # $labManager | gm | where { $_.MemberType -eq "Method" }
  69. # See http://www.vmware.com/pdf/lm30_soap_api_guide.pdf for help on arguments.
  70.  
  71. # List all library configurations.
  72. # $labManager.ListConfigurations(1)
  73.  
  74. # Find all machines deployed from any library configuration.
  75. # $labManager.ListConfigurations(1) | foreach { write-host ("For Configuration " + $_.id + ":"); $labManager.ListMachines($_.id) }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me