Selecting Multiple Properties


If you’re interested in only a certain number of properties, you can use Select to specify that. All you have to do is separate the properties by a comma. Suppose you run the preceding script and find a number of undocumented shares on one of the servers-you might want a little bit more information, such as the path to the share and how many people are allowed to connect to it. By default, when a share is created, the “maximum allowed” bit is set, which basically says anyone who has rights to the share can connect. This can be a problem because if too many people connect to a share, they can degrade the performance of the server. To preclude such an eventuality, I always specify a maximum number of connections to the server. The commands to list these properties are in the image from book ListNamePathShare.ps1 script.

Note 

I occasionally see people asking whether spaces or namecase in the property list matter. In fact, when I first started writing scripts and they failed, I often modified spacing and capitalization in feeble attempts to make the script work. Spacing and capitalization do not matter for WMI properties.

image from book 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 command 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 command 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 command. I called the script image from book 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 Diskinfo.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.

image from book
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, youcannot pull anything out of the bag. In the same manner, if you do not "select" a property, 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.

image from book




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net