blog.icewolf.ch

Let's talk about IT!
posts - 1317, comments - 295, trackbacks - 0

My Links

Archives

Post Categories

icewolf

Free Disk Space with WMI

Hallo zusammen,

Kürzlich musste ich eine Abfrage des freien Speicherplatzes machen. Da bietet sich natürlich Windows Management Instrumentation (WMI) an.

Das kann man entweder per VBS oder per Powershell machen. Ich habe mal für beide Arten ein Beispiel erstellt.

VBS Code

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk WHERE DriveType=3")

For Each objDisk in colDisks
 result = result & "SystemName: " & objDisk.SystemName & vbcrlf
 result = result & "DeviceID: " & objDisk.DeviceID & vbcrlf
 result = result & "VolumeName: " & objDisk.VolumeName & vbcrlf
 result = result & "Size: " & FormatNumber((objDisk.Size/1024/1024/1024),2) & vbcrlf
 result = result & "FreeSpace: " & FormatNumber((objDisk.FreeSpace/1024/1024/1024), 2) & vbcrlf
 result = result & vbcrlf
Next
Wscript.Echo result

Powershell Befehl

Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer . | Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}}

Grüsse
Andres Bohren

Print | posted on Sunday, February 17, 2013 10:28 AM | Filed Under [ Windows Powershell ]

Powered by:
Powered By Subtext Powered By ASP.NET