vmware / PowerCLI-Example-Scripts

http://blogs.vmware.com/powercli
Other
743 stars 602 forks source link

error o command Disconnect-SsoAdminServer -Server $server #534

Closed yanlu2 closed 2 years ago

yanlu2 commented 2 years ago

Describe the bug

I've trying to configure a AD over LDAPs Identity source with connect-ssoadminServer command. I used command Disconnect-SsoAdminServer at end but it failed. and it failed in my other scripts. the srcipt I'm using in vCenter-adding-AD-LDAPs.ps1 is

$path="C:\Temp\Reports\vCenters\" # file location

$server="server.comp.com" # VC server

$username = "administrator@vsphere.local" $passwd = password $localdomain="vsphere.local" $corpdomain="corp.comp.com" $vcenterfile=$path+"vcentertestlist.txt" $vcenterlist = get-content $vcenterfile

Foreach ($server in $vcenterlist) { $hostname=$server.Split(".")[0] $rptfile=$path+"usrimpt-"+$hostname+".txt"

Read-Host -Prompt "`nPress any key to continue add AD over LDAPs to vCenter $server"

Connect-SsoAdminServer -Server $server -User $username -Password $passwd -SkipCertificateCheck

#Get-IdentitySource -External
Add-LDAPIdentitySource `
      -Name 'Corp LDAPs' `
      -DomainName 'corp.comp.com' `
      -DomainAlias 'Corp' `
      -PrimaryUrl 'ldaps://appauth.corp.comp.com:636' `
      -BaseDNUsers 'dc=corp,dc=comp,dc=com' `
      -BaseDNGroups 'dc=corp,dc=comp,dc=com' `
      -Username 'svc_vmlabmgrrsa@corp.comp.com' `
      -Password 'aaaaaaaaa' `
      -Certificates 'C:\Temp\Certs\cert.cer'
Disconnect-SsoAdminServer -Server $server

}

And output is

PS C:\Users\luy13> C:\PS_scripts\vCenter\vCenter-adding-AD-LDAPs.ps1

Press any key to continue add AD over LDAPs to vCenter server.corp.comp.com:

Name : server.corp.comp.com ServiceUri : https://server.corp.comp.com/sso-adminserver/sdk/vsphere.local User : administrator@vsphere.local Id : /SsoAdminServer=vsphere.local/administrator@server.corp.comp.com IsConnected : True Client : VMware.vSphere.SsoAdminClient.SsoAdminClient RefCount : 1

Disconnect-SsoAdminServer : Cannot process argument transformation on parameter 'Server'. Cannot convert the "server.corp.comp.com" value of type "System.String" to type "VMware.vSphere.SsoAdminClient.DataTypes.SsoAdminServer[]". At C:\PS_scripts\vCenter\vCenter-adding-AD-LDAPs.ps1:30 char:39

Reproduction steps

1. run scrpt vCenter-adding-AD-LDAPs.ps1
2. Got error on Disconnect-SsoAdminServer -Server $server
3.
...

Expected behavior

should be no error after run Disconnect-ssoadminserver command

Additional context

No response

bwuch commented 2 years ago

Could there be extra lines or incorrect server names in your vcentertestlist.txt text file? I'm able to Connect & Disconnect without any sort of error message.

PS C:\Users\Administrator> Connect-SsoAdminServer -Server vc1.example.com -User administrator@vsphere.local -Password VMware1! -SkipCertificateCheck

Name        : vc1.example.com
ServiceUri  : https://core-vcenter01.lab.enterpriseadmins.org/sso-adminserver/sdk/vsphere.local
User        : administrator@vsphere.local
Id          : /SsoAdminServer=vsphere.local/administrator@vc1.example.com
IsConnected : True
Client      : VMware.vSphere.SsoAdminClient.SsoAdminClient
RefCount    : 1

PS C:\Users\Administrator> Disconnect-SsoAdminServer -Server vc1.example.com
PS C:\Users\Administrator> $global:DefaultSsoAdminServers

I even made a simple test loop and the Disconnect works fine there too:

foreach ($vcenter in 'vc1.example.com','vc2.example.com') {
  "Attempting to connect to $vcenter"
  $thisConnection = Connect-SsoAdminServer -Server $vcenter -User administrator@vsphere.local -Password VMware1! -SkipCertificateCheck

  "Attempting to disconnect from $vcenter"
  Disconnect-SsoAdminServer -Server $vcenter

  "Done"
  "===================="
}

The output of the loop doesn't display any sort of error:

Attempting to connect to vc1.example.com
Attempting to disconnect from vc1.example.com
Done
====================
Attempting to connect to vc2.example.com
Attempting to disconnect from vc2.example.com
Done
====================
yanlu2 commented 2 years ago

Thanks Brain, It works if I put .trim() behind of $server.

Foreach ($server in $vcenterlist) { $hostname=$server.Split(".")[0] $rptfile=$path+"usrimpt-"+$hostname+".txt" write-host "`nConnecting to vCenter SSO server $server" Connect-SsoAdminServer -Server $server -User $username -Password $passwd -SkipCertificateCheck

Disconnect-SsoAdminServer -Server $server.Trim()

}

And I more like the answer at #533,

Foreach ($server in $vcenterlist) { $hostname=$server.Split(".")[0] $rptfile=$path+"usrimpt-"+$hostname+".txt" write-host "`nConnecting to vCenter SSO server $server" $serverConnection = Connect-SsoAdminServer -Server $server -User $username -Password $passwd -SkipCertificateCheck

Disconnect-SsoAdminServer -Server $serverConnection

}

Many thanks,

Regards, -Yan