Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Today’s post is the advertisement of a simple script, which uses Powershell and WMI to query the disks and their usage of some machine(s). Furthermore, the script allows you to query a comma-separated list of hosts by name, set a usage treshold and some html file used to display the results.
For example, the following output (html file) was obtained by querying my own machine:
As mentioned, the script itself uses simple Powershell techniques like WMI, html and csv handling. Beware that actual script comments and output is in German. Invoking the script is a simple matter, since default arguments are provided.
1: #param:
2: # machineName - einzelner Rechnername, oder kommaseparierte List von Rechnern => X oder X,Y
3: # usageTreshold - Wert zwischen 0 und 1, Schwellwert für Auslastung des Festplattenspeichers in %
4: # htmlFile - HTML Datei, die zur Dokumentation eingesetzt werden kann
5:
6: param(
7: [string]$machineName = "NONE",
8: [float]$usageTreshold = 0.11,
9: [string]$htmlFile = "C:\environment.html"
10: );
11:
12: function CheckParam(){
13: if ([System.String]::IsNullOrEmpty($machineName) -eq $true) { throw "Rechnername darf nicht null oder leer sein!" ; }
14: }
15:
16: function GetSpaceOnMachine([string]$machine, [ref]$HTMLRepository) {
17: write-host "WMI - GetSpace on `"$machine`" ... " -foregroundcolor gray;
18: $private:message = "";
19: $private:MBFrac = 0.00000095367431640625 ;
20:
21: (get-wmiobject win32_logicaldisk -namespace "root\cimv2" -computername "$machine" ) | ? { $_.DriveType -eq 3 ; } | % {
22:
23: $private:name = $_.Name;
24: $private:size = $_.Size;
25: $private:unused = $_.FreeSpace;
26: $private:sizeMB = [System.Math]::Round($size * $MBFrac);
27: $private:unusedMB = [System.Math]::Round($unused * $MBFrac);
28:
29: $private:usage = [double]$unused / [double]$size ;
30: $private:usagePercent = [System.Math]::Round($usage * 100.0);
31: if ($usage -lt $usageTreshold) {
32: $message = "Festplatte $name auf $machine ... weniger als $usagePercent% frei ($unusedMB MB von $sizeMB MB frei) !" ;
33: write-host $message -foregroundcolor red ;
34: }
35: else {
36: $message = "Festplatte $name auf $machine ... mehr als $usagePercent% frei ($unusedMB MB von $sizeMB MB frei) !" ;
37: write-host $message -foregroundcolor green ;
38: }
39:
40: $HTMLRepository.Value.Add( ($machine + " (" + $name + ")" ), $message);
41: }
42: }
43:
44: function Main(){
45: cls ;
46:
47: CheckParam ;
48:
49: if ( ($machineName.ToUpperInvariant() -eq 'NONE') -eq $true) {
50: write-host "Rechnername `"NONE`" wird auf hostname gesetzt ... ";
51:
52: $machineName = hostname ;
53: }
54:
55: [hashtable]$private:HTMLRepository = @{};
56:
57: if ( $machineName.Contains(",") -eq $true ) {
58: write-host "Collection von Rechnern wird abgefragt ... " -foregroundcolor white ;
59:
60: ([System.Text.RegularExpressions.Regex]::Split($machineName, ",") ) | % {
61: GetSpaceOnMachine $_ ([ref]$HTMLRepository);
62: }
63: }
64: else { GetSpaceOnMachine $machineName ([ref]$HTMLRepository) ; }
65:
66: if ( [System.String]::IsNullOrEmpty($htmlFile) -eq $false ) {
67:
68: if ( (test-path $htmlFile) -eq $true ) { del $htmlFile -force ; }
69:
70: write-host "Exportiere Ergebnis in HTML Datei ... " ;
71: add-content ("`"Rechner (Festplatte)`", `"Status (" + [System.DateTime]::Now + ")`"") -path "C:\temp.csv" ;
72: $HTMLRepository.Keys | % {
73: $private:val = $HTMLRepository[$_];
74: $private:key = $_ ;
75: add-content "`"$key`", `"$val`" " -path "C:\temp.csv" ;
76: }
77: import-csv "C:\temp.csv" | convertto-html -title ("Freier Festplattenplatz in Umgebung (" + [System.DateTime]::Now + ")" ) > $htmlFile ;
78:
79: $x = (get-content $htmlFile)
80: del -force $htmlFile ;
81: $x | % { $_ -replace "</head>", "<style type=`"text/css`"> td { text-align:left; border:1px solid #000; vertical-align:top; overflow:hidden; } th { background-color: black; color: white; text-align:left; } table { width:100%; }</style></head>" } | add-content $htmlFile ;
82:
83:
84: del "C:\temp.csv" ;
85: }
86: }
87:
88: Main ;
Comments
- Anonymous
August 10, 2011
Where is the original source? Who is the author? Where is the link to his site? - Anonymous
August 10, 2011
Why use inlined constants such as $private:MBFrac = 0.00000095367431640625 ; when PowerShell provides these constants already:$private:sizeMB = [System.Math]::Round($size / 1MB);PS C:> 1KB1024PS C:> 1MB1048576PS C:> 1GB1073741824PS C:> 1PB1125899906842624PS C:> - Anonymous
August 12, 2011
@karl:I made the source, and put it (omitting my name as the author) in this blog post. I don't see a reason for putting a link to this exact same page, into the article. Maybe you take the word "advertisement" too literal.@PhilThanks! I did not think about them (inlined constants) when I typed the script, but I will surely integrate your hint.