Closed xwing3 closed 3 years ago
I have tried executing the script generated by the provider with pywinrm and it works, above error still occurs when using terraform, code below:
import winrm
ps_script = """$ErrorActionPreference = 'Stop'
Get-Vm | Out-Null
$vmSwitch = '{"Name":"dmx","Notes":"","AllowManagementOS":true,"EmbeddedTeamingEnabled":false,"IovEnabled":false,"PacketDirectEnabled":false,"BandwidthReservationMode":3,"SwitchType":1,"NetAdapterNames":[],"DefaultFlowMinimumBandwidthAbsolute":0,"DefaultFlowMinimumBandwidthWeight":0,"DefaultQueueVmmqEnabled":false,"DefaultQueueVmmqQueuePairs":16,"DefaultQueueVrssEnabled":false}' | ConvertFrom-Json
$minimumBandwidthMode = [Microsoft.HyperV.PowerShell.VMSwitchBandwidthMode]$vmSwitch.BandwidthReservationMode
$switchType = [Microsoft.HyperV.PowerShell.VMSwitchType]$vmSwitch.SwitchType
$NetAdapterNames = @($vmSwitch.NetAdapterNames)
#when EnablePacketDirect=true it seems to throw an exception if EnableIov=true or EnableEmbeddedTeaming=true
$switchObject = Get-VMSwitch | ?{$_.Name -eq $vmSwitch.Name}
if ($switchObject){
throw "Switch already exists - $($vmSwitch.Name)"
}
$NewVmSwitchArgs = @{}
$NewVmSwitchArgs.Name=$vmSwitch.Name
$NewVmSwitchArgs.MinimumBandwidthMode=$minimumBandwidthMode
$NewVmSwitchArgs.EnableEmbeddedTeaming=$vmSwitch.EmbeddedTeamingEnabled
$NewVmSwitchArgs.EnableIov=$vmSwitch.IovEnabled
$NewVmSwitchArgs.EnablePacketDirect=$vmSwitch.PacketDirectEnabled
if ($NetAdapterNames) {
$NewVmSwitchArgs.AllowManagementOS=$vmSwitch.AllowManagementOS
$NewVmSwitchArgs.NetAdapterName=$NetAdapterNames
} else {
$NewVmSwitchArgs.SwitchType=$switchType
#not used unless interface is specified
#-AllowManagementOS $vmSwitch.AllowManagementOS
}
New-VMSwitch @NewVmSwitchArgs
$switchObject = Get-VMSwitch | ?{$_.Name -eq $vmSwitch.Name}
if (!$switchObject){
throw "Switch does not exist - $($vmSwitch.Name)"
}
$SetVmSwitchArgs = @{}
$SetVmSwitchArgs.Name=$vmSwitch.Name
$SetVmSwitchArgs.Notes=$vmSwitch.Notes
if (($minimumBandwidthMode -eq [Microsoft.HyperV.PowerShell.VMSwitchBandwidthMode]::Absolute) -and $switchObject.DefaultFlowMinimumBandwidthAbsolute -ne $vmSwitch.DefaultFlowMinimumBandwidthAbsolute) {
$SetVmSwitchArgs.DefaultFlowMinimumBandwidthAbsolute=$vmSwitch.DefaultFlowMinimumBandwidthAbsolute
}
if ((($minimumBandwidthMode -eq [Microsoft.HyperV.PowerShell.VMSwitchBandwidthMode]::Weight) -or (($minimumBandwidthMode -eq [Microsoft.HyperV.PowerShell.VMSwitchBandwidthMode]::Default) -and (-not ($vmSwitch.IovEnabled)))) -and $switchObject.DefaultFlowMinimumBandwidthWeight -ne $vmSwitch.DefaultFlowMinimumBandwidthWeight) {
$SetVmSwitchArgs.DefaultFlowMinimumBandwidthWeight=$vmSwitch.DefaultFlowMinimumBandwidthWeight
}
$SetVmSwitchArgs.DefaultQueueVmmqEnabled=$vmSwitch.DefaultQueueVmmqEnabled
$SetVmSwitchArgs.DefaultQueueVmmqQueuePairs=$vmSwitch.DefaultQueueVmmqQueuePairs
$SetVmSwitchArgs.DefaultQueueVrssEnabled=$vmSwitch.DefaultQueueVrssEnabled
Set-VMSwitch @SetVmSwitchArgs """
s = winrm.Session('http://10.0.0.97:5985/wsman', auth=('Administrator', 'pass'))
r = s.run_ps(ps_script)
print(r)
Terraform Version 0.12.25
Affected Resource(s) Please list the resources as a list, for example:
hyperv_network_switch
If this issue appears to affect multiple resources, it may be an issue with Terraform's core, so please mention this.
Terraform Configuration Files
Debug Output
Server winrm config output