One Step Further: Examining the Environment Provider


In this exercise, we work with the Windows PowerShell Environment provider.

  1. Start Windows PowerShell.

  2. Use the New-PSDrive cmdlet to create a drive mapping to the alias provider. The name of the new PSDrive will be al. The PSProvider is alias, and the root will be dot (.). This command is shown here:

     new-PSDrive -name al -PSProvider alias -Root .

  3. Change your working location to the new PSDrive you called al. To do this, use the sl alias for the Set-Location cmdlet. This is shown here:

     SL al:\

  4. Use the gci alias for the Get-ChildItem cmdlet, and pipeline the resulting object into the Sort-Object cmdlet by using the sort alias. Supply name as the property to sort on. This command is shown here:

     GCI | Sort -Property name

  5. Use the up arrow to retrieve the previous gci | sort -property name command and modify it to use a Where-Object cmdlet to return aliases only when the name is greater than the letter t. Use the where alias to avoid typing the entire name of the cmdlet. The resulting command is shown here:

     GCI | sort -Property name | Where {$_.Name -gt "t"}c

  6. Change your location back to the C:\ drive. To do this, use the sl alias and supply the C:\ argument. This is shown here:

     SL C:\

  7. Remove the PSDrive mapping for al. To do this, use the Remove-PSDrive cmdlet and supply the name of the PSDrive to remove. Note, this command does not want a trailing colon (:) or colon with backslash (:\). The command is shown here:

     Remove-PSDrive al

  8. Use the Get-PSDrive cmdlet to ensure the al drive was removed. This is shown here:

     Get-PSDrive

  9. Use the Get-Item cmdlet to obtain a listing of all the environment variables. Use the path argument and supply env:\ as the value. This is shown here:

     Get-Item -Path env:\

  10. Use the up arrow to retrieve the previous command, and pipeline the resulting object into the Get-Member cmdlet. This is shown here:

     Get-Item -Path env:\ | Get-Member

  11. The results from the previous command are shown here:

        TypeName: System.Collections.Generic.Dictionary'2+ValueCollection[[System.St ring, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e0 89],[System.Collections.DictionaryEntry, mscorlib, Version=2.0.0.0, Culture=neu tral, PublicKeyToken=b77a5c561934e089]] Name          MemberType   Definition ----          ----------   ---------- CopyTo        Method       System.Void CopyTo(DictionaryEntry[] array, Int32... Equals        Method       System.Boolean Equals(Object obj) GetEnumerator Method       System.Collections.Generic.Dictionary`2+ValueColl... GetHashCode   Method       System.Int32 GetHashCode() GetType       Method       System.Type GetType() get_Count     Method       System.Int32 get_Count() ToString      Method       System.String ToString() PSDrive       NoteProperty System.Management.Automation.PSDriveInfo PSDrive=Env PSIsContainer NoteProperty System.Boolean PSIsContainer=True PSPath        NoteProperty System.String PSPath=Microsoft.PowerShell.Core\En... PSProvider    NoteProperty System.Management.Automation.ProviderInfo PSProvi... Count         Property     System.Int32 Count {get;}

  12. Press the up arrow twice to return to the get-item -path env:\ command. Use the Home key to move your insertion point to the beginning of the line. Add a variable called $objEnv and use it to hold the object returned by the get-item -path env:\ command. The completed command is shown here:

     $objEnv=Get-Item -Path env:\

  13. From the listing of members of the environment object, find the count property. Use this property to print out the total number of environment variables. As you type $o, try to use Tab completion to avoid typing. Also try to use Tab completion as you type the c in count. The completed command is shown here:

     $objEnv.Count

  14. Examine the methods of the object returned by get-item -path env:\. Notice there is a Get_Count method. Let’s use that method. The code is shown here:

     $objEnv.Get_count

  15. When this code is executed, however, the results define the method rather than execute the Get_Count method. These results are shown here:

     MemberType          : Method OverloadDefinitions : {System.Int32 get_Count()} TypeNameOfValue     : System.Management.Automation.PSMethod Value               : System.Int32 get_Count() Name                : get_Count IsInstance          : True

  16. To retrieve the actual number of environment variables, we need to use empty parentheses is at the end of the method. This is shown here:

     $objEnv.Get_count()

  17. If you want to know exactly what type of object you have contained in the $objEnv variable, you can use the GetType method, as shown here:

     $objEnv.GetType()

  18. This command returns the results shown here:

     IsPublic IsSerial Name                                     BaseType -------- -------- ----                                     -------- False    True     ValueCollection                          System.Object

  19. This concludes this one step further exercise.




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