VMware Cloud Community
VJ_VMware_111
Enthusiast
Enthusiast
Jump to solution

Urgent PowerCLi help needed

Hello Guys,

I am trying to connect to multiple vCenters with different logins that are securely stored in diff .xml files, as shown below. But it seems i cannot use switch statement output for credential switch. Please help out.

Error:

Connect-VIServer : Cannot bind parameter 'Credential'. Cannot convert the "switch" value of type "System.String" to type "System.Management.Automation.PSCredential".

Script:

$vcenters = get-content "C:\Scripts\VMware_Inventory\vcenters.txt"

$cred = Import-Clixml -Path C:\Scripts\VMware_Inventory\cred.xml

$cred_SAP = Import-Clixml -Path C:\Scripts\VMware_Inventory\cred_SAP.xml

$cred_UNIX = Import-Clixml -Path C:\Scripts\VMware_Inventory\cred_UNIX.xml

ForEach ($vcenter in $vcenters)

{

Connect-VIServer $vcenter -Credential switch ($vcenter) {

vcenter1 {$cred}

vcenter1 {$cred_SAP}

vcenter1 {$cred_UNIX}

}

}

Thanks,

VJ

1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You have to use parenthesis around the switch statement to ensure the switch statement is executed before the Connect-VIserver cmdlet:

ForEach ($vcenter in $vcenters)

{

Connect-VIServer $vcenter -Credential (switch ($vcenter) {

vcenter1 {$cred}

vcenter1 {$cred_SAP}

vcenter1 {$cred_UNIX}

})

}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

2 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You have to use parenthesis around the switch statement to ensure the switch statement is executed before the Connect-VIserver cmdlet:

ForEach ($vcenter in $vcenters)

{

Connect-VIServer $vcenter -Credential (switch ($vcenter) {

vcenter1 {$cred}

vcenter1 {$cred_SAP}

vcenter1 {$cred_UNIX}

})

}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
VJ_VMware_111
Enthusiast
Enthusiast
Jump to solution

Thanks.

0 Kudos