Hi Luc ,
can you please provide the simple code to get get windows vm in a cluster using rest api available from swagger 6.5 .
also does this kind of approach needs different kind of authentication to vcenter .
1. There is an error in the construction of the URI.
The line should say
$app_health_URL = $BaseURL,"system" -join '/'
2. Depending on which component you query, you will need different credentials.
The list of VMs is something you asked from the vCenter.
And in the vCenter your AD account obviously has the authority to make that query.
The system health is something you ask from the VAMI, the VCSA appliance.
To be able to do that the account needs to be in the SystemConfiguration.BashShellAdministrators group.
Is your AD account in there?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Using the REST API is a bit getting used to.
Once you get the hang, it becomes easy.
In this case we find the cluster we are looking for.
Then we use this cluster as a filter to find all VMs.
Something like this
$clusterName = 'MyCluster'
Connect-CisServer -Server $vcName | Out-Null
# Get the cluster
$cis = Get-CisService -Name 'com.vmware.vcenter.cluster'
$cluster = $cis.list() | where { $_.Name -eq $clusterName }
# Get all VMs in the cluster
$cis = Get-CisService -Name 'com.vmware.vcenter.vm'
$filter = $cis.Help.list.filter.Create()
$filter.clusters.Add($cluster.cluster)
$cis.list($filter)
Disconnect-CisServer -Server $vcName -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
actually i want to use rest api using powershell invoke-restmethod .
below is what i got from net but for some reason it not working ..... just trying to understand that this is how any rest api is being consumed from powershell .
can you have a look on it ??
###############################################
# Configure the variables below for the vCenter
################################################
$RESTAPIServer = "vcenterserver"
# Prompting for credentials
$Credentials = Get-Credential -Credential $null
$RESTAPIUser = $Credentials.UserName
$Credentials.Password | ConvertFrom-SecureString
$RESTAPIPassword = $Credentials.GetNetworkCredential().password
################################################
# Nothing to configure below this line - Starting the main function of the script
################################################
# Adding certificate exception to prevent API errors
################################################
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
################################################
# Building vCenter API string & invoking REST API
################################################
$BaseAuthURL = "https://" + $RESTAPIServer + "/rest/com/vmware/cis/"
#$BaseAuthURL = "https://" + $RESTAPIServer + "/rest/cis/"
$BaseURL = "https://" + $RESTAPIServer + "/rest/vcenter/"
$vCenterSessionURL = $BaseAuthURL + "session"
$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($RESTAPIUser+":"+$RESTAPIPassword))}
$Type = "application/json"
# Authenticating with API
Try
{
$vCenterSessionResponse = Invoke-RestMethod -Uri $vCenterSessionURL -Headers $Header -Method POST -ContentType $Type
}
Catch
{
$_.Exception.ToString()
$error[0] | Format-List -Force
}
# Extracting the session ID from the response
$vCenterSessionHeader = @{'vmware-api-session-id' = $vCenterSessionResponse.value}
###############################################
# Getting list of VMs
###############################################
$VMListURL = $BaseURL+"vm"
Try
{
$VMListJSON = Invoke-RestMethod -Method Get -Uri $VMListURL -TimeoutSec 100 -Headers $vCenterSessionHeader -ContentType $Type
$VMList = $VMListJSON.value
}
Catch
{
$_.Exception.ToString()
$error[0] | Format-List -Force
}
$VMList | Format-Table -AutoSize
###############################################
# End of script
###############################################
What exactly is not working?
Making the connection, creating the session id or retrieving the VMs?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
it seems it failed while retriving vm url as it says line 55 .
Your script works perfectly in my lab.
Can you try adding the Verbose switch on the Invoke-RestMethod cmdlet?
That way we can see the URI
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
it means its not authenticated are yu using administrator@vsphere.local id??
Yes, I'm using the SSO administrator account.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
well it is not working for me with sso account also.
from the screen shot it appears it stopped @ vm uri .
Where are those 5 lines of HEX characters at the beginning of the output coming from?
I don't see that when I run the script.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Never mind, that's from the ConvertFrom-SecureString cmdlet.
I commented that out.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Out of curiosity, does the earlier code with the CisService work for you?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I am yet to check the full.code but I was able to connect with connect-ciserver using both my domain and sso account.
It's Connect-CisServer, the Connect-CiServer is for Cloud servers.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I meant connect-cisserver .I was.able to consume some vami rest apis.but I thought of directly using it with invoke-restmethod (as joshua mentioned its fast and direct )using swagger provided by vsphere 6.5.
No clue who this Joshua is, and I definitely don't see where Swagger comes into it with Invoke-RestMethod.
Swagger is just a tool that in short allows to define API.
The REST API and documentation are generated from these Swagger files.
Using the REST API via the CisService or via Invoke-RestMethod has nothing to do with Swagger, underneath both methods use the same API
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Joshua is the author of this script.
By swagger or api explorer I wanted to say collection of all rest api arranged in vsphere 6.5.
I thought of using that script to see how we can use rest api using invoke-restmethod .
Anything that has rest is exciting to explore further
Where was this script published?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You might also want to have a look at UNDERSTANDING THE VMWARE REST API INTERFACE.
It goes into the difference between Invoke-RestMethod and Invoke-WebRequest.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference