Attempting to use the PowerShell Universal server on IIS to run a script that creates a new Virtual Machine in VMware, and sets the Network Adapter to the correct Distributed Switch Port Group, but run into an error when executing Set-NetworkAdapter
on any VM.
Running on Windows Server 2019 Datacenter with PowerShell Version 5.1.17763.1490.
Running against vCenter Server Appliance 6.7 with the latest patches and ESXi 6.5 Hosts.
Running PowerShell Universal 1.4.4 on IIS as Local System account in App Pool, with AD-auth logins set up.
The latest version of PowerCLI module is installed on the server.
Script is working with invoked manually in PowerShell on the server.
Section of code in question:
$myNetworkAdapters = Get-VM $VMName | Get-NetworkAdapter -Name "Network adapter 1"
$myVDPortGroup = Get-VDPortgroup -Name $VMPortGroup
Set-NetworkAdapter -NetworkAdapter $myNetworkAdapters -Portgroup $myVDPortGroup
error message seen in the job output:
[ERR] (Date/timestamp) Set-NetworkAdapter Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Set-NetworkAdapter doesn’t have an Index parameter. If I execute these manually in PowerShell, or run a script which execute these commands, they run fine, but prompt me for a confirmation. If I add -Confirm $False
to the code on PowerShell Universal, I receive instead the following error:
Cannot bind parameter 'NetworkAdapter'. Cannot convert value "False" to type "VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.NetworkAdapter". Error: "Invalid cast from 'System.Boolean' to 'VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.NetworkAdapter'."
I’ve checked the index value of a few VMs using Get-VM $Name | Get-NetworkAdapter "Network Adapter 1" | Select-Object Index
and it returns a null/blank value.
get-vm $vmname |Get-NetworkAdapter |Select-Object name, index
Name index
---- -----
Network adapter 1
Any help would be appreciated.