0

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

CFou
  • 978
  • 3
  • 13
  • You can only use `Key` properties to resolve instances by path - `DeviceID` is the only such property on Win32_NetworkAdapter. – Mathias R. Jessen Feb 02 '23 at 11:16
  • Ok, Thanks, so the only way is the bad one with 2 `Get-CimInstance` ? – CFou Feb 02 '23 at 11:25
  • Yeah, you can make it slightly more elegant (and natively supporting multiple interfaces) by using the pipeline: `Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "NetConnectionID = 'External'" |Get-CimAssociatedInstance -ResultClassName Win32_NetworkAdapterConfiguration` - but yes, you will need two calls – Mathias R. Jessen Feb 02 '23 at 11:38

1 Answers1

1

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
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206