capitalization in feeble attempts to make the script work. Spacing and capitalization do not matter for WMI properties.
ListNamePathShare.ps1 $strComputer = "." $wmiNS = "root\cimv2"
$wmiQuery = "Select name,path, AllowMaximum from win32_share"
$objWMIServices = Get-WmiObject -computer $strComputer -namespace $wmiNS ` -query $wmiQuery
$objWMIServices | Sort-Object -property name | Format-List -property name,path,allowmaximum
Working with running processes 1. Open Windows PowerShell.
2. Use the Get-Process cmdlet to obtain a listing of processes on your machine. The com-
mand is shown here: Get-Process
3. A portion of the results from the previous command is shown here:
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName --- --- --- --- --- --- -- --- 101 5 1132 3436 32 0.03 660 alg 439 7 1764 2856 60 6.05 1000 csrss 121 5 912 3532 37 0.22 1256 ctfmon 629 19 23772 23868 135 134.13 788 explorer 268 7 12072 18344 109 1.66 1420 hh
4. To return information about the explorer process, use the name argument. This com-
mand is shown here: Get-Process -name explorer
5. The results of this command are shown here:
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName --- --- --- --- --- --- -- --- 619 18 21948 22800 115 134.28 788 explorer
6. Use the Get-WmiObject cmdlet to retrieve information about processes on the machine.
Pipe the results into the more function, as shown here: Get-WmiObject win32_process |more
7. You will notice that the results go on for page after page. The last few lines of one of those
pages is shown here:
QuotaPagedPoolUsage : 0 QuotaPeakNonPagedPoolUsage : 0 QuotaPeakPagedPoolUsage : 0 ReadOperationCount : 0
<SPACE> next page; <CR> next line; Q quit
8. To retrieve information only about the Explorer.exe process, use the filter argument and
specify that the name property is equal to Explorer.exe. The revised command is shown here:
Get-WmiObject win32_process -Filter "name='explorer.exe'"
9. To display a table that is similar to the one produced by Get-Process, use the up arrow to
retrieve the previous Get-WmiObject command. Copy it to the clipboard by selecting it with the mouse and then pasting it into Notepad or some other script editor. Pipeline the results into the Format-Table cmdlet and choose the appropriate properties, as shown here. Saving this command into a script makes it easier to work with later. It also makes it easier to write the script by breaking the lines instead of just typing one long com- mand. I called the script ExplorerProcess.ps1, and it is shown here:
Get-WmiObject win32_process -Filter "name='explorer.exe'" | Format-Table handlecount,quotaNonPagedPoolUsage, PeakVirtualSize, WorkingSetSize, VirtualSize, UserModeTime,KernelModeTime, ProcessID, Name
10. This concludes the working with running processes procedure.
Caution When using the filter argument of the Get-WmiObject cmdlet, pay attention to the use of quotation marks. The filter argument is surrounded by double quotation marks. The value being supplied for the property is surrounded by single quotes. Example: -Filter "name='explorer.exe'". This can cause a lot of frustration if not followed exactly.
Adding logging
1. Open Windows PowerShell.
2. Use the alias for the Get-WmiObject cmdlet and supply the WIN32_logicalDisk class as
the argument to it. Use the redirection arrow (>) to redirect output to a file called Dis- kinfo.txt. Place this file in the C:\Mytest folder. This command is shown here:
gwmi win32_logicaldisk >c:\mytest\DiskInfo.txt
3. Use the up arrow and retrieve the previous command. This time, change the class name
to WIN32_OperatingSystem and call the text file OSinfo.txt. This command is shown here:
gwmi win32_operatingsystem >c:\mytest\OSinfo.txt
4. Use the up arrow and retrieve the previous gwmi WIN32_operatingsystem command.
Change the WMI class to WIN32_ComputerSystem and use two redirection arrows (>>) to cause the output to append to the file. Use Notepad to open the file, but include this command separated by a semicolon. This is illustrated here. The command is continued to the next line by using the grave accent character (`) for readability.
gwmi win32_ComputerSystem >>c:\mytest\OSinfo.txt; ` notepad c:\mytest\OSinfo.txt
5. This concludes the adding logging procedure.
Quick Check
Q. To select specific properties from an object, what do you need to do on the Select line?
A. You need to separate the specific properties of an object with a comma on the Select line of the execQuery method.
Q. To avoid error messages, what must be done when selecting individual properties on the Select line?
A. Errors can be avoided if you make sure each property used is specified in the select line. For example, the WMI query is just like a paper bag that gets filled with items that are picked up using the Select statement. If you do not put something in the paper bag, you- cannot pull anything out of the bag. In the same manner, if you do not "select" a prop- erty, you cannot later print or sort on that property. This is exactly the way that an SQL Select statement works.
Q. What can you check for in your script if it fails with an "object does not support this method or property" error?
A. If you are getting an "object does not support this method or property" error messages, you might want to ensure you have referenced the property in your Select statement before to trying to work with it in an Output section.