开发者

list hard drives within excel cell

开发者 https://www.devze.com 2023-03-10 01:06 出处:网络
I\'m trying to create a script to make an hardware inventory of my PCs and exporting it within excel.

I'm trying to create a script to make an hardware inventory of my PCs and exporting it within excel.

I have some problem listing hard drive.

Let's suppose one PC has two partions, C with 10开发者_JAVA技巧0 GB and E with 200 GB. I'd like to put drives within a single cell in this way with a carriage return.

C: 100 GB
E: 200 GB

If I want to create an excel file I can do something like this

$a = New-Object -comobject Excel.Application
$a.Visible = $True
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
$c.Cells.Item(1,1) = "A value in cell A1."

and I know I can query wmi to list hard drive:

gwmi win32_logicaldisk | ? {$_.drivetype -eq 3} | select deviceid,@{Label="Disk GB"; Expression={[math]::truncate($_.Size / 1GB)}}

but I don't know how to get my desired output. Thanks in advance.


http://www.java2s.com/Code/VBA-Excel-Access-Word/File-Path/GetDriveInformation.htm answers you question


How does this work?

$a = New-Object -comobject Excel.Application
$a.Visible = $True
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
$label=gwmi win32_logicaldisk | select deviceid,@{Label="Disk GB";  Expression={"$([math]::truncate($_.Size / 1GB)) GB"}} | ft -auto -HideTableHeaders | out-string
$c.Cells.Item(1,1) = $label


You can iterate over the returned drives from your WMI query and add the deviceid and "Disk GB" results to a string value. Then you can write the resulting string value to the Excel cell. One way to do this would be like this:

$a = New-Object -comobject Excel.Application
$a.Visible = $True
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
$outputstring = ""
foreach ($drive in $driveinfo) {
    $outputstring += "$($drive.deviceid)  $($drive."Disk GB") GB`n"
}
$c.cells.item(1,1) = $outputstring
0

精彩评论

暂无评论...
验证码 换一张
取 消