问题
I am trying to get a list of hard disk information. At the moment I would only want the list to show the model of the hard disk that each computer contains.
I have the below
Get-WMIObject win32_diskdrive -computer (Get-Content C:\Temp\DiskDrives\Computers.txt) | Select model | Export- CSV "C:\Temp\DiskDrives\Machines.csv"
The computers.txt file contains a list of computers that the code goes through one by one. The above works to a point it will list the drives that it can find for a machine but the csv file is not formatted too well.
Can the csv be formatted to include the computer name from the txt file? also to only pick out hard disks and not usb, sd devices etc?
Appreciate any thoughts / advice.
Many Thanks, Chris
回答1:
Just pick out the SystemName property as well, and you'll get a result like this.
Get-WMIObject win32_diskdrive -computer (Get-Content C:\Temp\DiskDrives\Computers.txt) | `
Select SystemName,model
>SystemName Model Size(GB)
---------- ----- --------
BEHEMOTH OCZ-VERTEX PLUS R2 58
BEHEMOTH JetFlash Transcend 32GB USB Device 29
BEHEMOTH INTEL SSDSC2CT180A3 168
BEHEMOTH SAMSUNG HD103SJ 932
BEHEMOTH OCZ-VERTEX 60
BEHEMOTH ST3000DM001-1CH166 2795
BEHEMOTH OCZ-VERTEX PLUS R2 58
BEHEMOTH JetFlash Transcend 32GB USB Device 29
BEHEMOTH INTEL SSDSC2CT180A3 168
BEHEMOTH SAMSUNG HD103SJ 932
BEHEMOTH OCZ-VERTEX 60
BEHEMOTH ST3000DM001-1CH166 2795
As soon as you run a WMI query against more than one machine, you also get the SystemName parameter too, which makes this easy!
Note that in my above result, I've got a small drive called Transcend, which is a 32 GB USB 3 FlashDrive. You mentioned wanting to include only Fixed disks, not USB, so to do that we just add in a Where Statement, like this:
PS C:\> Get-WMIObject win32_diskdrive -ComputerName behemoth,localhost |
Where-Object MediaType -eq 'Fixed hard disk media' | Select SystemName,Model
>SystemName Model Size(GB)
---------- ----- --------
BEHEMOTH OCZ-VERTEX PLUS R2 58
BEHEMOTH INTEL SSDSC2CT180A3 168
BEHEMOTH SAMSUNG HD103SJ 932
BEHEMOTH OCZ-VERTEX 60
BEHEMOTH ST3000DM001-1CH166 2795
BEHEMOTH OCZ-VERTEX PLUS R2 58
BEHEMOTH INTEL SSDSC2CT180A3 168
BEHEMOTH SAMSUNG HD103SJ 932
BEHEMOTH OCZ-VERTEX 60
BEHEMOTH ST3000DM001-1CH166 2795
Bonus points, if you want to show the size of the disk in GB, change your select statement to this, which uses a calculated property to make a new column called Size(GB) and populate it with the $_.Size property divided by 1GB in integer (whole number) form :
Select SystemName,Model,@{Name='Size(GB)';Exp={$_.Size /1gb -as [int]}}`
Completed One-Liner
Simply add your Export-CSV to the end of the pipeline to have a completed one- liner like this which should give you the results you're looking for:
Get-WMIObject win32_diskdrive -computer (Get-Content C:\Temp\DiskDrives\Computers.txt) |
Where-Object MediaType -eq 'Fixed hard disk media' |
Select SystemName,Model,@{Name='Size(GB)';Exp={$_.Size /1gb -as [int]}} |
Export- CSV "C:\Temp\DiskDrives\Machines.csv"
Ahh, the Power of PowerShell.
来源:https://stackoverflow.com/questions/29352300/list-hard-disk-information-on-remote-computers-within-a-domain