Workspace-Guru

New-vCenterHostConnection

Feb 3rd, 2018
2,848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <# 
  2.     .NOTES
  3.     ===========================================================================
  4.      Created on:    3-2-2018
  5.      Created by:    Chris Twiest
  6.      Organization:  Workspace-Guru.com     
  7.     ===========================================================================
  8.     .DESCRIPTION
  9.         This script will automaticly download the certificate from your vCenter server and import it. Then get the thumbprint and create a Citrix hosting connection.
  10.         Run the script on your Delivery controller or change line 35 to your delivery controller URL.
  11.     .EXAMPLE
  12.     New-vCenterHostConnection -vCenterURL "https://vcenter.domain.com" -vCenterUsername "DOMAIN\Administrator" -vCenterPassword 'Password01'
  13.  
  14.     This will create a new vCenter hosting connection to https://vcenter.domain.com with the user DOMAIN Administrator.
  15. #>
  16.  
  17.  
  18. function New-vCenterHostConnection
  19. {
  20.     [CmdletBinding()]
  21.     param (
  22.         [Parameter(Mandatory = $true)]
  23.         [string]$vCenterURL,
  24.         [Parameter(Mandatory = $true)]
  25.         [string]$vCenterUsername,
  26.         [Parameter(Mandatory = $true)]
  27.         [string]$vCenterPassword
  28.     )
  29.    
  30.     asnp Citrix*
  31.    
  32.     write-host "Create hosting connection to $vCenterURL"
  33.    
  34.     $computername = (Get-WmiObject win32_computersystem).DNSHostName + "." + (Get-WmiObject win32_computersystem).Domain
  35.     $URL = "$computername" + ":80"
  36.     $path = $env:TEMP
  37.     $Uri = $vCenterURL
  38.     $Outputfile = "$path" + "\vmware.cer"
  39.     $Testcertpath = $Outputfile | Test-Path
  40.     if ($Testcertpath -eq "False"){
  41.     write-host "Certificate already exsist will delete it"
  42.     Remove-Item -Path $Outputfile -Force
  43.     }
  44.     $request = [System.Net.WebRequest]::Create($Uri)
  45.     $Provider = New-Object Microsoft.CSharp.CSharpCodeProvider
  46.     $Compiler = $Provider.CreateCompiler()
  47.     $Params = New-Object System.CodeDom.Compiler.CompilerParameters
  48.     $Params.GenerateExecutable = $False
  49.     $Params.GenerateInMemory = $True
  50.     $Params.IncludeDebugInformation = $False
  51.     $Params.ReferencedAssemblies.Add("System.DLL") > $null
  52.     $TASource = @'
  53.       namespace Local.ToolkitExtensions.Net.CertificatePolicy {
  54.         public class TrustAll : System.Net.ICertificatePolicy {
  55.           public TrustAll() {
  56.           }
  57.           public bool CheckValidationResult(System.Net.ServicePoint sp,
  58.             System.Security.Cryptography.X509Certificates.X509Certificate cert,
  59.             System.Net.WebRequest req, int problem) {
  60.             return true;
  61.           }
  62.         }
  63.       }
  64. '@
  65.     $TAResults = $Provider.CompileAssemblyFromSource($Params, $TASource)
  66.     $TAAssembly = $TAResults.CompiledAssembly
  67.     $TrustAll = $TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
  68.     [System.Net.ServicePointManager]::CertificatePolicy = $TrustAll
  69.     $servicePoint = $request.ServicePoint
  70.     $response = $request.GetResponse()
  71.     $certificate = $servicePoint.Certificate
  72.     $certBytes = $certificate.Export(
  73.     [System.Security.Cryptography.X509Certificates.X509ContentType]::Cert)
  74.     [System.IO.File]::WriteAllBytes($OutputFile, $certBytes)
  75.    
  76.     $certPrint = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
  77.     $certPrint.Import("$path\vmware.cer")
  78.     $Connectionuid = New-Item -ConnectionType "VCenter" -CustomProperties "" -HypervisorAddress "$vCenterURL/sdk" -Path @("XDHyp:\Connections\VMware") -Scope @() -Password $vCenterPassword -UserName $vCenterUserName -SSLThumbprint $certPrint.Thumbprint -persist | select HypervisorConnectionUid
  79.     New-BrokerHypervisorConnection -AdminAddress "$URL" -HypHypervisorConnectionUid $connectionuid.HypervisorConnectionUid
  80. }
Advertisement
Add Comment
Please, Sign In to add comment