跟小D每日学口语

Powershell 变量和常量

Power Shell是一门语言,因此也有变量和常量。不同于其他强类型语言,在PowerShell的脚本中,变量无需预先定义就可以直接使用。当你使用一个变量时,变量被自动地声明。了解Windows Power Shell的变量和常量,是灵活编写脚本程序的基础。

变量以$符号开头: $a

clip_image001 强类型变量主要存在于一些编译型的语言,如C和C++。这种语言往往要求在使用一个变量之前必须先进行声明,而且在声明的时候必须注明变量的类型,如整形,浮点型,字符型等。编译器根据变量的类型,为变量静态地分配内存空间

clip_image001[1] PowerShell的变量使用方式跟Perl类似,无论何时,当需要使用变量时,都要用$带出变量名字。

在PowerShell中,有一系列特殊变量,这些变量都是系统自动创建并且具有特殊含义的。注意:用户自己定义的变量名称不应该与这些特殊变量相同。

clip_image001[2] 下表中的特殊变量,很有意思,但不太好记。对于了解“正则表达式”的程序员来说,比较好记:首先它们是变量,那么肯定是$符号开头,然后在正则表达式中,^符号表示文本行的开始,$符号表示结束,因此$^在PowerShell中表示第一个命令行,$$就表示最后一个命令行,取的就是“最开始”和“最后一个”的意思。$_在Perl的循环体中,也是用于表示当前循环的迭代变量,因此了解一点Perl,对于学习PowerShell还是有些帮助。对于没有Perl基础的人而言,根本就不要去记,用多了自然就记住了,没记住之前就查资料就行了,人脑多用来思考,让电脑帮人记忆吧。

下表列举了PowerShell的特殊变量:

 

 

Name

Use

$^

Contains the first token of the last line input into the shell

$$

Contains the last token of the last line input into the shell

$_

The current pipeline object; used in script blocks, filters, Where-Object, ForEach-Object, and Switch

$?

Contains the success/fail status of the last statement

$Args

Used in creating functions requiring parameters

$Error

If an error occurred, the error object is saved in the $error variable.

$ExecutionContext

The execution objects available to cmdlets

$foreach

Refers to the enumerator in a foreach loop

$HOME

The user's home directory; set to %HOMEDRIVE%\%HOMEPATH%

$Input

Input is piped to a function or code block.

$Match

A hash table consisting of items found by the -match operator

$MyInvocation

Information about the currently executing script or command-line

$PSHome

The directory where PS is installed

$Host

Information about the currently executing host

$LastExitCode

The exit code of the last native application to run

$true

Boolean TRUE

$false

Boolean FALSE

$null

A null object

$this

In the Types.ps1xml file and some script block instances, this represents the current object

$OFS

Output Field Separator used when converting an array to a string

$ShellID

The identifier for the shell. This value is used by the shell to determine the ExecutionPolicy and what profiles are run at Startup

$StackTrace

Contains detailed stack trace information about the last error

 

 

 

 

 

使用普通变量

l 给变量赋值: $a = “This is a string”

l 连接两个字符串变量:

$a = “This is the 1st string.”

$b=” This is the 2nd string.”

$c=$a+” and ”+$b

$c

This is the 1st string. and This is the 2nd string.

数组

在Perl或者vbscript中创建一个数组已经是够简单了,然而在PowerShell中,创建一个数组则更加简单:

$arrName = "LOGONSERVER","HOMEPATH", "APPDATA","HOMEDRIVE"

使用数组,跟VBScript一样:$arrName[0]

强制指定变量类型

通过下列的提示符,可以为一个变量强制指定类型。

 

 

Alias

Type

[int]

32-bit signed integer

[long]

64-bit signed integer

[string]

Fixed-length string of Unicode characters

[char]

A Unicode 16-bit character

[bool]

True/false value

[byte]

An 8-bit unsigned integer

[double]

Double-precision 64-bit floating point number

[decimal]

An 128-bit decimal value

[single]

Single-precision 32-bit floating point number

[array]

An array of values

[xml]

Xml objects

[hashtable]

A hashtable object (similar to a dictionary object)

例如:[int]$b=5,表示$b这个变量只能包含整数值,而不能包含其他类型的值。如果我们写出这样的语句:[int]$b=”abc”,则运行时系统会报告错误。

使用常量

PowerShell的常量与变量有两处不同的地方:常量的值永远不会变,常量不能被删除。

使用常量之前,必须通过Set-Variable这个cmdlet来创建常量,并且使用一些参数来指定它等于某个常量。

注意:当使用常量的时候,必须用$开头,就跟使用普通变量一样。然而,当使用Set-Variable定义常量时,不能用$符号开头。

$aryComputers = "loopback", "localhost"

Set-Variable -name intDriveType -value 3 -option constant

foreach ($strComputer in $aryComputers)

{"Hard drives on: " + $strComputer

Get-WmiObject -class win32_logicaldisk -computername $strComputer|

Where {$_.drivetype -eq $intDriveType}}

posted @ 2010-11-03 00:57  简简单单幸福  阅读(4285)  评论(1编辑  收藏  举报