VMware Cloud Community
PruDev
Contributor
Contributor

script needed to check for a config parameter for a VM

Hi Guys,

I'm looking for a script that will check a config parameter of a group of VMs and return its value. The specific parameter I'm looking for is

devices.hotplug = false

..this basically prevents the virtual NIC from showing up as a removable device and prevents users from accidentally killing their session.

If anyone has this script handy, that would be terrific.

Thanks!

0 Kudos
3 Replies
RvdNieuwendijk
Leadership
Leadership

The next function will check the devices.hotplug config parameter of a VM:

function Get-DevicesHotplug {
  param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [http://VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl|http://VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl] $vm)

  process {
    $Report = "" | Select-Object -Property Name,"devices.hotplug"
    $Report.Name = $vm.Name
    $DevicesHotplug = $vm.ExtensionData.Config.ExtraConfig | Where-Object { $_.Key -eq "devices.hotplug" }
    if ($DevicesHotplug) {
      $Report."devices.hotplug" = $DevicesHotplug.Value
    }
    else {
      $Report."devices.hotplug" = ""
    }
    $Report
  }
}

To check all your virtual machines with this function you can do:

Get-VM | Get-DevicesHotplug

The following function will set the devices.hotplug to false:

function Disable-DevicesHotplug {
  param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [http://VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl|http://VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl] $vm)

  process {
    $vmview = $vm | Get-View
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="devices.hotplug"
    $extra.Value="false"
    $vmConfigSpec.extraconfig += $extra

    $vmview.ReconfigVM($vmConfigSpec)
  }
}

To set the devices.hotplug to false for all your virtual machines you can do:

Get-VM | Disable-DevicesHotplug

The functions use PowerCLI v4.1 type declarations. So be sure to use PowerCLI v4.1.

Because the forum software has troubles with square brackets and the functions use them I attached the functions to the post. Use only the attached functions and not the functions that are included in the text!

Regards, Robert

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
PruDev
Contributor
Contributor

Thanks soo much!!!

You da man!

0 Kudos
ffkhaliljbara
Contributor
Contributor

Hi,

I need to help to execute this script, but I don't know how I do. Please Could you show me how I do to execute this script in PowerCLI?

Thanks

0 Kudos