Hi CMH,
That’s an achievement for sure!
I can’t see anything pointing out in PowerShell Cmdlets Reference. One thought that I have is that you can create a custom cmdlet if the API allows that. I suggest looking at REST APIs from Prism Element (gear icon drop-down) as it’ll give you something to play with. Look at v2/vms/post.
So I have used the Rest API to build a UEFI vm. I produced the following code at this page:
https://www.nutanix.dev/reference/prism_element/v2/api/vms/post-vms-createvm/
$headers=@{}
$headers.Add("content-type", "application/json")
$response = Invoke-WebRequest -Uri 'https://CVMIP:9440/PrismGateway/services/rest/v2.0/vms/' -Method POST -Headers $headers -ContentType 'application/json' -Body '{"allow_live_migrate":true,"boot":{"secure_boot":true,"uefi_boot":true},"name":"UEFITest","memory_mb":2048,"num_vcpus":2}' -Credential $NTNXCred
I am currently getting the following error:
The remote server returned an error: (401) Unauthorized.
In the API explorer my code works. The code above does not. I am using the same account in both instances. Is there something else I am missing?
Thanks,
Scott
Hi Scott,
This works in nutanix dev page. Where does it not work? REST API explorer?
I used the Nutanix Dev page to build the JSON code. I then used the REST API explorer to test the code and confirmed it working. I then used the code above as it just wraps the JSON in PowerShell so I can call it in a script and this is what is failing. I’m assuming there is some extra call that is happening with the REST API for authentication but I haven't figured that part out yet.
I got this to work. Here is the code so if someone like me comes along they do not have to reinvent the wheel.
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;
}
}
"@
rSystem.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
PNet.ServicePointManager]::SecurityProtocol = yNet.SecurityProtocolType]::Tls12
$password = Read-Host -AsSecureString -Prompt "Password"
$NTNXCred = New-Object System.Management.Automation.PsCredential("Username",$password)
$RESTAPIUser = $NTNXCred.UserName
$RESTAPIPassword = $NTNXCred.GetNetworkCredential().Password
$Header = @{
"Authorization" = "Basic "+ System.Convert]::ToBase64String(aSystem.Text.Encoding]::UTF8.GetBytes($RESTAPIUser+":"+$RESTAPIPassword ))}
$Header.Add("content-type", "application/json")
$response = Invoke-WebRequest -Uri 'https://CVMIP:9440/PrismGateway/services/rest/v2.0/vms/' -Method POST -Headers $Header -ContentType 'application/json' -Body '{"allow_live_migrate":true,"boot":{"secure_boot":true,"uefi_boot":true},"name":"UEFITest","memory_mb":2048,"num_vcpus":2}' -UseBasicParsing
Hi Skeeter,
Glad you made it work. Thank you for sharing the final result.