Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

exporting server volume information to csv file

$
0
0
 

i have a working powershell script which searches active directory for a list of servers, then connects to each server and collects volume statistics and displays the results on-screen.  i would like to export the results into a csv file for trending analysis, this is where my powershell programming knowledge runs out.

i think i understand logically that i need to introduce a blank array (which array type, unsure), then loop through the list of servers 'appending' the individual volume information to the array, then upon completion of the loop, utilize the export-csv cmdlet to export the volume information collected in the array to a csv file.  i've looked online at many examples, i think i need to introduce another variable or powershell object to handle the appending (+=) of the information into the array, but i think i've researched myself into a coma. when i run the script trying to add the export-csv command either inside the loop or after loop completion, i either get an empty csv file or "gobbledy gook".  as a secondary component, collecting the information in an array would then allow me to run the script, export-csv trending data and then i could introduce a where clause at the end allowing me to display/export-csv/export-html any volume that meets a logical threshold (say <10% freespace).

any and all help is appreciated !!

script:

$servers = Get-ADComputer -SearchBase 'OU=Servers,DC=company,DC=com' -Filter { OperatingSystem -Like '*Windows Server*' } | sort name
$results = @()

Foreach ($server in $servers) {
    Get-WmiObject Win32_Volume -computername $server.name | 
        Where {$_.drivetype -eq 3} |
        Where {$_.label -ne "System Reserved"} |
        Select pscomputername, `
            @{"Label"="Volume";"Expression"={$_.name}}, `
            @{"Label"="Label";"Expression"={$_.label}}, `
            @{"Label"="Capacity(GB)";"Expression"={"{0:N2}" -f ($_.capacity/1GB)}}, `
            @{"Label"="FreeSpace(GB)";"Expression"={"{0:N2}" -f ($_.freespace/1GB)}}, `
            @{"Label"="%Free";"Expression"={"{0:P2}" -f ($_.freespace/$_.capacity)}}, `
            @{"Label"="Timestamp";"Expression"={(get-date -format G)}} |
        FT -auto
}
$results

screen output:

PSComputerName  Volume Label        Capacity(GB) FreeSpace(GB) %Free   Timestamp           
--------------  ------ -----        ------------ ------------- -----   ---------           
WindowsServer1  D:\    apps         20.00        19.78         98.91 % 6/7/2013 10:51:06 AM
WindowsServer1  E:\    pagefile     16.00        11.91         74.44 % 6/7/2013 10:51:06 AM
WindowsServer1  C:\    windows      39.90        26.61         66.69 % 6/7/2013 10:51:06 AM

PSComputerName  Volume Label        Capacity(GB) FreeSpace(GB) %Free   Timestamp           
--------------  ------ -----        ------------ ------------- -----   ---------           
WindowsServer2  D:\    apps         20.00        19.91         99.56 % 6/7/2013 10:51:08 AM
WindowsServer2  E:\    pagefile     16.00        11.91         74.44 % 6/7/2013 10:51:08 AM
WindowsServer2  C:\    windows      39.90        21.60         54.14 % 6/7/2013 10:51:08 AM

Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>