欢迎光临 Enjoy IT (ITECN.NET) 登录 | 注册 | 帮助

联合

公告

注意:本人的所有文档均可以引用,但引用必须说明出处。
跟我从头学Powershell(4)-操作符

跟我从头学powershell(四)

操作符

一.数学操作符

数学操作符允许你进行数学运算:

Operator

Description

Example

+

Add two values together.

PS C:\> 5+4

9

-

Subtract one value from another.

PS C:\> 134-90

44

-

Change a value to a negative number.

PS C:\>-6

-6

*

Multiply two values together.

PS C:\> 3*4.5

13.5

/

Divide one value by another.

PS C:\> 6/4

1.5

%

取模.

PS C:\Temp> 6%4

2

优先级:

就和小学学得一样,各操作符之间有优先级。括号压倒优先级

 

变量:

变量在使用数学操作符的时候,和数字没有什么两样,但下面一个例子,则报错,因为我们采用的一个变量是字符串,一个是数字:

PS C:\> $var1="Windows"

PS C:\> $var2=100

PS C:\> $var1+var2

You must provide a value expression on the right-hand side of the '+'

operator.

At line:1 char:7

+ $var1+v <<<< ar2

PS C:\>

 

一元操作符

一元操作符通常用于增加或减少一个固定值

PS C:\> $var=10

PS C:\> $var++

PS C:\> $var

11

PS C:\> $var--

PS C:\> $var

10

PS C:\>

二.逻辑操作符

Powershell的逻辑操作符用来测试和检查一个表达式,通常的结果是TRUE FALSE

Operator

Description

Example

-and

All expressions must evaluate as TRUE.

(1 -eq 1) -and (2 - eq 2) returns TRUE

-or

At least one expression must evaluate as TRUE.

(1 -eq 1) -or (2 -eq 4) returns TRUE

-not

Evaluates the inverse of one of the expressions.

(1 -eq 1) -and -not (2 -gt 2) returns TRUE

!

The same as -not.

(1 -eq 1) -and ! (2 -gt 2) returns TRUE

 

 

逻辑操作符通常用于你需要比较多个条件的情况。下面是举例,第一个是and情况,第二个是or逻辑操作符:

PS C:\> $varA=5

PS C:\> $varB=5

PS C:\> if (($varA -eq $varB) -and ($varB -gt 20))

>>{

>>Write-Host "Both conditions are true."

>>}

>>else

>>{

>>Write-Host "One or both conditions are false."

>>}

>> 

One or both conditions are false.

PS C:\>

or逻辑操作符:

PS C:\> if (($varA -eq $varB) -or ($varB -gt 20))

>> {

>> Write-Host "At least one condition is true."

>> }

>> else

>> {

>> Write-Host "Both conditions are false."

>> }

At least one condition is true.

PS C:\>

 

三.分配操作符:

Powershell采用分配操作符设定变量值,我们在前面已经采用了等号,但还有一些其他的操作符:

Operator

Description

=

Sets a value of a variable to the specified value.

+=

Increases the value of a variable by the specified value or appends to the existing value.

-=

Decreases the value of a variable by the specified value.

*=

Multiplies the value of a variable by the specified value or appends to the existing value.

/=

Divides the value of a variable by the specified value.

%=

Divides the value of a variable by the specified value and assigns the remainder (modulus) to the variable.

下面是这些操作符的一些示例:

PS C:\> $var=7

PS C:\> $var

7

PS C:\> $var+=3

PS C:\> $var

10

PS C:\

 

PS C:\> $var-=3

PS C:\> $var

7

PS C:\>

 

PS C:\> $var*=3

PS C:\> $var

21

PS C:\>

 

PS C:\> $var/=7

PS C:\> $var

3

PS C:\>

 

PS C:\> $var=9

PS C:\> $var%=4

PS C:\> $var

1

PS C:\>

 

你必须留意赋值操作,因为powershell尽量采用“最好的”方式来理解你的意图,比如下面一个举例:

PS C:\> $var="3"

PS C:\> $var+=7

PS C:\> $var

37

PS C:\>

 

因为“3”是字符串,所以,powershell就用简单的连接来代替掉了相加,你如果对你的变量的类型不能确定的话,你可以使用gettype()方法:

PS C:\> $var.gettype()

 

IsPublic IsSerial Name                                   BaseType

-------- -------- ----                                   --------

True     True     String                               System.Object

 

PS C:\>

 

在采用“=”赋值的时候,powershell会有一些独特的特性,比如,你给变量赋予16进制的值时,会自动转变成10进制显示:

你甚至可以用一些标准的术语,比如KBMBGB等等:

PS C:\> $var=10KB

PS C:\> $var

10240

PS C:\> $var=2MB

PS C:\> $var

2097152

PS C:\> $var=.75GB

PS C:\> $var

805306368

PS C:\>

 

最后,我们可以在一行当中为多个变量赋值:

PS C:\> $varA,$varB,$varC="Apple",3.1416,"Windows"

PS C:\> get-variable var?

 

Name                           Value

----                           -----

varC                           Windows

varB                           3.1416

varA                           Apple

 

 

PS C:\>

 

当然如果你提供的值超过变量,那么将把多出来的值赋予给最后一个变量:

PS C:\> $varA,$varB,$varC="Apple",3.1416,"Windows","Linux"

PS C:\> get-variable var?

 

Name                           Value

----                           -----

varC                           {Windows, Linux}

varB                           3.1416

varA                           Apple

 

PS C:\>

 

四.位操作符

位操作符用于二进制计算:

Operator

Definition

-band

binary and

-bor

binary or

-bnot

binary not

 

下面是一个实例:

PS C:\> 255 -band 255

255

PS C:\> 255 -band 150

150

PS C:\> 32 -bor 16

48

PS C:\>

 

五.特殊操作符

Replace 操作符:

Replace操作符可以替换字符,这个操作本质上是找到目标字符,然后进行替换:

其语法是:

"String-to-search" -replace "Search-for","Replace-with"

下面是一个示例:

PS C:\> "PowerShell" -replace "e","3"

Pow3rSh3ll

PS C:\> "PowerShell" -replace "shell","tool"

Powertool

PS C:\> "PowerShell" -replace "k","m"

PowerShell

PS C:\>

你也可以采用-replace操作符来对变量操作,但是注意的是,它不改变变量的初始值,唯一改变的方法是重新为变量赋值。

PS C:\> $var=$var -replace "p","sh"

PS C:\> $var

showerShell

PS C:\>

我们也在数组中使用-replace

PS C:\> $var=@("aaa","bbb","abab","ccc")

PS C:\> $var

aaa

bbb

abab

ccc

PS C:\> $var=$var -replace "a","z"

PS C:\> $var

zzz

bbb

zbzb

ccc

PS C:\>

 

我们甚至可以在文本文件上采用-replace操作符:

PS C:\> $var=get-content "boot.ini"

PS C:\> $var -replace "windows","WIN"

[boot loader]

timeout=15

default=multi(0)disk(0)rdisk(0)partition(2)\WIN

[operating systems]

multi(0)disk(0)rdisk(0)partition(2)\WIN="Microsoft WIN XP

Professional" /fastdetect /NoExecute=OptIn

multi(0)disk(0)rdisk(0)partition(1)\WIN="WIN Server 2003, Enterprise"

/noexecute=optout /fastdetect

C:\CMDCONS\BOOTSECT.DAT="Microsoft WIN Recovery Console" /cmdcons

PS C:\>

注意,-replace是不区分大小写的。如果你要区分大小写,应该使用-creplace操作符。

下面是这些操作符的一个表格:

Operator

Definition

Example

-replace

Replace

"PowerShell" - replace "s","$"

-ireplace

Case-insensitive replace. Essentially the same as
-replace.

"PowerShell" - replace "s","$"

-creplace

Case-sensitive replace.

"PowerShell" - creplace "p","t"

 

 

PS C:\> "PowerShell" -creplace "p","t"

PowerShell

PS C:\> "PowerShell" -creplace "P","t"

towerShell

PS C:\>

 

Type操作符

Powershell包括三种类型的type操作符

Operator

Definition

Example

-is

Check if object IS a specific type.

$var -is [string]

-isnot

Check if object IS NOT a specific type.

$var -isnot [string]

-as

Convert object to specified type.

3.1416 -as string

操作的结果对于-is-isnottruefalse

PS C:\> $now=get-date

PS C:\> $now -is [datetime]

True

PS C:\> 1024 -is [int]

True

PS C:\> "Microsoft" -isnot [string]

False

PS C:\>

 

-as操作符将转换对象成为指定的类型:

PS C:\> $var=get-date

PS C:\> $var.gettype()

 

IsPublic IsSerial Name                                     BaseType

-------- -------- ----                                     --------

True     True     DateTime                            System.ValueType

 

PS C:\> if ($var -isnot [string]) {

>> $var=$var -as [string]

>> $var.gettype()

>> $var.PadLeft(25)

>> }

>> 

 

IsPublic IsSerial Name                                     BaseType

-------- -------- ----                                     --------

True True String                                         System.Object

5/21/2006 7:53:02 PM

PS C:\>

上面这个实例,将日期类型的数据转换成了字符类型。做这个操作的另一个方法是tostring方法,结果和-as操作符一样。

 

Range操作符

Range操作符用来指定一个范围内的值,注意,你必须指定一个开始和结束的值:

PS C:\> $var=@(1..5)

PS C:\> $var

1

2

3

4

5

PS C:\>

 

注意,range操作符仅仅能够操作整数,如果你对字符串进行操作,将出错:

PS C:\> $var=@("a".."j")

Cannot convert value "a" to type "System.Int32". Error: "Input string

was not in a correct format."

At line:1 char:13

+ $var=@("a".." <<<< j")

PS C:\>

 

Call操作符

Call操作符用于你要执行一个命令,是一个“&”字符,用于强制powershell执行命令:

PS C:\> $all=get-childitem *.ps1

PS C:\> foreach ($s in $all) {&$s}

PS C:\>#each Powershell script executes

 

你也可以采用call操作来创建变量,存放结果:

PS C:\> $j=get-process | where {$_.workingset -gt 5000000}

PS C:\> &$j

PS C:\> #an array of all processes with a workingset size greater than

 5000 is returned.

 

已发表 2008年3月3日 10:44 作者 qiqinghua

归档在:

评论

尚无任何评论

禁止匿名发表评论