(SOLVED) Error running PowerCLI cmdlet Set-NetworkAdapter on PowerShell Universal

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.

Solved it.

The issue is what I suspected, that the Confirmation message is not able to be answered in PSUniversal.

The issue was when I attempted to use the -Confirm parameter to answer it, it is a Switch-type parameter, and needed to be used as -Confirm:$false instead of -Confirm $false. Once I added the colon between them, the Set-NetworkAdapter worked perfectly and changed the port group.

The post that helped lead me to the answer was on stack overflow at https://stackoverflow.com/questions/45691877/why-is-the-value-of-switch-type-parameter-passed-to-a-string-parameter