0

I'm trying to use WMI to retrieve network adapter information on remote servers.

I've ran a few Get-WmiObject commands with various degrees of success.

This line works fine:

 Invoke-Command $serverName -ScriptBlock {Get-WmiObject 
 Win32_networkadapterconfiguration | where {$_.Index -eq 7}} 

When I try to introduce a variable to the command it returns nothing:

 $Variable = 7
 Invoke-Command $serverName -ScriptBlock {Get-WmiObject 
 Win32_networkadapterconfiguration | where {$_.Index -eq $Variable}}

Any idea why the command with the variable would fail?

b.Bish
  • 55
  • 2
  • 7
  • 2
    Swap `$Variable` with `$using:Variable` in the `where { }` statement. The reason is that the scriptblock knows nothing about variables in your current scope. The `$using:` scope modifier will make the mapping happen for you. – AdminOfThings Jun 12 '19 at 17:58
  • @AdminOfThings thank you! I'm sorry what exactly does the using keyword do to a variable? – b.Bish Jun 12 '19 at 17:59
  • 1
    See [This Answer](https://stackoverflow.com/a/56451708/11025476) for an explanation. – AdminOfThings Jun 12 '19 at 18:01
  • 2
    You can also avoid the `Invoke-Command` entirely; just do `Get-WMIObject -Class Win32_NetworkAdapterConfiguration -Computer $servername | Where-Object {$_.Index -eq $variable}` - this also bypasses the need to fiddle with `$using:`. – Jeff Zeitlin Jun 12 '19 at 18:02

0 Answers0