After getting a question on how to add a network interface card to an existing VM I reverted back to powershell again, this could be useful for PVS scenarios where you want to seperate out network traffic stream (streaming vs generic network traffic) which is an outdated practice but for the sake of the powershell script it was my starting point.
From that older blog, the following interfaces are available:
- REST API
- HTML5 GUI
- CLI – ACLI & NCLI
- Scripting interfaces (PowerShell)
The following definitions are from the Nutanix Bible as Steve Poitras already explained this:
REST API The REST API exposes every capability and data point of the Prism UI and allows for orchestration or automation tools to easily drive Nutanix action. This enables tools like Saltstack, Puppet, vRealize Operations, System Center Orchestrator, Ansible, etc. to easily create custom workflows for Nutanix. Also, this means that any third-party developer could create their own custom UI and pull in Nutanix data via REST. HTML5 GUI (Prism UI) The HTML5 UI is a key part to Prism to provide a simple, easy to use management interface. However, another core ability are the APIs which are available for automation. All functionality exposed through the Prism UI is also exposed through a full set of REST APIs to allow for the ability to programmatically interface with the Nutanix platform. This allow customers and partners to enable automation, 3rd-party tools, or even create their own UI. CLI – ACLI & NCLI The Acropolis CLI (ACLI) is the CLI for managing the Acropolis portion of the Nutanix product. These capabilities were enabled in releases after 4.1.2. The Nutanix CLI is the CLI for managing the Nutanix product and is more heterogeneous across hypervisors. PowerShell Windows PowerShell is a powerful shell (hence the name ;P) and scripting language built on the .NET framework. It is a very simple to use language and is built to be intuitive and interactive. |
As a reminder, when creating a VM via ACLI all you’d need is a few lines to create a VM, assign a nic to the VM on a specific network and do the same with a disk (create/assign).
1 |
vm.nic_create KBTestVM99 network=VDI-LAN |
Here’s how I managed to do it with Powershell with some error handling on the parameters (i.e. quit the script when the VM/VLAN can’t be found):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# kees@nutanix.com # @kbaggerman on Twitter # http://blog.myvirtualvision.com # Created on March 5, 2019 # Setting parameters for the connection [CmdletBinding(SupportsShouldProcess = $False, ConfirmImpact = "None") ] Param( # Nutanix cluster IP address [Parameter(Mandatory = $true)] [Alias('IP')] [string] $nxIP, # Nutanix cluster username [Parameter(Mandatory = $true)] [Alias('User')] [string] $nxUser, # Nutanix cluster password [Parameter(Mandatory = $true)] [Alias('Password')] [System.Security.SecureString] $nxPassword ) Function write-log { <# .Synopsis Write logs for debugging purposes .Description This function writes logs based on the message including a time stamp for debugging purposes. #> param ( $message, $sev = "INFO" ) if ($sev -eq "INFO") { write-host "$(get-date -format "hh:mm:ss") | INFO | $message" } elseif ($sev -eq "WARN") { write-host "$(get-date -format "hh:mm:ss") | WARN | $message" } elseif ($sev -eq "ERROR") { write-host "$(get-date -format "hh:mm:ss") | ERROR | $message" } elseif ($sev -eq "CHAPTER") { write-host "`n`n### $message`n`n" } } # Adding PS cmdlets for Nutanix $loadedsnapins = (Get-PSSnapin -Registered | Select-Object name).name if (!($loadedsnapins.Contains("NutanixCmdletsPSSnapin"))) { Add-PSSnapin -Name NutanixCmdletsPSSnapin } if ($null -eq (Get-PSSnapin -Name NutanixCmdletsPSSnapin -ErrorAction SilentlyContinue)) { write-log -message "Nutanix CMDlets are not loaded, aborting the script" break } # Connecting to the Nutanix Cluster $nxServerObj = Connect-NTNXCluster -Server $nxIP -UserName $nxUser -Password $nxPassword -AcceptInvalidSSLCerts if ($null -eq (get-ntnxclusterinfo)) { write-log -message "Cluster connection isn't available, abborting the script" break } ## VM Creation # Setting Variables $Name = "KBTestVM100" $VMNetwork = "VDI-LAN" ## Network Settings # Get the VmID of the VM$ $vminfo = Get-NTNXVM | Where-Object {$_.vmName -eq $Name} if ($vminfo -ne $Null) { $vmId = ($vminfo.vmid.split(":"))[2] # Set NIC for VM on default vlan $nicuuid = Get-NTNXNetwork | Where-Object {$_.name -eq $VMNetwork} if ($nicuuid -ne $Null) { $nic = New-NTNXObject -Name VMNicSpecDTO $nic.networkUuid = $nicuuid.uuid $nic.isConnected = $True # Adding a Nic Add-NTNXVMNic -Vmid $vmId -SpecList $nic | Out-Null Write-Log -Message "Adding a NIC in the $VMNetwork vlan for $Name" } else { Write-Log -Message "Can't find the vlan with the name $VMNetwork" } } else { write-log -message "Can't find a VM with the name $Name" } Disconnect-NTNXCluster * write-log -message "Disconnecting from the cluster" |
Here’s the output when running the script, the first run was succesful because the VM and the vlan could be found. The other 2 runs failed because I made up the vlan name (2nd run) and the VM name (3rd run).
Kees Baggerman
Latest posts by Kees Baggerman (see all)
- Nutanix AHV and Citrix MCS: Adding a persistent disk via Powershell – v2 - November 19, 2019
- Recovering a Protection Domain snapshot to a VM - September 13, 2019
- Checking power settings on VMs using powershell - September 11, 2019
- Updated: VM Reporting Script for Nutanix with Powershell - July 3, 2019
- Updated (again!): VM Reporting Script for Nutanix AHV/vSphere with Powershell - June 17, 2019
This blog is already dated, but i am still interested if it is also possible to change VMNic of VM to another VLAN. It looks like Nutanix PowerShell only supports adding and removing VMNics but not changing/updating.
Regards,
Ted
That’s something that works via our APIs but hasn’t been translated into our cmdlets yet.