Hello Everyone,
I have the below code and I'm getting data back from the function but unable to filter it by calling $Sessions.Name. Goal is to be able to filter between vcenter and other vami devices. Anyone see what I'm doing wrong with my code or know a better way to filter a list?
function GetVAMISessionID{
$BaseAuthURL = "https://" + $ip + "/rest/com/vmware/cis/"
$SessionURL = $BaseAuthURL + "session"
$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($User+':'+$Password))}
$Type = "application/json"
#Authenticate
Try
{
$SessionResponse = Invoke-RestMethod -Uri $SessionURL -Headers $Header -Method POST -ContentType $Type -SkipCertificateCheck
}
Catch
{
$_.Exception.ToString()
$error[0] | Format-List -Force
}
# Extracting the session ID from the response
$SessionHeader = @{'vmware-api-session-id' = $SessionResponse.value}
return $SessionHeader
}
function APIAuth {
[hashtable]$Sessions = @{}
#VC1 API Call
if($VC1IP -AND $VC1Username -AND $VC1Password) {
$User = $VC1Username
$Password = $VC1Password
$IP = $VC1IP
$Sessions.VC1Session = GetVAMISessionID $IP $User $Password
}
#VC2 API Call
if($VC2IP -AND $VC2Username -AND $VC2Password) {
$User = $VC2Username
$Password = $VC2Password
$IP = $VC2IP
$Sessions.VC2Session = GetVAMISessionID $IP $User $Password
}
# PSC1 API Call
if($PSC1IP -AND $PSC1Username -AND $PSC1Password ){
$User = $PSC1Username
$Password = $PSC1Password
$IP = $PSC1IP
$Sessions.PSC1Session = GetVAMISessionID $IP $User $Password
}
# PSC2 API Call
if($PSC2IP -AND $PSC2Username -AND $PSC2Password){
$User = $PSC2Username
$Password = $PSC2Password
$IP = $PSC2IP
$Sessions.PSC2Session = GetVAMISessionID $IP $User $Password
}
write-host $Sessions.Name
Return $Sessions
}
$VC1IP = "xxxxxxxxxxx"
$VC1Username = "administrator@vsphere.local"
$VC1Password = "xxxxxxxxxxxxxxx"
$VC2IP = "xxxxxxxxxxxxx"
$VC2Username = "administrator@vsphere.local"
$VC2Password = "xxxxxxxxxxxxxxxx"
$PSC1IP = "xxxxxxxxxxxxxxxxxxxx"
$PSC1Username = "root"
$PSC1Password = "xxxxxxxxxxxxxxx"
$PSC2IP = "xxxxxxxxxxxxx"
$PSC2Username = "root"
$PSC2Password = "xxxxxxxxxxxxx"
$Sessions = APIAuth $VC1IP $VC1Username $VC1Password $VC2IP $VC2Username $VC2Password $PSC1IP $PSC1Username $PSC1Password $PSC2IP $PSC2Username $PSC2Password
The output from the above code is
$Sessions
Name Value
---- -----
PSC1Session {vmware-api-session-id}
VC1Session {vmware-api-session-id}
PSC2Session {vmware-api-session-id}
VC2Session {vmware-api-session-id}