Hello Team
Can anyone help me with the below code it is getting executed but the VLAN is still seen in the configuration.
$VMs = Get-VM -Name server
$NICs = x-vlan-222
$i = 0
foreach($VM in $VMs){
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$Adapters = Get-NetworkAdapter -VM $VM|?{$_.NetworkName -like "x-vlan-222"}
foreach($Adapter in $Adapters){
if($NICs -contains $Adapter){
$devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devSpec.operation = "remove"
$devSpec.device += $Adapter.ExtensionData
$spec.deviceChange += $devSpec
}
}
$VM.ExtensionData.ReconfigVM_Task($spec)
}
Since you already have the Where-clause there is no need for an additional If.
Also, the -like operator where the right operand has no meta characters (like *) is in fact an -eq.
You could do
$VMs = Get-VM -Name server
$NICs = 'x-vlan-222'
foreach($VM in $VMs){
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
Get-NetworkAdapter -VM $VM | Where-Object{$_.NetworkName -eq "x-vlan-222"} |
ForEach-Object -Process {
$devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devSpec.operation = "remove"
$devSpec.device += $_.ExtensionData
$spec.deviceChange += $devSpec
}
$VM.ExtensionData.ReconfigVM_Task($spec)
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If you mean 2vNICs attached to different portgroups, you could do
$VMs = Get-VM -Name server
$NICs = 'pg1','pg2'
foreach($VM in $VMs){
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
Get-NetworkAdapter -VM $VM | Where-Object{$NICs -contains $_.NetworkName} |
ForEach-Object -Process {
$devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devSpec.operation = "remove"
$devSpec.device += $_.ExtensionData
$spec.deviceChange += $devSpec
}
$VM.ExtensionData.ReconfigVM_Task($spec)
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I have no idea what you are doing in that script?!?
In $NICs you have what I assume is a String with the name of the targetted Portgroup.
Then you select all vNIC on VM that are connected to a Portgroup named 'Dummy'.
Then you compare vNIC objects with the single string in $NICs.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD
Apologies for the typo,I have corrected the script.
I want to remove the network adapter x-vlan-222 from multiple VM's how shall I achieve this, for safety I'm trying it on one VM.
The condition I want is if $NICs matches $Adapters then only the network adapter should be removed.
Please suggest.
$VMs = Get-VM -Name server
$NICs = x-vlan-222
$i = 0
foreach($VM in $VMs){
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$Adapters = Get-NetworkAdapter -VM $VM|?{$_.NetworkName -like "x-vlan-222"}
foreach($Adapter in $Adapters){
if($NICs -contains $Adapter){
$devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devSpec.operation = "remove"
$devSpec.device += $Adapter.ExtensionData
$spec.deviceChange += $devSpec
}
}
$VM.ExtensionData.ReconfigVM_Task($spec)
}
Since you already have the Where-clause there is no need for an additional If.
Also, the -like operator where the right operand has no meta characters (like *) is in fact an -eq.
You could do
$VMs = Get-VM -Name server
$NICs = 'x-vlan-222'
foreach($VM in $VMs){
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
Get-NetworkAdapter -VM $VM | Where-Object{$_.NetworkName -eq "x-vlan-222"} |
ForEach-Object -Process {
$devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devSpec.operation = "remove"
$devSpec.device += $_.ExtensionData
$spec.deviceChange += $devSpec
}
$VM.ExtensionData.ReconfigVM_Task($spec)
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks a lot LucD that worked like a charm.
Hi LucD
Is there any way that I will be able to remove 2 or more network adapter at once using variable $NICs.
Thanks in advance
If you mean 2vNICs attached to different portgroups, you could do
$VMs = Get-VM -Name server
$NICs = 'pg1','pg2'
foreach($VM in $VMs){
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
Get-NetworkAdapter -VM $VM | Where-Object{$NICs -contains $_.NetworkName} |
ForEach-Object -Process {
$devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devSpec.operation = "remove"
$devSpec.device += $_.ExtensionData
$spec.deviceChange += $devSpec
}
$VM.ExtensionData.ReconfigVM_Task($spec)
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yess, that worked.
Thanks a lot.
Hey LucD
How can I capture the difference in VM-reconfiguration of removing the Nics has done (like before & after printing as logs)
Thanks
You can send whatever text you want to a file via the Out-File cmdlet.
Or do you mean something else?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference