locked
Powershell will run wmi query but not in a loop RRS feed

  • Question

  • If I run this command I can get the mac address off a pc with no problems.
    Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName computer| Select-Object -Property macaddress
     
    Now if I put a bunch of workstations into a text file and do a get-content it shows how many objects are in there (51). Yet all three of these scripts are failing for me and I cant figure out why. Replacing the computer name above with the variable and it fails. The error is useless to me, say the RPC server is unavailable. Does anyone see a problem?

    foreach ($i in get-content "c:\temp\computers.txt")
    {Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $i} | Select-Object -Property macaddress
     
    get-content c:\temp\computers.txt | foreach-object {Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $_} | Select-Object -Property macaddress
     
    $s = get-content c:\temp\computers.txt
    foreach ($i in $s) {Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $i} | Select-Object -Property macaddress
    • Changed type Bill_Stewart Wednesday, December 4, 2013 11:01 PM User asked a question
    Wednesday, December 4, 2013 10:51 PM

Answers

  •   Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter 'IPEnabled=TRUE' -ComputerName $_.Trim()

    ¯\_(ツ)_/¯

    • Proposed as answer by Mike Laughlin Friday, December 6, 2013 1:23 AM
    • Marked as answer by Bill_Stewart Friday, March 14, 2014 3:43 PM
    Friday, December 6, 2013 12:44 AM

All replies

  • The error "RPC server is unavailable" means that WMI could not connect to the remote computer.

    There are a number of possible causes. Two of the most common causes are 1) the computer is turned off and 2) the computer's firewall is not allowing the connection.

    Bill

    • Proposed as answer by Mike Laughlin Wednesday, December 4, 2013 11:40 PM
    Wednesday, December 4, 2013 11:00 PM
  • Fix WMI on remote machine (firewall, WMI corruption - other)

    Not a scripting issue.

    Also be sure that you are doing this:

    get-content c:\temp\computers.txt | 
         foreach-object{
             Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter 'IPEnabled=TRUE' -ComputerName $_
         } | 
         Select-Object -Property macaddress
      


    ¯\_(ツ)_/¯

    • Proposed as answer by Mike Laughlin Wednesday, December 4, 2013 11:40 PM
    Wednesday, December 4, 2013 11:02 PM
  • Thank you all for your replies. The problem wasn't my script, it was the text file holding the machine names. When I run the query I need to clean up the white space after the machine names.
    Friday, December 6, 2013 12:34 AM
  •   Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter 'IPEnabled=TRUE' -ComputerName $_.Trim()

    ¯\_(ツ)_/¯

    • Proposed as answer by Mike Laughlin Friday, December 6, 2013 1:23 AM
    • Marked as answer by Bill_Stewart Friday, March 14, 2014 3:43 PM
    Friday, December 6, 2013 12:44 AM