Hi
Any procedure/powercli to change VMNIC adapter to VMXNET3 without replacing MAC address?
as far as i know, the only way from vCenter client is to remove adapter and re-add with different type. Keep me updated when there is dynamic way to do it.
Hi,
you will have to do this in multiple steps, as you can't just change the adapter type:
1. save the current MAC
2. remove adapter
3. add VMXNET3 adapter
4. add the old MAC
Tim
You can change the type of the NIC with the Set-NetworkAdapter cmdlet.
Unfortunately the MacAddress parameter on that cmdlet can't always be used due to the MAC address range the cmdlet accepts.
For that use Matt's script in Setting MAC Address for VM NICs using PowerShell
You will of course first have to retrieve the current MAC address
Since the guest OS will most probably see this as a new device, you will have to (re)define the network settings inside the guest OS as well.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I've used this in the past to change from E1000 to VMXNET3 without changing MACs:
Get-VM "vmname" | get-networkAdapter | Where { $_.Type -eq "E1000"} | set-NetworkAdapter -Type "vmxnet3" -NetworkName "porggroupname",.
Regards,
I was able to run that same command on a 2008 guest and in vmware it did get changed to vmxnet3
but windows woud not recognize any difference and still shows e1000
do i need to remove the nic from inside windows?
after i removed the e1000 nic inside windows and did a reboot, windows still shows the e1000 nic
but in vmware its vmxnet3
any idea?
Could it be that you have one or more hidden devices ?
Inside the Windows OS, do the following
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
lucd
I dont see it under devices and I did show the nonpresent devices
The vm used to be on e1000 and I already changed the nic to vmxnet3 with
get-vm xxx | get-networkadapter | set-networkadapter -type vmxnet3 -confirm:$false
Can you try reinstalling the VMware Tools ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes tried uninstalling vmtools and reinstalling
same issue
vmware side shows vmxnet3
windows 2008 shows e1000 without any hidden network devices
And an uninstall of the driver inside the guest OS ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can change NIC type to VMXNET3 via PowerCLI without replacing MAC address:
List VM: SRV1, NIC type:
Get-VM SRV1| get-networkadapter
Change VM: SRV1, NIC type to VMXNET3:
get-vm SRV1 | Get-NetworkAdapter | set-networkadapter -type vmxnet3 -confirm:$false
This procedure preserves the original MAC address, but NIC adapter changes.
Here are more detailed info: http://www.sysadmit.com/2016/03/vmware-cambiar-tipo-de-nic-sin-cambiar-la-MAC.html
If the operating system is Windows, the old NIC hasn’t actually been removed from Windows, it's just hidden.
You must remove it, here is the procedure:
http://www.sysadmit.com/2015/09/windows-eliminar-dispositivos-ocultos.html
We managed to complete this process using the following procedure:
Remove/re-configure any NIC using E1000 adapter in VMWare on VM's ( as they can cause memory leaks )
For each VM affected - perform the following:
1) Before converting the adapter, open CMD and then type this to save the current configuration to the registry: "c:\Program Files\VMware\VMware Tools\VMUpgradeHelper.bat" /s
Also take a screenshot of the current IP(or IP's) in case the IP changes.
2) Then shutdown your VM
3) Now in PowerCLI, enter this to convert the VM (replace with the name of your VM exactly as it appears in vCenter: get-vm <VM Name> | Get-NetworkAdapter | set-networkadapter -type vmxnet3
You will be prompted to confirm with Y or N or A (Yes to All) to convert the adapter. If you have more than one adapter you will be prompted to confirm for each one. Just say N for any you don't want converting!
4) Now, power on your VM, open CMD and enter this to restore your Network Configuration:
"c:\Program Files\VMware\VMware Tools\VMUpgradeHelper.bat" /r
Configure the IP / DNS / Gateway on the adapter(s) as they were originally.
Done.
Hope this helps!
This does not work for me. It does not save the network config to the registry like it is suppose to.
I realize this is a dead thread from years ago, but I scripted a solution. Feel free to edit and correct as necessary for your environment. No guarantees provided... It consists of three different scripts. One that actually changes the NIC, one that reassigns the IP, and one that removes ghost network devices. All scripts made by me except removeghosts.ps1
It's a bit messy but it gets the job done.
$VIServer=<vcenter server here>
Connect-VIServer -Server $VIServer
#Begin script
#Get server list from file
$serverlist = Get-Content <path to file with server list>
#Begin foreach loop - Loops through each server to change NIC Type
foreach ($computer in $serverlist) {
#Checks if computer has E1000 or E1000e NIC Type, if not it skips
if ((Get-VM $computer | Get-NetworkAdapter | Where {$_.Type -like "*E1000*"}) -ne $null) {
#Copies IPConfig and removeghosts.ps1 files to C Drive
## IPConfig.PS1 reconfigures IP Settings after NIC change
## removeghosts.ps1 removes devices with Intel in friendlyname - old NICs
## Paths below are a temp location, can be anything you want
Copy-Item -Path C:\IPConfig.ps1 -Destination \\$computer\c$\ -Force
Copy-Item -Path C:\scripts\removeghosts.ps1 -Destination \\$computer\c$ -Force
#Save DNS and IP Settings to files on local C to be restored further in script
try {
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer | where{$_.IPEnabled -eq $true} -ErrorAction Stop
}
catch {
$ErrorMsg = "Unable to get WMI Object IP Info from $Computer, skipping... Change NIC and restore IP manually"
$ErrorMsg | Out-File C:\scripts\ErroredVMs.txt -Append -Force
}
Foreach($NIC in $NICs) {
$ip = ($NIC.IPAddress[0]) | Out-File "\\$computer\c$\ip.txt"
$gateway = $NIC.DefaultIPGateway | Out-File "\\$computer\c$\gateway.txt"
$subnet = $NIC.IPSubnet[0] | Out-File "\\$computer\c$\subnet.txt"
$dns = $NIC.DNSServerSearchOrder | Out-File "\\$computer\c$\dns.txt"
}
#Shutdown VM, wait for shutdown verification, change NIC, start VM
Shutdown-VMGuest -VM "$Computer" -Confirm:$false
$VMState = Get-VM -Name $computer | Select-Object -ExpandProperty PowerState
while ( $VMState -eq "PoweredOn") {
Sleep 15
$VMState = Get-VM -Name $computer | Select-Object -ExpandProperty PowerState
}
try {
Get-VM "$computer" | get-networkAdapter | Where { $_.Type -like "*E1000*"} | set-NetworkAdapter -Type "vmxnet3" -confirm:$false -ErrorAction Stop }
catch {
$ErrorMsg = "Unable to Get-VM Info and change VM NIC on $computer, skipping... Change NIC and restore IP manually"
$ErrorMsg | Out-File C:\scripts\ErroredVMs.txt -Append -Force
}
sleep 5
try {
Start-VM -VM "$Computer" -Confirm:$false -ErrorAction Stop
} catch {
$ErrorMsg = "Unable to start VM on $computer, NIC has been changed but IP Settings have not been restored... Start VM and restore IP manually"
$ErrorMsg | Out-File C:\scripts\ErroredVMs.txt -Append -Force
}
# Check for startup verification, and restore DNS and IP Settings
$vm = Get-View -Viewtype VirtualMachine -Property Name,Guest -Filter @{"name"="$computer"}
$vm.Guest.GuestStateChangeSupported
Sleep 5
while ( $vm.Guest.GuestStateChangeSupported -eq "False" ) {
Sleep 15
$vm = Get-View -Viewtype VirtualMachine -Property Name,Guest -Filter @{"name"="$computer"}
}
try {
Invoke-VMScript -VM "$computer" -GuestCredential (Get-Credential) -ScriptText { C:\IPConfig.ps1 }
} catch {
$ErrorMsg = "Unable to invoke-VMScript to restore IP on $computer, IP Settings have not been restored. Get IP and address manually"
$ErrorMsg | Out-File C:\scripts\ErroredVMs.txt -Append -Force
}
try {
Restart-VMGuest -VM "$computer" -Confirm:$false
} Catch {
$ErrorMsg = "Unable to reboot $computer"
$ErrorMsg | Out-File C:\scripts\ErroredVMs.txt -Append -Force
}
}
else { Write-Host "$computer already has vmxnet3 adapter"}
}
#### IPConfig.ps1 ####
$IP = Get-Content C:\ip.txt
$gateway = Get-Content C:\gateway.txt
$subnet = Get-Content C:\subnet.txt
if ($subnet -eq "255.255.255.252") { $prefix = 30}
if ($subnet -eq "255.255.255.248") { $prefix = 29}
if ($subnet -eq "255.255.255.240") { $prefix = 28}
if ($subnet -eq "255.255.255.224") { $prefix = 27}
if ($subnet -eq "255.255.255.192") { $prefix = 26}
if ($subnet -eq "255.255.255.128") { $prefix = 25}
if ($subnet -eq "255.255.255.0") { $prefix = 24}
if ($subnet -eq "255.255.254.0") { $prefix = 23}
if ($subnet -eq "255.255.252.0") { $prefix = 22}
if ($subnet -eq "255.255.248.0") { $prefix = 21}
if ($subnet -eq "255.255.240.0") { $prefix = 20}
if ($subnet -eq "255.255.224.0") { $prefix = 19}
if ($subnet -eq "255.255.192.0") { $prefix = 18}
if ($subnet -eq "255.255.128.0") { $prefix = 17}
if ($subnet -eq "255.255.0.0") { $prefix = 16}
New-NetIPAddress -InterfaceAlias $alias -IPAddress $IP -PrefixLength $prefix -DefaultGateway $gateway
Set-DnsClientServerAddress -InterfaceAlias $alias -ServerAddress ("<Your DNS Server IPs here>")
C:\removeghosts.ps1 -NarrowbyFriendlyName Intel -Force
####RemoveGhosts.ps1####
.SYNOPSIS
Removes ghost devices from your system
.DESCRIPTION
This script will remove ghost devices from your system. These are devices that are present but have an "InstallState" as false. These devices are typically shown as 'faded'
in Device Manager, when you select "Show hidden and devices" from the view menu. This script has been tested on Windows 2008 R2 SP2 with PowerShell 3.0, 5.1, Server 2012R2
with Powershell 4.0 and Windows 10 Pro with Powershell 5.1. There is no warranty with this script. Please use cautiously as removing devices is a destructive process without
an undo.
.PARAMETER filterByFriendlyName
This parameter will exclude devices that match the partial name provided. This paramater needs to be specified in an array format for all the friendly names you want to be excluded.
"Intel" will match "Intel(R) Xeon(R) CPU E5-2680 0 @ 2.70GHz". "Loop" will match "Microsoft Loopback Adapter".
.PARAMETER narrowByFriendlyName
This parameter will include devices that match the partial name provided. This paramater needs to be specified in an array format for all the friendly names you want to be included.
"Intel" will match "Intel(R) Xeon(R) CPU E5-2680 0 @ 2.70GHz". "Loop" will match "Microsoft Loopback Adapter".
.PARAMETER filterByClass
This parameter will exclude devices that match the class name provided. This paramater needs to be specified in an array format for all the class names you want to be excluded.
This is an exact string match so "Disk" will not match "DiskDrive".
.PARAMETER narrowByClass
This parameter will include devices that match the class name provided. This paramater needs to be specified in an array format for all the class names you want to be included.
This is an exact string match so "Disk" will not match "DiskDrive".
.PARAMETER listDevicesOnly
listDevicesOnly will output a table of all devices found in this system.
.PARAMETER listGhostDevicesOnly
listGhostDevicesOnly will output a table of all 'ghost' devices found in this system.
.PARAMETER force
If specified, each matching device will be removed WITHOUT any confirmation!
.EXAMPLE
Lists all devices
. "removeGhosts.ps1" -listDevicesOnly
.EXAMPLE
Save the list of devices as an object
$Devices = . "removeGhosts.ps1" -listDevicesOnly
.EXAMPLE
Lists all 'ghost' devices
. "removeGhosts.ps1" -listGhostDevicesOnly
.EXAMPLE
Lists all 'ghost' devices with a class of "Net"
. "removeGhosts.ps1" -listGhostDevicesOnly -narrowByClass Net
.EXAMPLE
Lists all 'ghost' devices with a class of "Net" AND a friendly name matching "Realtek"
. "removeGhosts.ps1" -listGhostDevicesOnly -narrowbyfriendlyname Realtek -narrowbyclass Net
.EXAMPLE
Save the list of 'ghost' devices as an object
$ghostDevices = . "removeGhosts.ps1" -listGhostDevicesOnly
.EXAMPLE
Remove all ghost devices EXCEPT any devices that have "Intel" or "Citrix" in their friendly name
. "removeGhosts.ps1" -filterByFriendlyName @("Intel","Citrix")
.EXAMPLE
Remove all ghost devices that have "Intel" in their friendly name
. "removeGhosts.ps1" -narrowByFriendlyName Intel
.EXAMPLE
Remove all ghost devices EXCEPT any devices that are apart of the classes "LegacyDriver" or "Processor"
. "removeGhosts.ps1" -filterByClass @("LegacyDriver","Processor")
.EXAMPLE
Remove all ghost devices EXCEPT for devices with a friendly name of "Intel" or "Citrix" or with a class of "LegacyDriver" or "Processor"
. "removeGhosts.ps1" -filterByClass @("LegacyDriver","Processor") -filterByFriendlyName @("Intel","Citrix")
.EXAMPLE
Remove all ghost network devices i.e. the ones with a class of "Net"
. "removeGhosts.ps1" -narrowByClass Net
.EXAMPLE
Remove all ghost devices without confirmation
. "removeGhosts.ps1" -Force
.NOTES
Permission level has not been tested. It is assumed you will need to have sufficient rights to uninstall devices from device manager for this script to run properly.
#>
Param(
[array]$FilterByClass,
[array]$NarrowByClass,
[array]$FilterByFriendlyName,
[array]$NarrowByFriendlyName,
[switch]$listDevicesOnly,
[switch]$listGhostDevicesOnly,
[switch]$Force
)
#parameter futzing
$removeDevices = $true
if ($FilterByClass -ne $null) {
write-host "FilterByClass: $FilterByClass"
}
if ($NarrowByClass -ne $null) {
write-host "NarrowByClass: $NarrowByClass"
}
if ($FilterByFriendlyName -ne $null) {
write-host "FilterByFriendlyName: $FilterByFriendlyName"
}
if ($NarrowByFriendlyName -ne $null) {
write-host "NarrowByFriendlyName: $NarrowByFriendlyName"
}
if ($listDevicesOnly -eq $true) {
write-host "List devices without removal: $listDevicesOnly"
$removeDevices = $false
}
if ($listGhostDevicesOnly -eq $true) {
write-host "List ghost devices without removal: $listGhostDevicesOnly"
$removeDevices = $false
}
if ($Force -eq $true) {
write-host "Each removal will happen without any confirmation: $Force"
}
function Filter-Device {
Param (
[System.Object]$dev
)
$Class = $dev.Class
$FriendlyName = $dev.FriendlyName
$matchFilter = $false
if (($matchFilter -eq $false) -and ($FilterByClass -ne $null)) {
foreach ($ClassFilter in $FilterByClass) {
if ($ClassFilter -eq $Class) {
Write-verbose "Class filter match $ClassFilter, skipping"
$matchFilter = $true
break
}
}
}
if (($matchFilter -eq $false) -and ($NarrowByClass -ne $null)) {
$shouldInclude = $false
foreach ($ClassFilter in $NarrowByClass) {
if ($ClassFilter -eq $Class) {
$shouldInclude = $true
break
}
}
$matchFilter = !$shouldInclude
}
if (($matchFilter -eq $false) -and ($FilterByFriendlyName -ne $null)) {
foreach ($FriendlyNameFilter in $FilterByFriendlyName) {
if ($FriendlyName -like '*'+$FriendlyNameFilter+'*') {
Write-verbose "FriendlyName filter match $FriendlyName, skipping"
$matchFilter = $true
break
}
}
}
if (($matchFilter -eq $false) -and ($NarrowByFriendlyName -ne $null)) {
$shouldInclude = $false
foreach ($FriendlyNameFilter in $NarrowByFriendlyName) {
if ($FriendlyName -like '*'+$FriendlyNameFilter+'*') {
$shouldInclude = $true
break
}
}
$matchFilter = !$shouldInclude
}
return $matchFilter
}
function Filter-Devices {
Param (
[array]$devices
)
$filteredDevices = @()
foreach ($dev in $devices) {
$matchFilter = Filter-Device -Dev $dev
if ($matchFilter -eq $false) {
$filteredDevices += @($dev)
}
}
return $filteredDevices
}
function Get-Ghost-Devices {
Param (
[array]$devices
)
return ($devices | where {$_.InstallState -eq $false} | sort -Property FriendlyName)
}
# NOTE: White spaces are important in $setupapi for some reason!
$setupapi = @"
using System;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;
namespace Win32
{
public static class SetupApi
{
// 1st form using a ClassGUID only, with Enumerator = IntPtr.Zero
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetupDiGetClassDevs(
ref Guid ClassGuid,
IntPtr Enumerator,
IntPtr hwndParent,
int Flags
);
// 2nd form uses an Enumerator only, with ClassGUID = IntPtr.Zero
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetupDiGetClassDevs(
IntPtr ClassGuid,
string Enumerator,
IntPtr hwndParent,
int Flags
);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupDiEnumDeviceInfo(
IntPtr DeviceInfoSet,
uint MemberIndex,
ref SP_DEVINFO_DATA DeviceInfoData
);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern bool SetupDiDestroyDeviceInfoList(
IntPtr DeviceInfoSet
);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupDiGetDeviceRegistryProperty(
IntPtr deviceInfoSet,
ref SP_DEVINFO_DATA deviceInfoData,
uint property,
out UInt32 propertyRegDataType,
byte[] propertyBuffer,
uint propertyBufferSize,
out UInt32 requiredSize
);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool SetupDiGetDeviceInstanceId(
IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
StringBuilder DeviceInstanceId,
int DeviceInstanceIdSize,
out int RequiredSize
);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupDiRemoveDevice(IntPtr DeviceInfoSet,ref SP_DEVINFO_DATA DeviceInfoData);
}
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVINFO_DATA
{
public uint cbSize;
public Guid classGuid;
public uint devInst;
public IntPtr reserved;
}
[Flags]
public enum DiGetClassFlags : uint
{
DIGCF_DEFAULT = 0x00000001, // only valid with DIGCF_DEVICEINTERFACE
DIGCF_PRESENT = 0x00000002,
DIGCF_ALLCLASSES = 0x00000004,
DIGCF_PROFILE = 0x00000008,
DIGCF_DEVICEINTERFACE = 0x00000010,
}
public enum SetupDiGetDeviceRegistryPropertyEnum : uint
{
SPDRP_DEVICEDESC = 0x00000000, // DeviceDesc (R/W)
SPDRP_HARDWAREID = 0x00000001, // HardwareID (R/W)
SPDRP_COMPATIBLEIDS = 0x00000002, // CompatibleIDs (R/W)
SPDRP_UNUSED0 = 0x00000003, // unused
SPDRP_SERVICE = 0x00000004, // Service (R/W)
SPDRP_UNUSED1 = 0x00000005, // unused
SPDRP_UNUSED2 = 0x00000006, // unused
SPDRP_CLASS = 0x00000007, // Class (R--tied to ClassGUID)
SPDRP_CLASSGUID = 0x00000008, // ClassGUID (R/W)
SPDRP_DRIVER = 0x00000009, // Driver (R/W)
SPDRP_CONFIGFLAGS = 0x0000000A, // ConfigFlags (R/W)
SPDRP_MFG = 0x0000000B, // Mfg (R/W)
SPDRP_FRIENDLYNAME = 0x0000000C, // FriendlyName (R/W)
SPDRP_LOCATION_INFORMATION = 0x0000000D, // LocationInformation (R/W)
SPDRP_PHYSICAL_DEVICE_OBJECT_NAME = 0x0000000E, // PhysicalDeviceObjectName (R)
SPDRP_CAPABILITIES = 0x0000000F, // Capabilities (R)
SPDRP_UI_NUMBER = 0x00000010, // UiNumber (R)
SPDRP_UPPERFILTERS = 0x00000011, // UpperFilters (R/W)
SPDRP_LOWERFILTERS = 0x00000012, // LowerFilters (R/W)
SPDRP_BUSTYPEGUID = 0x00000013, // BusTypeGUID (R)
SPDRP_LEGACYBUSTYPE = 0x00000014, // LegacyBusType (R)
SPDRP_BUSNUMBER = 0x00000015, // BusNumber (R)
SPDRP_ENUMERATOR_NAME = 0x00000016, // Enumerator Name (R)
SPDRP_SECURITY = 0x00000017, // Security (R/W, binary form)
SPDRP_SECURITY_SDS = 0x00000018, // Security (W, SDS form)
SPDRP_DEVTYPE = 0x00000019, // Device Type (R/W)
SPDRP_EXCLUSIVE = 0x0000001A, // Device is exclusive-access (R/W)
SPDRP_CHARACTERISTICS = 0x0000001B, // Device Characteristics (R/W)
SPDRP_ADDRESS = 0x0000001C, // Device Address (R)
SPDRP_UI_NUMBER_DESC_FORMAT = 0X0000001D, // UiNumberDescFormat (R/W)
SPDRP_DEVICE_POWER_DATA = 0x0000001E, // Device Power Data (R)
SPDRP_REMOVAL_POLICY = 0x0000001F, // Removal Policy (R)
SPDRP_REMOVAL_POLICY_HW_DEFAULT = 0x00000020, // Hardware Removal Policy (R)
SPDRP_REMOVAL_POLICY_OVERRIDE = 0x00000021, // Removal Policy Override (RW)
SPDRP_INSTALL_STATE = 0x00000022, // Device Install State (R)
SPDRP_LOCATION_PATHS = 0x00000023, // Device Location Paths (R)
SPDRP_BASE_CONTAINERID = 0x00000024 // Base ContainerID (R)
}
}
"@
Add-Type -TypeDefinition $setupapi
#Array for all removed devices report
$removeArray = @()
#Array for all devices report
$array = @()
$setupClass = [Guid]::Empty
#Get all devices
$devs = [Win32.SetupApi]::SetupDiGetClassDevs([ref]$setupClass, [IntPtr]::Zero, [IntPtr]::Zero, [Win32.DiGetClassFlags]::DIGCF_ALLCLASSES)
#Initialise Struct to hold device info Data
$devInfo = new-object Win32.SP_DEVINFO_DATA
$devInfo.cbSize = [System.Runtime.InteropServices.Marshal]::SizeOf($devInfo)
#Device Counter
$devCount = 0
#Enumerate Devices
while([Win32.SetupApi]::SetupDiEnumDeviceInfo($devs, $devCount, [ref]$devInfo)) {
#Will contain an enum depending on the type of the registry Property, not used but required for call
$propType = 0
#Buffer is initially null and buffer size 0 so that we can get the required Buffer size first
[byte[]]$propBuffer = $null
$propBufferSize = 0
#Get Buffer size
[Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo, [Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_FRIENDLYNAME, [ref]$propType, $propBuffer, 0, [ref]$propBufferSize) | Out-null
#Initialize Buffer with right size
[byte[]]$propBuffer = New-Object byte[] $propBufferSize
#Get HardwareID
$propTypeHWID = 0
[byte[]]$propBufferHWID = $null
$propBufferSizeHWID = 0
[Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo, [Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_HARDWAREID, [ref]$propTypeHWID, $propBufferHWID, 0, [ref]$propBufferSizeHWID) | Out-null
[byte[]]$propBufferHWID = New-Object byte[] $propBufferSizeHWID
#Get DeviceDesc (this name will be used if no friendly name is found)
$propTypeDD = 0
[byte[]]$propBufferDD = $null
$propBufferSizeDD = 0
[Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo, [Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_DEVICEDESC, [ref]$propTypeDD, $propBufferDD, 0, [ref]$propBufferSizeDD) | Out-null
[byte[]]$propBufferDD = New-Object byte[] $propBufferSizeDD
#Get Install State
$propTypeIS = 0
[byte[]]$propBufferIS = $null
$propBufferSizeIS = 0
[Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo, [Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_INSTALL_STATE, [ref]$propTypeIS, $propBufferIS, 0, [ref]$propBufferSizeIS) | Out-null
[byte[]]$propBufferIS = New-Object byte[] $propBufferSizeIS
#Get Class
$propTypeCLSS = 0
[byte[]]$propBufferCLSS = $null
$propBufferSizeCLSS = 0
[Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo, [Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_CLASS, [ref]$propTypeCLSS, $propBufferCLSS, 0, [ref]$propBufferSizeCLSS) | Out-null
[byte[]]$propBufferCLSS = New-Object byte[] $propBufferSizeCLSS
[Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo,[Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_CLASS, [ref]$propTypeCLSS, $propBufferCLSS, $propBufferSizeCLSS, [ref]$propBufferSizeCLSS) | out-null
$Class = [System.Text.Encoding]::Unicode.GetString($propBufferCLSS)
#Read FriendlyName property into Buffer
if(![Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo,[Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_FRIENDLYNAME, [ref]$propType, $propBuffer, $propBufferSize, [ref]$propBufferSize)){
[Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo,[Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_DEVICEDESC, [ref]$propTypeDD, $propBufferDD, $propBufferSizeDD, [ref]$propBufferSizeDD) | out-null
$FriendlyName = [System.Text.Encoding]::Unicode.GetString($propBufferDD)
#The friendly Name ends with a weird character
if ($FriendlyName.Length -ge 1) {
$FriendlyName = $FriendlyName.Substring(0,$FriendlyName.Length-1)
}
} else {
#Get Unicode String from Buffer
$FriendlyName = [System.Text.Encoding]::Unicode.GetString($propBuffer)
#The friendly Name ends with a weird character
if ($FriendlyName.Length -ge 1) {
$FriendlyName = $FriendlyName.Substring(0,$FriendlyName.Length-1)
}
}
#InstallState returns true or false as an output, not text
$InstallState = [Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo,[Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_INSTALL_STATE, [ref]$propTypeIS, $propBufferIS, $propBufferSizeIS, [ref]$propBufferSizeIS)
# Read HWID property into Buffer
if(![Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo,[Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_HARDWAREID, [ref]$propTypeHWID, $propBufferHWID, $propBufferSizeHWID, [ref]$propBufferSizeHWID)){
#Ignore if Error
$HWID = ""
} else {
#Get Unicode String from Buffer
$HWID = [System.Text.Encoding]::Unicode.GetString($propBufferHWID)
#trim out excess names and take first object
$HWID = $HWID.split([char]0x0000)[0].ToUpper()
}
#all detected devices list
$device = New-Object System.Object
$device | Add-Member -type NoteProperty -name FriendlyName -value $FriendlyName
$device | Add-Member -type NoteProperty -name HWID -value $HWID
$device | Add-Member -type NoteProperty -name InstallState -value $InstallState
$device | Add-Member -type NoteProperty -name Class -value $Class
if ($array.count -le 0) {
#for some reason the script will blow by the first few entries without displaying the output
#this brief pause seems to let the objects get created/displayed so that they are in order.
sleep 1
}
$array += @($device)
<#
We need to execute the filtering at this point because we are in the current device context
where we can execute an action (eg, removal).
InstallState : False == ghosted device
#>
if ($removeDevices -eq $true) {
#we want to remove devices so let's check the filters...
$matchFilter = Filter-Device -Dev $device
if ($InstallState -eq $False) {
if ($matchFilter -eq $false) {
$message = "Attempting to remove device $FriendlyName"
$confirmed = $false
if (!$Force -eq $true) {
$question = 'Are you sure you want to proceed?'
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($message, $question, $choices, 1)
if ($decision -eq 0) {
$confirmed = $true
}
} else {
$confirmed = $true
}
if ($confirmed -eq $true) {
Write-Host $message -ForegroundColor Yellow
$removeObj = New-Object System.Object
$removeObj | Add-Member -type NoteProperty -name FriendlyName -value $FriendlyName
$removeObj | Add-Member -type NoteProperty -name HWID -value $HWID
$removeObj | Add-Member -type NoteProperty -name InstallState -value $InstallState
$removeObj | Add-Member -type NoteProperty -name Class -value $Class
$removeArray += @($removeObj)
if([Win32.SetupApi]::SetupDiRemoveDevice($devs, [ref]$devInfo)){
Write-Host "Removed device $FriendlyName" -ForegroundColor Green
} else {
Write-Host "Failed to remove device $FriendlyName" -ForegroundColor Red
}
} else {
Write-Host "OK, skipped" -ForegroundColor Yellow
}
} else {
write-host "Filter matched. Skipping $FriendlyName" -ForegroundColor Yellow
}
}
}
$devcount++
}
#output objects so you can take the output from the script
if ($listDevicesOnly) {
$allDevices = $array | sort -Property FriendlyName
$filteredDevices = Filter-Devices -Devices $allDevices
$filteredDevices | ft
write-host "Total devices found : $($allDevices.count)"
write-host "Total filtered devices found : $($filteredDevices.count)"
$ghostDevices = Get-Ghost-Devices -Devices $array
$filteredGhostDevices = Filter-Devices -Devices $ghostDevices
write-host "Total ghost devices found : $($ghostDevices.count)"
write-host "Total filtered ghost devices found : $($filteredGhostDevices.count)"
return $filteredDevices | out-null
}
if ($listGhostDevicesOnly) {
$ghostDevices = Get-Ghost-Devices -Devices $array
$filteredGhostDevices = Filter-Devices -Devices $ghostDevices
$filteredGhostDevices | ft
write-host "Total ghost devices found : $($ghostDevices.count)"
write-host "Total filtered ghost devices found : $($filteredGhostDevices.count)"
return $filteredGhostDevices | out-null
}
if ($removeDevices -eq $true) {
write-host "Removed devices:"
$removeArray | sort -Property FriendlyName | ft
write-host "Total removed devices : $($removeArray.count)"
return $removeArray | out-null
}
Thanks for sharing that.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference