We help IT Professionals succeed at work.

Powershell scripting issues "Mac Address List"

ITguy565
ITguy565 asked
on
150 Views
Last Modified: 2018-10-04
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

Comment
Watch Question

Jeremy WeisingerSenior Network Consultant / Engineer
CERTIFIED EXPERT

Commented:
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

CERTIFIED EXPERT

Author

Commented:
@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 WeisingerSenior Network Consultant / Engineer
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Jeremy WeisingerSenior Network Consultant / Engineer
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT

Author

Commented:
@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.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
Awesome, I will test momentarily!
CERTIFIED EXPERT

Author

Commented:
@jeremy

Thanks for assisting with this question!

@oBdA
You are the MAN! thanks you really saved my butt with this one.
Jeremy WeisingerSenior Network Consultant / Engineer
CERTIFIED EXPERT

Commented:
oBdA is the man!
Glad to help out a little. :)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions