0
votes

I'm trying to export to csv all disks for each computer from my list. I have computers configured with one, tow of tree disks. Now when I export the result of my script to csv file,I get it in formatted as list format:

ComputerName, Disk
Comp1,        C:
Comp1,        D:
Comp2,        C:
Comp2,        D:

I would like to know if it is possible to export this list but as table? If there are more then one disk in computer add a column to the table, something like this:

ComputerName, Disk1, Disk2...
Comp1,        C:,    D:
Comp2,        C:,    D:
Comp3,        C:

Update:

$computers = 'Comp1','Comp2'

$out_csv =@()

foreach ($computer in $computers) {

$disks = Get-WmiObject -Class win32_logicaldisk -ComputerName $computer| select deviceid, volumename


    foreach ($disk in  $disks) {

        $ou = New-Object -TypeName psobject -Property @{

            'Computer' = $computer
            'drive' = $disk.volumename
            'ID' = $disk.deviceid
        }

        $out_csv += $ou
    }
}


$out_csv | Export-Clixml -Path 'C:\Users\Administrator\disk.csv'
2
Your desired results are definitely achievable, but it would help if you posted the scripting you've used to help guide us to provide a useful answer here.Booga Roo
Sure, please see updated postkekimian

2 Answers

0
votes

Ugly but simple

$computers = 'Comp1','Comp2'

$out_csv =@()

$computers | ForEach-Object{

    $ou = "$_"

    Get-WmiObject -Class win32_logicaldisk -ComputerName $_ | ForEach-Object{ $ou += "," + $_.deviceid }

    $out_csv += $ou
}

$out_csv | Out-File 'C:\Users\Administrator\disk.csv'
0
votes

Using the following datas as c:\temp\comp.csv

ComputerName, Disk
Comp1,        C:
Comp1,        D:
Comp2,        C:
Comp2,        D:
Comp3,        C:
Comp3,        D:
Comp3,        E:
Comp4,        C:

You can use :

$comp = Import-Csv C:\temp\comp.txt 
$comp | Group-Object -Property computername | % {$a=$_.name; $_.group | % {$n=0;$d0="";$d1="";$d2=""}{Set-Variable -Name "D$n" -Value $_.disk;$n++}; [pscustomobject]@{ComputerName=$a;d1=$d0;d2=$d1;d3=$d2}} | Export-Csv "c:\temp\comp.csv"

That gives :

#TYPE System.Management.Automation.PSCustomObject
"ComputerName","d1","d2","d3"
"Comp1","C:","D:",""
"Comp2","C:","D:",""
"Comp3","C:","D:","E:"
"Comp4","C:","",""

You can adapt this way to manage the output of WMI CmdLet