Link to home
Start Free TrialLog in
Avatar of ITguy565
ITguy565Flag 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

Avatar of Jeremy Weisinger
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

Avatar of 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.
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.
SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
@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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome, I will test momentarily!
@jeremy

Thanks for assisting with this question!

@oBdA
You are the MAN! thanks you really saved my butt with this one.
oBdA is the man!
Glad to help out a little. :)