3. USING POWERSHELL TO INVESTIGATE SERVER ISSUES
PowerShell provides full integration
with the Windows Management Instrumentation (WMI) framework, The WMI
framework is a very powerful method for querying the Windows system to
find detailed information. Traditionally, it has not been easy to gain
access to WMI, and this was mainly done via a programming interface.
PowerShell opens up access to WMI making it a useful resource for your
troubleshooting efforts.
3.1 Interrogating Disk Space Utilization
In this example, you are going to see how to make use of one of the WMI win32-logicaldisk
classes to retrieve information about disk space utilization on the
server. Initially, the raw information provided by the class will be
returned and then the data will be used to produce a detailed analysis
of disk space available:
Get-wmiobject win32_logicaldisk
Running the Get-WmiObject cmdlet and providing the win32_logicaldisk class returns the following results on my computer:
DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 105582891008
Size : 128742060032
VolumeName :
DeviceID : D:
DriveType : 5
ProviderName :
FreeSpace : 0
Size : 45471744
VolumeName : VBOXADDITIONS_4.
The default list view of the results is
not very readable for this output, but you can improve it by formatting
the output as a table:
Get-wmiobject win32-logicaldisk | format-table -autosize
On my computer this statement produces the following output:
DeviceID DriveType ProviderName FreeSpace Size VolumeName
-------- --------- ------------ --------- ---- ----------
C: 3 105582891008 128742060032
D: 5 0 45471744 VBOXADDITIONS_4.
You may want to filter this information so that you are only displaying fixed disks. This can be achieved by using a Filter parameter to filter the disks returned to be fixed disks only:
Get-WmiObject Win32_logicaldisk -Filter "DriveType = 3" | Format-Table -autosize
This is getting better, but the results
are being presented with the free space and the size in bytes. I can’t
remember the last time when bytes were a meaningful unit of measure for
a hard disk, so you can modify the script to format the output in GB
(code file: PS_DiskInfo01.PS1):
$diskinfo = Get-WmiObject Win32_logicaldisk -Filter "DriveType = 3"
$diskinfo | foreach-object {$_.FreeSpace = $_.FreeSpace / 1GB; '
$_.Size = $_.Size / 1GB}
$diskinfo | format-table -autoSize
We are now presented with a table that
shows the total capacity of the drive and the available space in GB, as
shown when run on my system below:
DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 388
Size : 466
VolumeName : OS
DeviceID : H:
DriveType : 3
ProviderName :
FreeSpace : 303
Size : 466
VolumeName : DATA