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?
{
$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}
You assign a hash table to the Value of the $sessions hash table.
That means you will have to use double indexing to reach a specific value.
See this simplified example
{
$t = @{
Field1 = 'abc'
}
return $t
}
function f1
{
[hashtable]$h = @{}
$h.T1 = f2
return $h
}
$x = f1
$x['T1']['Field1']
Notice how we use ['T1']['Field1'] to reach the value of the nested hash table entry.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Understand your example you provided. I'm interested in the information in column "Name" on first hashtable.
Wanting to filer it down with a where statement based on the "Name" column in first hashtable. This possible?
$vcsa = $Sessions | Where { $Sessions.Name -like "VC"}
$Sessions
Name Value
---- -----
PSC1Session {vmware-api-session-id}
VC1Session {vmware-api-session-id}
PSC2Session {vmware-api-session-id}
VC2Session {vmware-api-session-id}
You have to include a meta character to handle the rest of the string.
$vcsa = $Sessions | Where { $Sessions.Name -like "VC*"}
Or else use the -match operator
$vcsa = $Sessions | Where { $Sessions.Name -match "VC"}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference