Happy New Year All
I am stuck with below script for removing vNIC from multiple machines, it works fine for one VM (got it from one of Luc's older posts). There is no space at the end of VM name in the text file.
$VMS = Get-Content "c:\servers.txt"
$vm = Get-VM $VMS
Foreach ($vm in $VMS)
{
$nic = $vm.ExtensionData.Config.Hardware.Device | where {$_.DeviceInfo.Label -eq "Network adapter 2"}
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
$dev.operation = "remove"
$dev.Device = $nic
$spec.DeviceChange += $dev
$vm.ExtensionData.ReconfigVM($spec)
}
I get this when I run it (there are 2 VM names in the text file)
You cannot call a method on a null-valued expression.At line:11 char:1
+ $vm.ExtensionData.ReconfigVM($spec)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:11 char:1
+ $vm.ExtensionData.ReconfigVM($spec)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Thank you ...
That all looks ok.
Now let's add a check to verify there is indeed a Network adapter 2 on these VMs.
$tgtNic = 'Network adapter 2'
$vms = Get-Content -Path "c:\servers.txt"
foreach($vm in Get-VM -Name $vms){
$nic = $vm.ExtensionData.Config.Hardware.Device | where {$_.DeviceInfo.Label -eq $tgtNic}
if($nic){
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
$dev.operation = "remove"
$dev.Device = $nic
$spec.DeviceChange += $dev
$vm.ExtensionData.ReconfigVM($spec)
}
else{
Write-Host "VM: $($vm.Name) could not find vNIC $tgtNic"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Can you run the following, and check if it returns the VMs you have entered in the .txt file?
$vms = Get-Content -Path "c:\servers.txt"
Write-Host "Count: $($vms.Count)"
foreach($vm in Get-VM -Name $vms){
Write-Host "VM: $($vm.Name) Type: $($vm.GetType().Name)"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you Luc, yes it does. Here is the output
Count: 2
VM: Testvm Type: VirtualMachineImpl
VM: TestVm2 Type: VirtualMachineImpl
That all looks ok.
Now let's add a check to verify there is indeed a Network adapter 2 on these VMs.
$tgtNic = 'Network adapter 2'
$vms = Get-Content -Path "c:\servers.txt"
foreach($vm in Get-VM -Name $vms){
$nic = $vm.ExtensionData.Config.Hardware.Device | where {$_.DeviceInfo.Label -eq $tgtNic}
if($nic){
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
$dev.operation = "remove"
$dev.Device = $nic
$spec.DeviceChange += $dev
$vm.ExtensionData.ReconfigVM($spec)
}
else{
Write-Host "VM: $($vm.Name) could not find vNIC $tgtNic"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, that worked. Both VMs had Network adapter 2 present and the script was able to remove it from both.
thank you !!