NIC Information of servers exported to one text file

Hello, I am trying to get the machine name, with the NIC information such as interfacealias, ipaddress, and a status of about 30 servers in a vlan. I got to 2 basic commands that do it separately, but they either overwrite the text file, and not get it all in one basically… I am thinking of running it using the invoke-command cmdlet…

Invoke-Command -ComputerName (Get-Content C:\PowerShell\servers.txt) -FilePath C:\PowerShell\NicServersStatus.ps1

Content of the .ps1 is something like -

Get-WmiObject -Class Win32_ComputerSystem | ft Domain, Name | out-file -filepath \xxx.xxx.xxx.xxx\c$\test\11.15.2013.txt

and

Get-NetAdapter | Get-NetIPAddress -ea 0 | ft interfacealias, ipaddress | out-file -filepath \xxx.xxx.xxx.xxx\c$\test\11.15.2013.txt

But I am kind of stuck, anyone has any good ideas of how to get it done?

Make sure your Out-File command has the -Append switch parameter on.

Get-WmiObject -Class Win32_ComputerSystem | ft Domain, Name | out-file -filepath \xxx.xxx.xxx.xxx\c$\test\11.15.2013.txt -Append

-VERN

You may run into problems even with the Append switch. The way you’ve set up that command, multiple computers might be trying to write to the same file at the same time, which will fail. It would be more reliable to have your remote script blocks just return objects, and do the file I/O from your local session.

Vern & Dave, thank you for your input.

Dave, could you give me an example of what you think could be a good way of having it done?

Basically, instead of this:

Invoke-Command -ComputerName (Get-Content C:\PowerShell\servers.txt) -ScriptBlock { Get-NetAdapter | Get-NetIPAddress -ea 0 | ft interfacealias, ipaddress | out-file -filepath \\xxx.xxx.xxx.xxx\c$\test\11.15.2013.txt }

It would look more like this:

Invoke-Command -ComputerName (Get-Content C:\PowerShell\servers.txt) -ScriptBlock { Get-NetAdapter | Get-NetIPAddress -ea 0 | Select-Object interfacealias, ipaddress } |
Format-Table |
Out-File -filepath \\xxx.xxx.xxx.xxx\c$\test\11.15.2013.txt

I used a script block instead of FilePath just to demonstrate how the contents of the file would change. Take any calls to Out-File out of the remotely invoked script, so they just return objects. The pipe to Out-File happens locally on your machine.

Ahhh very nice, thank you Dave! Definitely helped me!

How do I also get it to generate MAC address information? Here is what I got so far but the results are coming blank for the MAC -

Invoke-Command -ComputerName (Get-Content C:\test\PowerShell\servers.txt) -ScriptBlock { Get-NetAdapter | Get-NetIPAddress -AddressFamily IPv4 -ea 0 | Select-Object interfacealias, ipaddress, MacAddress} | sort-object -property PSComputerName | format-table PSComputerName, interfacealias, ipaddress, MacAddress -auto | Out-File -filepath \xxx.xxx.xxx.xxx\c$\test\11.15.2013.txt

Are you running PowerShell 4.0 yet? If so, this is a good spot to take advantage of the new common parameter “-PipelineVariable”:

Get-NetAdapter -PipelineVariable adapter |
Get-NetIPAddress -AddressFamily IPv4 -ea 0 |
Select-Object interfacealias, ipaddress, @{Name = 'MacAddress'; Expression = { $adapter.MacAddress } }

If you’re using PowerShell 3.0, you need to put some of this code into a ForEach-Object block to accomplish the same result:

Get-NetAdapter  |
ForEach-Object {
    $adapter = $_
    
    $adapter |
    Get-NetIPAddress -AddressFamily IPv4 -ea 0 |
    Select-Object interfacealias, ipaddress, @{Name = 'MacAddress'; Expression = { $adapter.MacAddress } }
}

I tried this but the MAC is still not generated…

Also, isn’t there a “cleaner” way of coding this in PS 3.0?

[quote=11563]Also, isn’t there a “cleaner” way of coding this in PS 3.0?
[/quote]

Not that I’m aware of. There are small changes you can make, but they all boil down to splitting your pipeline into smaller parts so you can save a reference to the NetAdapter object. I don’t think you can do it all in one pipeline without the -PipelineVariable parameter.

Gotcha, very well, thanks Dave!