| Author |
Topic  |
|
|
GeorgeM
Seasoned But Casual Onlooker

USA
67 Posts
Status: offline |
Posted - 03/27/2012 : 3:16:28 PM
|
Hello, I have been trying to get ip addresses to print to csv in a script but can not get it to work. I found that some people are using | foreach{$_.ipaddress = [string]$_.ipaddress to get the IP to prin instead of System.String[] but withinthe script I am using I can't get it to work. I still get System.String[] when I run it. I am not a developer, just a technician so i may be missing something obvious. The script is below. Any help is greatly appreciated.
function Inv { Process{
$CS = gwmi Win32_ComputerSystem -comp $_ $BIOS = gwmi win32_BIOS -comp $_ $OS = gwmi win32_OperatingSystem -comp $_ $NIC = gwmi win32_NetworkAdapterConfiguration -comp $_ | Where-Object {$_.IPEnabled -eq "true"} $NIC | | foreach{$_.ipaddress = [string]$_.ipaddress}
$obj = new-object psobject $obj | add-member noteproperty Name $CS.Name $obj | add-member noteproperty Model $CS.Model $obj | add-member noteproperty RAM $CS.TotalPhysicalMemory $obj | add-member noteproperty User $CS.UserName
$obj | add-member noteproperty SN $BIOS.SerialNumber $obj | add-member noteproperty BIOSv $Bios.SMBIOSBIOSVersion
$obj | add-member noteproperty OS $OS.Caption $obj | add-member noteproperty SP $OS.CSDVersion
$obj | add-member noteproperty IP $NIC.IPAddress $obj | add-member noteproperty MAC $NIC.MACAddress
write-output $obj } }
get-content D:\Inv\testIp.txt | Inv | Export-CSV D:\Inv\TestNic.csv
|
|
|
jhicks
Here To Stay
 
USA
283 Posts
Status: offline |
Posted - 03/27/2012 : 3:32:51 PM
|
First off, you have an extra pipe symbol here:
$NIC | | foreach{$_.ipaddress = [string]$_.ipaddress}
And even then $_ is a string. I think you mean not the WMI object so this isn't going to do anything anyway.
But the primary issue is that the IPAddress value is an array, actuThally $NIC is also most likely an array. You are trying to treat it as a single value which is why the object isn't getting created correctly. And even if it is was, you can't export a multi-value property to a CSV. You really need to use an XML format.
Lastly, when querying WMI, use WMI filtering not Where-Object. $NIC = gwmi win32_NetworkAdapterConfiguration -comp $_ -filter "IPEnabled = 'True'"
I could make many other script suggestions but these are the main issues you have to address.
|
Jeffery Hicks Windows PowerShell MVP
http://jdhitsolutions.com.blog http://twitter.com/JeffHicks http://www.ScriptingGeek.com Now Available: Managing Active Directory with Windows PowerShell: TFM 2nd ed. |
 |
|
|
GeorgeM
Seasoned But Casual Onlooker

USA
67 Posts
Status: offline |
Posted - 03/28/2012 : 08:02:14 AM
|
Thanks Jeff. Scripting is obviously not my thing but this gets me the information I need. Here is what I ended up with and it prints the IP address. Thanks again.
function Inv { Process{
$CS = gwmi Win32_ComputerSystem -comp $_ $BIOS = gwmi win32_BIOS -comp $_ $OS = gwmi win32_OperatingSystem -comp $_ $NIC = gwmi win32_NetworkAdapterConfiguration -comp $_ -filter "IPEnabled = 'true'" $NIC | foreach{$_.ipaddress = [string]$_.ipaddress}
$obj = new-object psobject $obj | add-member noteproperty Name $CS.Name $obj | add-member noteproperty Model $CS.Model $obj | add-member noteproperty RAM $CS.TotalPhysicalMemory $obj | add-member noteproperty User $CS.UserName
$obj | add-member noteproperty SN $BIOS.SerialNumber $obj | add-member noteproperty BIOSv $Bios.SMBIOSBIOSVersion
$obj | add-member noteproperty OS $OS.Caption $obj | add-member noteproperty SP $OS.CSDVersion
$obj | add-member noteproperty IP $NIC.IPAddress $obj | add-member noteproperty MAC $NIC.MACAddress
write-output $obj } }
(get-content D:\Inv\testIp.txt | Inv | ConvertTo-XML).Save("D:\Inv\TestNic3.XML")
|
 |
|
|
jhicks
Here To Stay
 
USA
283 Posts
Status: offline |
Posted - 03/28/2012 : 08:21:59 AM
|
I guess as long as you have something that works for you. This line:
$NIC | foreach{$_.ipaddress = [string]$_.ipaddress}
doesn't really do anything. And I can only see this working if you only have a single adapter. If you query a machine like laptop with an ethernet port and wireless I doubt this will work. But if you only have single NICs, carry on. |
Jeffery Hicks Windows PowerShell MVP
http://jdhitsolutions.com.blog http://twitter.com/JeffHicks http://www.ScriptingGeek.com Now Available: Managing Active Directory with Windows PowerShell: TFM 2nd ed. |
 |
|
|
jhicks
Here To Stay
 
USA
283 Posts
Status: offline |
Posted - 03/28/2012 : 08:32:40 AM
|
This is far from a complete script but should be more robust than what you have been using.
function Get-Inventory {
Param( [Parameter(Position=0,ValueFromPipeline=$True)] [ValidateNotNullOrEmpty()] [string]$Computername=$env:computername)
Process{ $CS = Get-WmiObject Win32_ComputerSystem -comp $Computername $BIOS = Get-WmiObject win32_BIOS -comp $Computername $OS = Get-WmiObject win32_OperatingSystem -comp $Computername $NIC = Get-WmiObject win32_NetworkAdapterConfiguration -comp $Computername -filter "IPEnabled = 'true'"
$obj = new-object psobject $obj | add-member noteproperty Name $CS.Name $obj | add-member noteproperty Model $CS.Model $obj | add-member noteproperty RAM $CS.TotalPhysicalMemory $obj | add-member noteproperty User $CS.UserName
$obj | add-member noteproperty SN $BIOS.SerialNumber $obj | add-member noteproperty BIOSv $Bios.SMBIOSBIOSVersion
$obj | add-member noteproperty OS $OS.Caption $obj | add-member noteproperty SP $OS.CSDVersion
$obj | add-member noteproperty IP ($NIC | Select-Object -ExpandProperty IPAddress) $obj | add-member noteproperty MAC ($NIC | Select-Object -ExpandProperty MACAddress)
write-output $obj } #close process } #close function
This version let's you run the function like a command or pipe names to it. Your version only works with pipelined input. This version you can run like this:
get-inventory server1
or like this for multiple machines:
get-content computers.txt | get-inventory
This version also better handles multiple NICS and IP Addresses.
Name : SERENITY Model : Qosmio X505 RAM : 8577855488 User : SERENITY\Jeff SN : Z9131790W BIOSv : V2.90 OS : Microsoft Windows 7 Ultimate SP : Service Pack 1 IP : {172.16.10.123, 192.168.56.1, fe80::25d8:382a:2191:8cef} MAC : {00:26:9E:C8:09:72, 08:00:27:00:F0:AF}
|
Jeffery Hicks Windows PowerShell MVP
http://jdhitsolutions.com.blog http://twitter.com/JeffHicks http://www.ScriptingGeek.com Now Available: Managing Active Directory with Windows PowerShell: TFM 2nd ed. |
 |
|
|
GeorgeM
Seasoned But Casual Onlooker

USA
67 Posts
Status: offline |
Posted - 03/28/2012 : 09:26:13 AM
|
Jeff, Thanks again! I wouldn't have come up with that on my own. |
 |
|
|
JSCLMEDAVE
Administrator
    
USA
6113 Posts
Status: offline |
Posted - 03/28/2012 : 09:55:18 AM
|
Jeff I can follow "most" of this without issue. However, I was REALLY expecting a For-Each to be included somewhere.
|
Tim-
“This too shall pass" |
 |
|
|
jhicks
Here To Stay
 
USA
283 Posts
Status: offline |
Posted - 03/28/2012 : 10:27:23 AM
|
You don't need a ForEach. I'm taking advantage of the pipeline. A line like this
$obj | add-member noteproperty IP ($NIC | Select-Object -ExpandProperty IPAddress)
is sort of using ForEach behind the scenes. The code in parentheses says take the $NIC object and pipe it to Select-Object. Then for each NIC select the IPAddress property but instead of writing it as an object, just write the IPAddress value to the pipeline. In my case this will be several IP addresses which is why I end up with an array. |
Jeffery Hicks Windows PowerShell MVP
http://jdhitsolutions.com.blog http://twitter.com/JeffHicks http://www.ScriptingGeek.com Now Available: Managing Active Directory with Windows PowerShell: TFM 2nd ed. |
 |
|
|
JSCLMEDAVE
Administrator
    
USA
6113 Posts
Status: offline |
Posted - 03/28/2012 : 3:40:46 PM
|
| Awesome!! The Lead Tech's where I am currently at knew exactly why you were doing this. The pipeline is so much for efficient than using a bunch of For-Each when ever possible. They even mentioned that Don had said coders that user a lot of For-Each should be shot. <g> |
Tim-
“This too shall pass" |
 |
|
|
JSCLMEDAVE
Administrator
    
USA
6113 Posts
Status: offline |
Posted - 03/29/2012 : 11:51:04 AM
|
From my co-worker Chris Duck http://blog.whatsupduck.net/
Where you "were" expecting a for-each loop would be in these lines:
$obj | add-member noteproperty IP ($NIC | Select-Object -ExpandProperty IPAddress) $obj | add-member noteproperty MAC ($NIC | Select-Object -ExpandProperty MACAddress)
And the reason you don't see one is that he is creating a property called "IP" and setting that equal to the array of IP addresses. Those lines could also be written (I think more clearly) like this:
$obj | add-member noteproperty IP $NIC.IPAddress $obj | add-member noteproperty MAC $NIC.MACAddress
If you look at the sample output in the forum post from Jeff, you will notice the IP and MAC properties are showing using the array {} notation:
IP : {172.16.10.123, 192.168.56.1, fe80::25d8:382a:2191:8cef} MAC : {00:26:9E:C8:09:72, 08:00:27:00:F0:AF}
Another alternative that is a little closer to what the original poster intended (and coincidentally is something we covered in class today) would be this:
$obj | add-member noteproperty IP [string]::Join(", ", $NIC.IPAddress) $obj | add-member noteproperty MAC [string]::Join(", ", $NIC.MACAddress)
This would produce IP and MAC properties with a single string value that is the result of joining the individual array elements with ", ", which would look like this:
IP : 172.16.10.123, 192.168.56.1, fe80::25d8:382a:2191:8cef MAC : 00:26:9E:C8:09:72, 08:00:27:00:F0:AF
Did that help any?
Chris Duck
YES IT DID! <g> |
Tim-
“This too shall pass" |
 |
|
|
jhicks
Here To Stay
 
USA
283 Posts
Status: offline |
Posted - 03/29/2012 : 1:27:35 PM
|
I think some clarity is called for here: Doing *anything* with $NIC.IPAddress only works if $NIC is a single object. Joining and the rest will work if IPAddress is multi-valued, but if $NIC returns multiple objects these techniques fail. In my setup, $NIC returns objects.
PS C:\> $nic.count 2
So I can't use $nic.IPAddress. I'd have to specify with object.
PS C:\> $nic.IPAddress PS C:\> $nic[0].IPAddress 172.16.10.123
That's why I use the technique I do in my version of the function.
|
Jeffery Hicks Windows PowerShell MVP
http://jdhitsolutions.com.blog http://twitter.com/JeffHicks http://www.ScriptingGeek.com Now Available: Managing Active Directory with Windows PowerShell: TFM 2nd ed. |
 |
|
|
JSCLMEDAVE
Administrator
    
USA
6113 Posts
Status: offline |
Posted - 03/29/2012 : 3:19:02 PM
|
Correct Jeff!
The confusion on "my part" to Chris was that I was working on the bases of pulling the IP from the local host = single active NIC.
Apologies. |
Tim-
“This too shall pass" |
 |
|
| |
Topic  |
|