Utilizing an Operator


One of the nice things you can do is use greater than and less than operators in your evaluation clause. What is so great about greater than? It makes working with alphabetic characters and numeric characters easy. If you work on a server that hosts home directories for users (which are often named after their user names), you can easily produce a list of all home directories from the letters D through Z by using the > D operation. Keep in mind that D$ is greater than D, and if you really want shares that begin with the letter E, then you could say greater than or equal to E. This command would look like >='E'.

 ListGreaterThanShares.ps1 $strComputer = "." $wmiNS = "root\cimv2" $wmiQuery = "Select name from win32_Share where name > 'd'" $objWMIServices = Get-WmiObject -computer $strComputer `    -namespace $wmiNS -query $wmiQuery    $objWMIServices | Sort-Object -property name |    Format-List -property name

Identifying service accounts

  1. Open Notepad, or some other script editor.

  2. On the first line, declare a variable called $strComputer. Use the dot (.) WMI shortcut to point to the local computer. This line of code is shown here:

     $strComputer = "."

  3. On the next line, declare a variable called $wmiNS. Assign the string "Root\cimv2" to the variable. This will cause the WMI query to use the Root\cimv2 WMI namespace. This line of code is shown here:

     $wmiNS = "root\cimv2"

  4. On the next line, declare a variable called $wmiQuery. You will select only the startname property and the name property from the WIN32_Service WMI class. This line of code is shown here:

     $wmiQuery = "Select startName, name from win32_service"

  5. On the next line, declare the $objWMIServices variable. Use the $objWMIServices variable to hold the object that comes back from using the Get-WmiObject cmdlet. Use the computer argument of the Get-WmiObject cmdlet to point the query to the local computer. To do this, use the dot (.) value that is contained in the variable $strComputer. Because we will continue the command on the next line, use the grave accent (`) character to tell Windows PowerShell to continue the command on the next line. The code that does this is shown here:

     $objWMIServices = Get-WmiObject -computer $strComputer `

  6. Use the namespace argument of the Get-WmiObject cmdlet to specify the WMI namespace specified in the $wmiNS variable. Use the query argument of the Get-WmiObject cmdlet to specify the WMI query contained in the variable $wmiQuery. This code is shown here:

     -namespace $wmiNS -query $wmiQuery

  7. Use the object that comes back from the Get-WmiObject cmdlet that is contained in the $objWMIServices variable and pipeline it into the Sort-Object. Use the Sort-Object cmdlet to sort the list first by the startName property and second by the name property. Place the pipeline character at the end of the line because we will pipeline this object into another cmdlet. The code that does this is shown here:

     $objWMIServices | Sort-Object startName, name |

  8. Finally, we will receive the pipelined object into the Format-List cmdlet. We first format the list by the name property from WIN32_Service and second print out the startName. This code is shown here:

     Format-List name, startName

  9. The completed script is shown here:

     $strComputer = "." $wmiNS = "root\cimv2" $wmiQuery = "Select startName, name from win32_service" $objWMIServices = Get-WmiObject -computer $strComputer `     -namespace $wmiNS -query $wmiQuery     $objWMIServices | Sort-Object startName, name |     Format-List name, startName

  10. Save the script as yournameimage from book IdentifyServiceAccounts.ps1. Run the script. You should see an output similar to the one shown here. If not, compare your script to the image from book IdentifyServiceAccounts.ps1 script.

     name      : BITS startName : LocalSystem name      : Browser startName : LocalSystem name      : CcmExec startName : LocalSystem name      : CiSvc startName : LocalSystem

  11. This completes the identifying service accounts procedure.

Logging service accounts

  1. Open the image from book IdentifyServiceAccounts.ps1 script in Notepad or your favorite script editor. Save the script as yournameimage from book IdentifyServiceAccountsLogged.ps1.

  2. Declare a new variable called $strFile. This variable will be used for the filePath argument of the Out-File cmdlet. Assign the string "C:\Mytest\ServiceAccounts.txt" to the $strFile variable. This code is shown here:

     $strFile = "c:\mytest\ServiceAccounts.txt"

  3. Under the line of code where you declared the $strFile variable, use the New-Variable cmdlet to create a constant called constASCII. When you assign the constASCII value to the name argument of the New-Variable cmdlet, remember you leave off the dollar sign. Use the value argument of the New-Variable cmdlet to assign the value of "ASCII" to the constASCII constant. Use the option argument and supply constant as the value for the argument. The completed command is shown here:

     New-Variable -name constASCII -value "ASCII" `    -option constant

  4. At the end of the Format-List line, place the pipeline character (|). This is shown here:

     Format-List name, startName |

  5. On the next line, use the Out-File cmdlet to produce an output file containing the results of the previous command. Use the filepath argument to specify the path and file name to create. Use the value contained in the $strFile variable. To ensure that the output file is easily read, we want to use ASCII encoding. To do this, use the encoding argument of the Out-File cmdlet and supply the value contained in the $constASCII variable. Use the grave accent character (`) to indicate the command will continue to the next line. The resulting code is shown here:

     Out-File -filepath $strFile -encoding $constASCII `

  6. On the next line, use two arguments of the Out-File cmdlet. The first argument tells Out-File to append to a file if it exists. The second argument tells Out-File not to overwrite any existing files. This code is shown here:

     -append -noClobber

  7. Save and run your script. You should see a file called ServiceAccounts.txt in your Mytest directory on the C:\ drive. The contents of the file will be similar to the output shown here:

     name      : AppMgmt startName : LocalSystem name      : AudioSrv startName : LocalSystem name      : BITS startName : LocalSystem

  8. If you do not find an output similar to this, compare your script with image from book IdentifyServiceAccountsLogged.ps1.

  9. This concludes the logging service accounts procedure.




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