Goal: Script that produces a .csv file that lists all server names with their IP and MAC addresses<\/p>\n
Current script:<\/p>\n
import-module ActiveDirectory\n\n#Create .csv file with a list of all of our servers\nget-adcomputer -filter * -SearchBase 'ou=servers, ou=firm, dc=domain, dc=com' | select dnshostname | export-csv \"\\\\computer\\c$\\scripts\\servernames.csv\"\n\n#create an array from the csv file\n$servernames = @(import-csv \"C:\\scripts\\servernames.csv\")\n\n#Loop through each element in the array to retrieve Server name, MAC and IP Address\nforeach ( $servername in $servernames )\n{\n\n#Get MAC and IP Address\n$colItems = Get-WmiObject -Class \"Win32_NetworkAdapterConfiguration\" -ComputerName $servername.dnshostname -Filter \"IpEnabled = TRUE\"\n\n#Write info to screen\nwrite-host Server name is: $servername.dnshostname\n\n foreach ($objItem in $colItems)\n {\n write-host MAC Address is: $objItem.MACAddress\n write-host IP Address is: $objItem.IPAddress\n }\n\n}\n<\/code><\/pre>\n
Advertisement
Problem: The above script works great and produces exactly what I want in the shell but I want to instead send this output to a file (.txt, .csv, doesn’t matter). This is where I’m hitting a road block and can’t seem to figure out how to do it. My searches have just confused me as I don’t think I know the proper terms to ask my question.<\/p>\n
My attempt:<\/p>\n
$nameis = \"Server name is: $servername.dnshostname\"\n\n foreach ($objItem in $colItems)\n {\n $macis = \"MAC Address is: $objItem.MACAddress\"\n $ipis = \"IP Address is: $objItem.IPAddress\"\n }\n\n$outarray = @($nameis, $macis, $ipis)\n}\n\n$outarray | export-csv \"\\\\computer\\c$\\scripts\\final.csv\"\n<\/code><\/pre>\nI know i’m not setting my variables correctly here as the values are not what I want but that’s another thing I’m not sure how to do or word it correctly. How would I set the $ipis variable for example so that it’s value shows as: IP Address is: 127.0.0.1?<\/p>","upvoteCount":6,"answerCount":11,"datePublished":"2013-05-14T12:01:12.000Z","author":{"@type":"Person","name":"tysonokeefe5343","url":"https://community.spiceworks.com/u/tysonokeefe5343"},"acceptedAnswer":{"@type":"Answer","text":"
Woo Hoo - I figured it out. I needed to put [string] in front of the $item.ipaddress call.<\/p>\n
Here is my finished script (thanks to you guys!):<\/p>\n
import-module ActiveDirectory\n\n#create .csv file with a list of all of our servers\nget-adcomputer -filter * -SearchBase 'ou=servers, ou=firm, dc=domain, dc=com' | select dnshostname | export-csv \"\\\\computer\\c$\\scripts\\servernames.csv\"\n\n#create an array from the csv file\n$servernames = @(import-csv \"C:\\scripts\\servernames.csv\")\n\n#create array used to capture hostname, mac and ip address\n$outarray = @()\n\n#loop through each element in the array to retrieve Server name, mac and ip Address\nforeach ( $servername in $servernames )\n{\n\n#get mac and ip address\n$colItems = Get-WmiObject -Class \"Win32_NetworkAdapterConfiguration\" -ComputerName $servername.dnshostname -Filter \"IpEnabled = TRUE\"\n\n#populate array with results\n foreach ($item in $colitems)\n {\n $outarray += New-Object PsObject -property @{\n 'Server' = $item.DNSHostName\n 'MAC' = $item.MACAddress\n 'IP' = [string]$item.IPAddress\n }\n } \n}\n\n#export to .csv file\n$outarray | export-csv \\\\computer\\c$\\scripts\\final.csv\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2013-05-14T17:44:32.000Z","url":"https://community.spiceworks.com/t/exporting-arrays-to-a-csv-file/212632/10","author":{"@type":"Person","name":"tysonokeefe5343","url":"https://community.spiceworks.com/u/tysonokeefe5343"}},"suggestedAnswer":[{"@type":"Answer","text":"Goal: Script that produces a .csv file that lists all server names with their IP and MAC addresses<\/p>\n
Current script:<\/p>\n
import-module ActiveDirectory\n\n#Create .csv file with a list of all of our servers\nget-adcomputer -filter * -SearchBase 'ou=servers, ou=firm, dc=domain, dc=com' | select dnshostname | export-csv \"\\\\computer\\c$\\scripts\\servernames.csv\"\n\n#create an array from the csv file\n$servernames = @(import-csv \"C:\\scripts\\servernames.csv\")\n\n#Loop through each element in the array to retrieve Server name, MAC and IP Address\nforeach ( $servername in $servernames )\n{\n\n#Get MAC and IP Address\n$colItems = Get-WmiObject -Class \"Win32_NetworkAdapterConfiguration\" -ComputerName $servername.dnshostname -Filter \"IpEnabled = TRUE\"\n\n#Write info to screen\nwrite-host Server name is: $servername.dnshostname\n\n foreach ($objItem in $colItems)\n {\n write-host MAC Address is: $objItem.MACAddress\n write-host IP Address is: $objItem.IPAddress\n }\n\n}\n<\/code><\/pre>\nProblem: The above script works great and produces exactly what I want in the shell but I want to instead send this output to a file (.txt, .csv, doesn’t matter). This is where I’m hitting a road block and can’t seem to figure out how to do it. My searches have just confused me as I don’t think I know the proper terms to ask my question.<\/p>\n
My attempt:<\/p>\n
$nameis = \"Server name is: $servername.dnshostname\"\n\n foreach ($objItem in $colItems)\n {\n $macis = \"MAC Address is: $objItem.MACAddress\"\n $ipis = \"IP Address is: $objItem.IPAddress\"\n }\n\n$outarray = @($nameis, $macis, $ipis)\n}\n\n$outarray | export-csv \"\\\\computer\\c$\\scripts\\final.csv\"\n<\/code><\/pre>\nI know i’m not setting my variables correctly here as the values are not what I want but that’s another thing I’m not sure how to do or word it correctly. How would I set the $ipis variable for example so that it’s value shows as: IP Address is: 127.0.0.1?<\/p>","upvoteCount":6,"datePublished":"2013-05-14T12:01:12.000Z","url":"https://community.spiceworks.com/t/exporting-arrays-to-a-csv-file/212632/1","author":{"@type":"Person","name":"tysonokeefe5343","url":"https://community.spiceworks.com/u/tysonokeefe5343"}},{"@type":"Answer","text":"
What is it doing now? Is it just showing the last machine queried? If so, you need to have the array add new items each iteration of the loop.<\/p>\n
There’s a command in the back of my head that would send output to the screen and file in one step, but i can’t remember it right now.<\/p>","upvoteCount":0,"datePublished":"2013-05-14T12:31:36.000Z","url":"https://community.spiceworks.com/t/exporting-arrays-to-a-csv-file/212632/2","author":{"@type":"Person","name":"chrisseiter","url":"https://community.spiceworks.com/u/chrisseiter"}},{"@type":"Answer","text":"
I think i just figured out how to do the last part to my question with the following:<\/p>\n
[array]$temp = $servername.dnshostname\n $nameis = \"Server name is: $temp\"\n<\/code><\/pre>\nBut I still need help with exporting it to a .csv<\/p>\n
With the code I posted in my first post, when I try to export it to a .csv file it just lists the length of the array string but not the actual values.<\/p>","upvoteCount":0,"datePublished":"2013-05-14T12:33:49.000Z","url":"https://community.spiceworks.com/t/exporting-arrays-to-a-csv-file/212632/3","author":{"@type":"Person","name":"tysonokeefe5343","url":"https://community.spiceworks.com/u/tysonokeefe5343"}},{"@type":"Answer","text":"
These two lines need to be fixed.<\/p>\n
$macis = \"MAC Address is: $objItem.MACAddress\"\n$ipis = \"IP Address is: $objItem.IPAddress\" \n<\/code><\/pre>\nto be this:<\/p>\n
$macis = \"MAC Address is: $($objItem.MACAddress)\"\n$ipis = \"IP Address is: $($objItem.IPAddress)\" \n<\/code><\/pre>\nInside a string, you need to enclose anything that needs to be expanded in $() or what your going to get is the standard string expansion of (for example) $objItem followed by the string .MACAddress.<\/p>\n
Or, you can do it this way:<\/p>\n
$OutArray = @() \nforeach ($Item in $colItems) { \n $OutArray += New-Object PsObject -Property @{ \n 'NameIs' = $ServerName.DnsHostName \n 'MacIs' = $Item.MACAddress \n 'IPIs' = $Item.IPAddress \n } \n} \n$OutArray | Export-Csv \"\\\\computer\\C$\\scripts\\final.csv\" -NoTypeInformation\n<\/code><\/pre>","upvoteCount":4,"datePublished":"2013-05-14T13:09:18.000Z","url":"https://community.spiceworks.com/t/exporting-arrays-to-a-csv-file/212632/4","author":{"@type":"Person","name":"artb","url":"https://community.spiceworks.com/u/artb"}},{"@type":"Answer","text":"Thanks Art, I was able to accomplish the same thing with my previous post but I like your way better.<\/p>\n
However, i’m now realizing that I don’t even really need those lines sense I want to create a .csv file. It was good with testing the values and their output to the screen but I think 3 columns labled Server, MAC, IP would be better. Still not able to get the output into the .csv file how I want it though. Here’s where I’m at with the foreach loop now:<\/p>\n
#Loop through each element in the array to retrieve Server name, MAC and IP Address\nforeach ( $servername in $servernames )\n{\n\n#Get MAC and IP Address\n$colItems = Get-WmiObject -Class \"Win32_NetworkAdapterConfiguration\" -ComputerName $servername.dnshostname -Filter \"IpEnabled = TRUE\"\n\n$myhash = @{Server = $objItem.DNSHostName; MAC = $objItem.MACAddress; IP = $objItem.IPAddress} \nNew-Object PSobject -property $myhash\n}\n\n$myhash | export-csv \\\\computer\\c$\\scripts\\final.csv\n<\/code><\/pre>\nand I get the following in my .csv file:<\/p>\n
#TYPE<\/span> System.Collections.Hashtable
\nIsReadOnly IsFixedSize IsSynchronized Keys Values SyncRoot Count
\nFALSE FALSE FALSE System.Collections.Hashtable+KeyCollection System.Collections.Hashtable+ValueCollection System.Object 3<\/p>","upvoteCount":0,"datePublished":"2013-05-14T15:47:33.000Z","url":"https://community.spiceworks.com/t/exporting-arrays-to-a-csv-file/212632/5","author":{"@type":"Person","name":"tysonokeefe5343","url":"https://community.spiceworks.com/u/tysonokeefe5343"}},{"@type":"Answer","text":"Building off what ArtB was saying, you’ll want to use the += operator instead of the = as well as use the ‘New-Object’ cmdlet like:<\/p>\n
#Loop through each element in the array to retrieve Server name, MAC and IP Address\n$myhash = @()\nforeach ( $servername in $servernames )\n{\n\t#Get MAC and IP Address\n\t$colItems = Get-WmiObject -Class \"Win32_NetworkAdapterConfiguration\" -ComputerName $servername.dnshostname -Filter \"IpEnabled = TRUE\"\n\n\t$myhash += New-Object PsObject -Property @{'Server' = $colItems.DNSHostName; 'MAC' = $colItems.MACAddress; 'IP' = $colItems.IPAddress} \n\tNew-Object PSobject -property $myhash\n}\n\n$myhash | export-csv \\\\computer\\c$\\scripts\\final.csv\n<\/code><\/pre>\nI also noticed you named the variable objItems instead of colItems.<\/p>","upvoteCount":0,"datePublished":"2013-05-14T16:01:10.000Z","url":"https://community.spiceworks.com/t/exporting-arrays-to-a-csv-file/212632/6","author":{"@type":"Person","name":"anthony","url":"https://community.spiceworks.com/u/anthony"}},{"@type":"Answer","text":"
Tyson: You were very close, but as was just shown, you need to put quote marks around the property names in the hash table. Also, you don’t need to use so many internal variables that only get used once. This is all you really need to get the output the way you want it…<\/p>\n
$OutArray = @()\n#Loop through each element in the array to retrieve Server name, MAC and IP Address\nforeach ( $servername in $servernames ) {\n #Get MAC and IP Address\n $colItems = Get-WmiObject -Class \"Win32_NetworkAdapterConfiguration\" -ComputerName $servername.dnshostname -Filter \"IpEnabled = TRUE\"\n foreach ($Item in $colItems) {\n $OutArray += New-Object PsObject -Property @{\n 'Server' = $ServerName.DnsHostName\n 'MAC' = $Item.MACAddress\n 'IP' = $Item.IPAddress\n }\n }\n}\n$OutArray | Export-Csv \"\\\\computer\\C$\\scripts\\final.csv\" -NoTypeInformation \n<\/code><\/pre>","upvoteCount":0,"datePublished":"2013-05-14T16:52:30.000Z","url":"https://community.spiceworks.com/t/exporting-arrays-to-a-csv-file/212632/7","author":{"@type":"Person","name":"artb","url":"https://community.spiceworks.com/u/artb"}},{"@type":"Answer","text":"Thank you for all the help but my final .csv file just looks like this:<\/p>\n
#TYPE<\/span> System.Management.Automation.PSCustomObject
\nServer MAC IP<\/p>\nIt doesn’t actually fill in anything in the columns.<\/p>\n
Here’s where i’m at with the code:<\/p>\n
#create an array from the csv file\n$servernames = @(import-csv \"C:\\scripts\\servernames.csv\")\n\n#create array used to capture hostname, mac and ip address\n$outarray = @()\n\n#loop through each element in the array to retrieve Server name, mac and ip Address\nforeach ( $servername in $servernames )\n{\n\n#get mac and ip address\n$colItems = Get-WmiObject -Class \"Win32_NetworkAdapterConfiguration\" -ComputerName $servername.dnshostname -Filter \"IpEnabled = TRUE\"\n\n#populate array with results\n foreach ($item in $colitems)\n {\n $outarray += New-Object PsObject -property @{\n 'Server' = $objItem.DNSHostName\n 'MAC' = $objItem.MACAddress\n 'IP' = $objItem.IPAddress\n }\n } \n}\n\n#export to .csv file\n$outarray | export-csv \\\\computer\\c$\\scripts\\final.csv\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2013-05-14T17:23:24.000Z","url":"https://community.spiceworks.com/t/exporting-arrays-to-a-csv-file/212632/8","author":{"@type":"Person","name":"tysonokeefe5343","url":"https://community.spiceworks.com/u/tysonokeefe5343"}},{"@type":"Answer","text":"Okay - I figured this one out. I needed to drop the “obj” and just call item.dnshostname.<\/p>\n
I’m ALMOST there! I’m getting the .csv to output almost exactly how I want it except the IP address shows up as System.String<\/span><\/p>\nExample:<\/p>\n
#TYPE<\/span> System.Management.Automation.PSCustomObject
\nServer MAC IP
\nservername 00:00:00:00:00:00 System.String<\/span>
\nservername2 00:00:00:00:00:00 System.String<\/span><\/p>\nI’m assuming this is because the IP address is a string value and can’t be read into the array that way? or the Array is expecting a string value but the IP address is of a different property? I’m assuming I need to somehow convert the item.ipaddress prior to filling the $outarray?<\/p>","upvoteCount":0,"datePublished":"2013-05-14T17:39:45.000Z","url":"https://community.spiceworks.com/t/exporting-arrays-to-a-csv-file/212632/9","author":{"@type":"Person","name":"tysonokeefe5343","url":"https://community.spiceworks.com/u/tysonokeefe5343"}},{"@type":"Answer","text":"
Thanks, this helped me in one of my scripts by adding the [string] to convert it from an array to a string in the output as a powershell object!<\/p>","upvoteCount":0,"datePublished":"2015-05-18T16:10:31.000Z","url":"https://community.spiceworks.com/t/exporting-arrays-to-a-csv-file/212632/11","author":{"@type":"Person","name":"beaupellowski","url":"https://community.spiceworks.com/u/beaupellowski"}}]}}