Ezine 145 - PowerShell Employs WMI to Interrogate DisksPowerShell Employs WMI* to Interrogate DisksOne key reason for switching from VBScript to PowerShell is because PowerShell handles WMI* so much more easily than VBScript. * (Windows Management Instrumentation) Topics for WMI Interrogates Disks
This Week's SecretDid you ever stop and think about what your operating system must know? In a word the operating system knows EVERYTHING. Normally XP, Vista or Windows 2003 hides much of the detail from displaying in the Windows Explorer shell and its numerous GUIs. WMI (Windows Management Instrumentation) is a built-in technology, which provides us with a way to retrieve technical detail from the operating system. My WMI examples reminds me of climbing a mountain, there is more than one route to the summit. Moreover, you could climb using a variety of equipment, and you could even call for help from a guide. This Week's MissionThis week's mission is to check how much free space is available on each disk. From the point of view of learning PowerShell, we really are diving in at the deep-end. I admit that I am obsessed with finding scripts which do useful work, rather than just show off clever scripting techniques. The price we pay for this gung-ho approach is that some elements may be incomprehensible to the beginner, however that is a risk worth taking. Also remember that I do provide Learning Points to explain how the script achieves its goals. As ever, if we break-down the task into individual elements, it is easier to understand what is going on. My hope is that by dissecting my scripts you can modify commands and improve them to fit your circumstances. Objective 1: Get one of the WMI objects. As the acronym WMI (Windows Management Instrumentation) suggests, this method probes the operating system; in our disk example WMI connects specifically to the win32_logicaldisk object. Objective 2: We want PowerShell to report each disk's size and free space. To collect this data we can to issue a SQL query to the LogicalDisk. In Example 2, observe the command: get-wmiobject -query "Select.....". Objective 3: Much of the rest of Examples 2 and 3 is concerned with sorting the data, and then formatting the output to produce a table to our liking. Example 1 (Basic) Command to display disk drivesInstructions: Pre-requisite: Visit Microsoft's site and download the correct version of PowerShell for your operating system.
# Start of Example 1 clear-Host Example 1a: (Format) Table with automatic column width# Start of Example 1a clear-Host Learning PointsNote 1: PowerShell commands are not case sensitive. Get-wmiobject works as well as get-WmiObject. Note 2: Observe PowerShell and WMI can display properties such as DriveType which do not display in Windows Explorer Trusty Twosome (Get-Help and Get-Member)Whenever you discover a new PowerShell command such as get-wmiobject, it benefits from being surveyed by what I call the 'Trusty Twosome': Get-Help and Get-Member. To see what I mean, try these two commands:
1) get-help Get-WmiObject Get-help shows useful parameters such as: -query, -computername and -credential 2)
get-wmiobject win32_logicaldisk |get-member -memberType property This command reveals a list of properties that you can incorporate in your scripts, for example: DeviceID and DriveType. If you are looking for handy network utilities, try some of the free downloads at Tools4Ever Example 2 (Grouping) Disk drives by type.# Start of Example 2 clear-Host Learning PointsNote 1: This example uses the powerful -query to select (* all) properties from the win32_logicaldisk object. Note 2: Once selected, we can sort the data and even group the disks in the output. Note 3: If you look carefully you can see the backtick (`) at the end of line two. The purpose of this tiny symbol is to tell PowerShell that the same command continues on the next line. Example 3 (Better) Displaying FreeSpace as a percentage# Start of Example 3 clear-Host Learning PointsNote 1: This example does not use the -query parameter. Instead it employs the where clause to filter the data and pass the stream to the foreach loop, which display each disk drive. Note 2: $Jumbo is a variable to assist with converting the bytes to gigabytes. Note 3: My purpose is to show you how flexible PowerShell is at achieving slightly different objectives. (Remember my analogy with mountain climbing, many ways of reaching the summit.) Example 3a (Best) Displaying FreeSpace as a percentage# Start of Example 3a clear-Host Learning PointsNote 1: Observe the -f formatting technique. What -f does is give greater control over the column widths. Each of the three pairs of {braces} represents a data item in the last line. Another benefit of the -f formatting is to enable us to display the results as a percentage {2,10:p}. Note 2: $Jumbo is a variable to assist with converting bytes to gigabytes. I expect you spotted the usual maths operators to divide (/) and display as whole numbers [int]. Tip: Keep an eye on the type of bracket. Whether PowerShell uses (parenthesis) {braces} or [square] brackets is highly significant. As a rule of thumb these types of bracket mean: (compulsory), {script block}, [optional]. ChallengesChallenge 1: Experiment with the display of Examples 1a and 2, remove -auto. You could also try format-list instead of format-table. Challenge 2: In Example 3a, edit the properties, for example, change [int]($_.freespace / $Jumbo), to ($_.freespace / $Jumbo) Challenge 3: In Example 2 attempt to alter the 'Sort' and '-groupby' criteria. ˆ Summary of Displaying Disk InformationOur mission was to employ get-wmiobject to display disk information. By using 3 or 4 examples, I hope that you gain perspective of the command. My greatest joy would be if you would amend one of my scripts to suit your circumstances. See More Microsoft PowerShell WMI Examples:• Home • PowerShell WMI • WMI Services • Memory • Disk • DNS • Win32_pingstatus • WMI Class • Win32_NetworkAdapter • Win32_NetworkAdapterConfiguration • Disable NIC • Examples • Win32_product • PowerShell -Query • PowerShell -Filter Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
*
| ||||||