Solved

AHV VM Connect and Disconnect NIC with PowerShell

  • 30 March 2020
  • 3 replies
  • 3102 views

Userlevel 2
Badge +3

I have an AHV VM that I would like to disconnect the NIC and connect the NIC in my script.  I need it disconnected while sysprep runs and sets a static IP address then I’m good to connect the NIC again.  (Otherwise it pulls a DHCP address and replaced the hostname with the DHCP address in DNS).

 

I can Get-NTNXVMNIC and see the current status of the NIC but I don’t see a way to change the status (i.e. I don’t find a Set-NTNXVMNIC command).

 

Thanks!

 

-TimG

icon

Best answer by TimGaray 6 April 2020, 17:12

View original

This topic has been closed for comments

3 replies

Userlevel 2
Badge +3

I tried removing the NIC and then adding the NIC with “isConnected = False” in the VMNicSpecDTO but it still started up as connected.

Userlevel 2
Badge +3

I did figure out that I wasn’t actually deleting the VMNIC like I thought.  The -Nicid parameter is actually looking for the MAC address.  Once I used that then I could delete the NIC.  Of course, when you remove the NIC and then add a new one to it then that changes the MAC address.

Userlevel 2
Badge +3

I managed to do this “the hard way” by using REST API.

 

Thanks to:

Failing to execute Set-NTNXVMPowerState from PoSH Script - access denied

leepryor / Nutanix_Powershell

 

$Uri = "https://<PrismClusterElement>:9440/api/nutanix/v2.0/vms/<AHV VMid>/nics/<AHV VM NIC MAC Address>"
$RESTAPIUser = $MyCreds.UserName
$RESTAPIPassword = $MyCreds.GetNetworkCredential().Password
$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($RESTAPIUser+":"+$RESTAPIPassword ))}
$ContentType = "application/json"
$Body = '
{
"nic_spec": {
"is_connected": true,
"network_uuid": "<AHV Network UUID>"
}
}
'
Invoke-RestMethod -Uri $Uri -Body $Body -ContentType $ContentType -Headers $Header -Method "PUT"

 

It then returns the task_uuid and you can see in the web GUI the status of the task.

 

Another big help was the “REST API Explorer Live!” in the web GUI.

 

I can use this for now but I would really like to see a “Set-NTNXVMNIC” cmdlet in future releases.

 

-TimG