I would like to retrieve a spefic network adapter configuration. When I do it with its DeviceID, it works well
Get-CimInstance -Query `
"Associators of {Win32_NetworkAdapter.DeviceID=3} Where ResultClass=Win32_NetworkAdapterConfiguration"
However, when I try with its NetConnectionID
Get-CimInstance -Query `
"Associators of {Win32_NetworkAdapter.NetConnectionID='External'} Where ResultClass=Win32_NetworkAdapterConfiguration"
this throw an invalid path error.
of, I could do this
Get-CimInstance -Query `
"Associators of {Win32_NetworkAdapter.DeviceID=$((Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "NetConnectionID = 'External'").DeviceID)} Where ResultClass=Win32_NetworkAdapterConfiguration"
but the double Get-CimInstance
is actually not optimized
The goal is to renew a DHCP Lease for a specific named adapter
Get-CimInstance -Query `
"Associators of {Win32_NetworkAdapter.NetConnectionID='External'} Where ResultClass=Win32_NetworkAdapterConfiguration" |
Invoke-CimMethod -MethodName RenewDHCPLease
Thanks
CodePudding user response:
As mentioned in the comments, you can only construct CIM/WMI object paths using key properties - and the Win32_NetworkAdapter
class only defines one key property: the DeviceID
.
So yes, you'll need to perform two queries, but I would strongly recommend using the pipeline to construct the operation, as this will allow PowerShell to retrieve the associated configuration for multiple adapters, should the first query return more than one:
$adapterConfigs = Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "NetConnectionID = 'External'" |Get-CimAssociatedInstance -ResultClassName Win32_NetworkAdapterConfiguration