Avatar of ITguy565
ITguy565
Flag for United States of America asked on

Powershell scripting issues "Mac Address List"

Experts,

I am looking for a script that can take a list of hostnames ($mypcs) and provide the IP, MacAddress, and Hostname of each of the computers in question.

I would like this script to also be able to tell when computers were not reachable..

I have been working on the following but haven't gotten it to work property yet and need this information ASAP.


Function Get-Mac { 
    $ComputerName = $mypcs 
    $ErrorActionPreference = 'Stop'
    
    $COlResults = foreach ($Computer in $ComputerName) {     
        invoke-command -ComputerName $computer -Credential $cred -ScriptBlock {
            Try{
                $WMI = gwmi -class "Win32_NetworkAdapterConfiguration" |? IpEnabled -EQ "True" | select PSComputerName, description, ipAddress, MACAddress | FT -AutoSize;
                $WMI        
            }catch {
                    Write-Warning "System is not reachable : $Computer";Continue
             }
            
        
    }

    }
    $COlResults
}
. Get-Mac

Open in new window

PowershellShell Scripting

Avatar of undefined
Last Comment
Jeremy Weisinger

8/22/2022 - Mon
Jeremy Weisinger

What error are you getting? Have you tried connecting over WMI:

$ComputerName = $mypcs 
$ErrorActionPreference = 'Stop'
    
$COlResults = foreach ($Computer in $ComputerName) {     
        Try{
            $WMI = gwmi -class "Win32_NetworkAdapterConfiguration" -ComputerName $Computer|? IpEnabled -EQ "True" | select PSComputerName, description, ipAddress, MACAddress | FT -AutoSize
            $WMI        
        }catch {
            Write-Warning "System is not reachable : $Computer";Continue
        }
    }

Open in new window

ITguy565

ASKER
@jeremy

Thanks for your response:

I started out with code very similar to what you presented. I was getting a lot of RPC errors when running it as you can see here:

wmi : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:3 char:20
+ ...      $WMI = gwmi -class "Win32_NetworkAdapterConfiguration" -Computer ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

gwmi : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:3 char:20
+ ...      $WMI = gwmi -class "Win32_NetworkAdapterConfiguration" -Computer ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

gwmi : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:3 char:20
+ ...      $WMI = gwmi -class "Win32_NetworkAdapterConfiguration" -Computer ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

gwmi : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:3 char:20
+ ...      $WMI = gwmi -class "Win32_NetworkAdapterConfiguration" -Computer ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

That is when I moved to the invoke-command as you can see in my original example above.

When I run the invoke command I get the proper output, but duplicates of that output across the whole command.


Can you present me with a modification of your code above that utilizes the invoke command and runs a break out based on Name, Ipaddress, and Macaddress?


I have a feeling the only way that is going to work is store the information in an object, and then loop the information into array per computer. I just haven't seemed to be able to get it to function properly yet.
Jeremy Weisinger

Sure. Something like this:
$ComputerName = $mypcs 

Function Get-Mac { 
    Param ($computer)
    $ErrorActionPreference = 'Stop'
    invoke-command -ComputerName $computer -ScriptBlock {
        Try{
            gwmi -class "Win32_NetworkAdapterConfiguration" |? IpEnabled -EQ "True" 
        }catch {
            Write-Warning "System is not reachable : $Computer";Continue
        }
    } | select PSComputerName, description, ipAddress, MACAddress
}

$results = foreach($Computer in $ComputerName){Get-Mac -computer $Computer}
$results | ft -AutoSize

Open in new window

I took out the -credential parameter as you shouldn't need it if you're running it with an account that has the proper permissions. But if you need to specify alternate credentials, add the parameter back.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Jeremy Weisinger

Ah, yes. Use oDbA's script. It handles the Invoke-Command properly. It should keep running when it encounters an error connecting to a computer.
ITguy565

ASKER
@Jeremy,

Thanks for your assistance.. You are correct the script that you presented above errored and stopped at the first error and thus didn't log. I appreciate your assistance with this.

@oBdA,

The script that you presented is still running, I will post back with the results as soon as I receive them.. Thanks for your assistance Gentlemen..

I can tell from the error output of your script so far oBdA that it is catching a lot of information, It is also not grabbing some of the computers that I can get by running :

Get-WmiObject -class Win32_NetworkAdapterConfiguration -ComputerName $pc -ErrorAction Stop |
        Where-Object IpEnabled -EQ "True" |
        Select-Object PSComputerName, description, ipAddress, MACAddress

Open in new window

manually which is kinda perplexing but I will know more once the script completes.


An Example of one of the errors are :

"Connecting to remote server SERVERNAME failed with the following error message : WinRM cannot complete the operation. Verify that the specified computer name is valid, that the computer is accessible over the network, and that a firewall exception for the WinRM service is enabled and allows access from this computer. By default, the WinRM firewall exception for public profiles limits access to remote computers within the same local subnet. For more information, see the about_Remote_Troubleshooting Help topic."

I haven't checked the firewall yet on that server.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ITguy565

ASKER
oBdA,

Upon completion of the script, I noticed that it failed to connect to a lot of my servers due to the error listed above.

I removed the invoke-command loop from your script and that appeared to clear up a lot of the issues.

$inFile = 'C:\temp\mypcs.txt'
$outFile = 'C:\temp\mypcs_netInfo.csv'
Get-Content -Path $inFile | ForEach-Object {
	$computerName = $_
    Write-Host "Processing $($computerName) ..."
    
			Try {
				Get-WmiObject -computer $_ -Query "SELECT __Server, Description, IPAddress, MACAddress FROM Win32_NetworkAdapterConfiguration WHERE IpEnabled='True'" -ErrorAction Stop
			} Catch {
				$_ | Select-Object @{n='PSComputerName'; e={$ENV:ComputerName}}, @{n='Error'; e={$_.Exception.Message}}
			}
} | Select-Object PSComputerName, Description, IPAddress, MACAddress, Error | Export-Csv -NoTypeInformation -Path $outFile

Open in new window


The code above allowed me to collect the information I needed, the only problem I see is with the IP address.

The IP now is showing as a "System.string[]" rather than the value associated with it. Once that shows the proper value then I believe I will have all the information I need on this.

I REALLY APPRECIATE your assistance with this, because for some reason, I just couldn't wrap my head around this code.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ITguy565

ASKER
Awesome, I will test momentarily!
ITguy565

ASKER
@jeremy

Thanks for assisting with this question!

@oBdA
You are the MAN! thanks you really saved my butt with this one.
Jeremy Weisinger

oBdA is the man!
Glad to help out a little. :)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes