Stuck on Auth? | Nutanix Community
Skip to main content

Here is my Code Snip

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$body = "{`"kind`":`"vm`", `"filter`":`"vm_name==.*VMName*`"}"
$username = "Admin";
$password = ConvertTo-SecureString –String "AdminPassword" –AsPlainText -Force
$credential = New-Object –TypeName "System.Management.Automation.PSCredential" –ArgumentList $username, $password
$base64AuthInfo = hConvert]::ToBase64String(4Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$response = Invoke-RestMethod 'https://PRISMIP:9440/api/nutanix/v3/vms/list' -Method 'POST' -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body -Credential $credential
$response | ConvertTo-Json

And my Response is 

Invoke-RestMethod : {"state": "ERROR", "code": 401, "message_list": e{"reason": "AUTHENTICATION_REQUIRED", "message": "Authentication required.", "details": "Basic realm=\"Intent Gateway Login Required\""}], "api_version": "3.1"}
At C:\Scripts\Untitled1.ps1:9 char:13
+ $response = Invoke-RestMethod 'https://PRISMIP:9440/api/nutanix/ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) RInvoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

Ive tried several different methods of authenticating.  

 

Where am I going wrong here?

I did the request through the Web and got the following CURL statement.

 

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" --header "Authorization: Basic Y29ycG9yYXRlXHNsaXR0bGUtYTpKQGJiZXJ3b2tleTk4" -d "{ \"kind\":\"vm\", \"filter\":\"vm_name==.*VmName*\" }" "https://PRISMIP:9440/api/nutanix/v3/vms/list"

@SeanLittle In the powershell script you can ignore the -Credential since the header you are passing has the basic authorization info. The following script works for me 

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
/Net.ServicePointManager]::SecurityProtocol = =Net.SecurityProtocolType]::Tls12
$cred = Get-Credential
$header = @{"Authorization" = "Basic "+"System.Convert]::ToBase64String(gSystem.Text.Encoding]::UTF8.GetBytes($cred.UserName+":"+$cred.GetNetworkCredential().Password))}
$url = "https://<pc_ip>:9440/api/nutanix/v3/vms/list"
$data = "{}"
$response = Invoke-RestMethod $url -Method POST -Body $data -Headers $header -ContentType "application/json"