Solved

UEFI vm creation

  • 11 February 2021
  • 6 replies
  • 465 views

Hello All,

 

I have created this great powershell script that makes VM’s in AHV. It has been a journey let me tell you, but I made it through. The one thing I would like to add to this is creating a vm with UEFI boot option. I know this needs to be done at creation and I can create it in the acli with the following:

 

acli vm.create UEFIVM2 uefi_boot=true

 

However; is there a way to do this with powershell? I will look at finding a way to wrap the ssh command above but it sure would be nice if it was just a parameter on the                         new-ntnxvirtualmachine cmdlet

Is there a way I can create the vm with UEFI with powershell?

 

Thanks,

Scott

icon

Best answer by skeeter 18 February 2021, 23:12

View original

This topic has been closed for comments

6 replies

Userlevel 6
Badge +5

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.

 

Badge +1

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

Userlevel 6
Badge +5

Hi Scott,

This works in nutanix dev page. Where does it not work? REST API explorer?

Badge +1

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.

Badge +1

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;
    }
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.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([System.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
 

Userlevel 6
Badge +5

Hi Skeeter,

 

Glad you made it work. Thank you for sharing the final result.