Gieno  DEBUGGING : PowerShell Training, Episode II

This is a continuation of Episode I. If you haven't read it I suggest at least going through the summary at the end of the post.

Working with Drives

In your basic command prompt you have the normal file system drives that you use to access your hard-drive, floppy, DVD, USB-stick, network shares etc. PowerShell uses this established interface to access other data sources as well. The following providers are accessible by default through drives in PowerShell 1.0:

  • Registry (HKLM: or HKCU:)
  • Certificate store (Cert:)
  • Environment variables (Env:)
  • Aliases (Alias:)
  • Functions (Function:)
  • Variables (Variable:)

When you install extensions for like the one fore IIS, etc. you usually get another drive accessible where you can look at the current webapplications and check their properties.

Navigating

Selecting one of the PowerShell drives is easy. It is almost like in MS-DOS.

 

PS C:\>CD HKLM: 
PS HKLM:\
> DIR

You can use the same commands as you'd do in MS-DOS, such as CD, DIR, MKDIR, etc.

To get a certain property for a registry key, (or a regular folder for that matter) you use the Get-ItemProperty cmdlet.

 

PS HKLM:\> CD system 
PS HKLM:\system
> CD CurrentControlSet\Control 
PS HKLM:\system\CurrentControlSet\Control
> Get-ItemProperty CrashControl 
  
PSPath           : Microsoft.PowerShell.Core\Registry
::HKEY_LOCAL_MACHINE\system\CurrentControlSet\Control\CrashControl 
PSParentPath     : Microsoft.PowerShell.Core\Registry
::HKEY_LOCAL_MACHINE\system\CurrentControlSet\Control 
PSChildName      : CrashControl 
PSDrive          : HKLM 
PSProvider       : Microsoft.PowerShell.Core\Registry 
AutoReboot       : 
1 
CrashDumpEnabled : 
2 
Overwrite        : 
1 
LogEvent         : 
1 
DumpFile         : C:\Windows\MEMORY.DMP 
MinidumpDir      : C:\Windows\Minidump
All 
in all it's more or less the same working with Aliases or Environment variables. Note the following difference in output, though:

PS Env:\
> Dir Env:PROCESSOR_ARCHITECTURE 
  
Name                           Value 
----                           ----- 
PROCESSOR_ARCHITECTURE         AMD64 
  
PS Env:\
> $Env:PROCESSOR_ARCHITECTURE 
AMD64 
PS Env:\
>

Did you note how putting a $-sign before the variable name gave us the value of the variable instead of a complete object? This brings us quite nicely to the subject of variables.

Variables

Variables aren't that tricky to work with. They do not need to be pre-initiated. They do, however, require a $ to be identified as variables. Otherwise you get the following error message:

 

PS C:\> $Name = "Gieno" 
PS C:\
> Name = "Gieno" 
The term 'Name' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again. 
At line:1 char:5 
+ Name  <<<< = "Gieno" 
PS C:\
>

To display the value of a variable you simply type the name of the variable and PowerShell kindly returns the contents

 

PS C:\> $Name 
Gieno

When it comes to strings, you can choose for yourself if you want to use double quotes or single quotes, but there is one minor difference:

 

PS C:\> $str1 = "Hello" 
PS C:\
> $str2 = "World!" 
PS C:\
> $strCombo1 = "$str1 $str2" 
PS C:\
> $strCombo2 = '$str1 $str2
PS C:\
> $strCombo1 
Hello World
! 
PS C:\
> $strCombo2 
$str1 $str2 
PS C:\
> 

As you can see, a variable within double quotes will be evaluated, while within single quotes it will be interpreted literally.

Apart from strings you can also store integers and dates. Integers do not need delimiters and dates are most easily created using the Get-Date function

PS C:\> $birthday = 11/15/1973

 

Arrays

Declaring Arrays is a breeze. PowerShell will automatically Split any comma-separated sequence of values so creating a arrays is as easy as this:

PS C:\> $birthday = 11/15/1973

The arrays are also dynamic by default, so adding values to them is a simple matter as well.

 

 

PS C:\> $Colors += "Cyan""Magenta""Yellow"

Creating two-dimensional arrays are done by declaring each row of the array as a one-dimensional array and then adding them to another array:

PS C:\> $arr1 = 123 
PS C:\
> $arr2 = "A""B""C" 
PS C:\
> $arr3 = $arr1$arr2 
PS C:\
> $arr3[0][0
1 
PS C:\
> $arr3[1][1

PS C:\
>
 

 

User input

Finally, to get dynamic user input you can use the Read-Host cmdlet.

$Name = Read-Host "What is your name?"

 

Summary

Okay, so to summarize we have now covered the following:

  • PowerShell uses system drives to access the registry, environment variables, etc.
  • Variables are preceded with a $
  • To access the value of a variable, simply type it's name.
  • Arrays are comma-separated lists of variables
  • User input can be handled by the Read-Host cmdlet

 

 posted on 2009-09-07 09:49  Gieno  阅读(238)  评论(0编辑  收藏  举报