Create Set-NetworkAdapterOpaqueNetwork.ps1
Code sample provided as part of KB 65149: https://kb.vmware.com/s/article/65149
This commit is contained in:
50
Scripts/Set-NetworkAdapterOpaqueNetwork.ps1
Normal file
50
Scripts/Set-NetworkAdapterOpaqueNetwork.ps1
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
function Set-NetworkAdapterOpaqueNetwork {
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 1)]
|
||||||
|
[VMware.VimAutomation.Types.NetworkAdapter]
|
||||||
|
$NetworkAdapter,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $true, Position = 2)]
|
||||||
|
[string]
|
||||||
|
$OpaqueNetworkName,
|
||||||
|
|
||||||
|
[Parameter()]
|
||||||
|
[switch]
|
||||||
|
$Connected,
|
||||||
|
|
||||||
|
[Parameter()]
|
||||||
|
[switch]
|
||||||
|
$StartConnected
|
||||||
|
)
|
||||||
|
process {
|
||||||
|
$opaqueNetwork = Get-View -ViewType OpaqueNetwork | ? {$_.Name -eq $OpaqueNetworkName}
|
||||||
|
if (-not $opaqueNetwork) {
|
||||||
|
throw "'$OpaqueNetworkName' network not found."
|
||||||
|
}
|
||||||
|
|
||||||
|
$opaqueNetworkBacking = New-Object VMware.Vim.VirtualEthernetCardOpaqueNetworkBackingInfo
|
||||||
|
$opaqueNetworkBacking.OpaqueNetworkId = $opaqueNetwork.Summary.OpaqueNetworkId
|
||||||
|
$opaqueNetworkBacking.OpaqueNetworkType = $opaqueNetwork.Summary.OpaqueNetworkType
|
||||||
|
|
||||||
|
$device = $NetworkAdapter.ExtensionData
|
||||||
|
$device.Backing = $opaqueNetworkBacking
|
||||||
|
|
||||||
|
if ($StartConnected) {
|
||||||
|
$device.Connectable.StartConnected = $true
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($Connected) {
|
||||||
|
$device.Connectable.Connected = $true
|
||||||
|
}
|
||||||
|
|
||||||
|
$spec = New-Object VMware.Vim.VirtualDeviceConfigSpec
|
||||||
|
$spec.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::edit
|
||||||
|
$spec.Device = $device
|
||||||
|
$configSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
|
||||||
|
$configSpec.DeviceChange = @($spec)
|
||||||
|
$NetworkAdapter.Parent.ExtensionData.ReconfigVM($configSpec)
|
||||||
|
|
||||||
|
# Output
|
||||||
|
Get-NetworkAdapter -Id $NetworkAdapter.Id
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user