Automation

 View Only
  • 1.  Remove-NetworkAdapter from multiple VMs

    Posted Dec 27, 2018 06:16 AM

    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 ...



  • 2.  RE: Remove-NetworkAdapter from multiple VMs

    Posted Dec 27, 2018 06:22 AM

    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)"

    }



  • 3.  RE: Remove-NetworkAdapter from multiple VMs

    Posted Dec 27, 2018 06:27 AM

    Thank you Luc, yes it does. Here is the output

    Count: 2

    VM: Testvm  Type: VirtualMachineImpl

    VM: TestVm2  Type: VirtualMachineImpl



  • 4.  RE: Remove-NetworkAdapter from multiple VMs
    Best Answer

    Posted Dec 27, 2018 06:34 AM

    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"

        }

    }



  • 5.  RE: Remove-NetworkAdapter from multiple VMs

    Posted Dec 27, 2018 06:43 AM

    Yes, that worked. Both VMs had Network adapter 2 present and the script was able to remove it from both.

    thank you !!