Reputation: 11
With help from the Internet I've managed to write a basic PowerShell script that collects hardware info and exports it to a CSV file. It gathers:
My script still needs to get HDD info (capacity, freespace, id), IPv4 address and MAC.
$Name = hostname
$Motherboard = Get-WmiObject Win32_BaseBoard
$CPU = Get-WmiObject Win32_Processor
$GPU = Get-WmiObject Win32_VideoController
$RAM = Get-WmiObject Win32_ComputerSystem
$To_File = new-object PSObject -property @{
'Name' = $Name
'Motherboard' = $Motherboard.Product
'CPU' = $CPU.Name
'Cores' = $CPU.NumberOfCores
'GPU' = $GPU.Name
'RAM' = "{0:N2}" -f ($RAM.TotalPhysicalMemory/1GB)
}
$To_File | Select-Object Name, Motherboard, CPU, Cores, GPU, RAM |
Export-Csv G:\$Name.csv
Earlier, I also had a line for getting IP and MAC, but it wasn't working universally. It looked like this:
$IP = gwmi Win32_NetworkAdapterConfiguration | Where ($_.DNSDomain -eq 'lan')
$MAC = gwmi Win32_NetworkAdapterConfiguration | Where ($_.DNSDomain -eq 'lan')
Then I would just use in $To_File
object 'IP' = $IP.IPAddress
and 'MAC' = $MAC.MacAddress
It wasn't good since not all the adapters were named "lan".
For HDD I wrote:
$Disk = Get-WmiObject Win32_LogicalDisk -Filter drivetype=3
$DiskInfo = foreach ($zm in $Disk) {
'ID: ' + $zm.DeviceID
'Capacity: ' + "{0:N2}" -f ($zm.Size/1GB)
'Free Space: ' + "{0:N2}" -f ($zm.FreeSpace/1GB)
}
Since some computers had more than one HDD but I didn't know how I can join these two things together so I could have all that info above and HDD's in one file.
I can't do all that stuff remotely, so the script is on a USB drive and I go to each computer and run it by myself. I still have to do around 20 machines.
I'd also like to know how I can add new rows to an existing CSV file because having 15 CSVs and 'merging' them together is painfull.
Upvotes: 1
Views: 642
Reputation: 3350
Use the -append
command with your export-csv
command in order add new rows with the existing rows. Your command would look like
$To_File | Select-Object Name, Motherboard, CPU, Cores, GPU, RAM |
Export-Csv G:\$Name.csv -NoTypeInformation -Append
Powershell - Export-CSV and Append link will be helpful to you I guess.
Upvotes: 1