当前位置: 移动技术网 > IT编程>脚本编程>Shell > Windows Powershell 定义变量

Windows Powershell 定义变量

2017年12月08日  | 移动技术网IT编程  | 我要评论

变量可以临时保存数据,因此可以把数据保存在变量中,以便进一步操作。

#定义变量
$a=10
$b=4
#计算变量
$result=$a*$b
$msg="保存文本"

#输出变量
$result
$msg

40
保存文本

powershell 不需要显示地去声明,可以自动创建变量,只须记住变量的前缀为$.

创建好了变量后,可以通过变量名输出变量,也可以把变量名存在字符串中。但是有个例外单引号中的字符串不会识别和处理变量名。

选择变量名

在powershell中变量名均是以美元符”$”开始,剩余字符可以是数字、字母、下划线的任意字符,并且powershell变量名大小写不敏感($a和$a 是同一个变量)。

某些特殊的字符在powershell中有特殊的用途,一般不推荐使用这些字符作为变量名。当然你硬要使用,请把整个变量名后缀用花括号括起来。

ps c:\test> ${"i"like $}="mossfly"
ps c:\test> ${"i"like $}

mossfly赋值和返回值

赋值操作符为“=”,几乎可以把任何数据赋值给一个变量,甚至一条cmdlet命令
,为什么,因为powershell支持对象,对象可以包罗万象。

ps c:\test> $item=get-childitem .
ps c:\test> $item

  directory: c:\test

mode        lastwritetime   length name
----        -------------   ------ ----
d----    2011/11/23   17:25      abc
-a---    2011/11/24   18:30   67580 a.html
-a---    2011/11/24   20:04   26384 a.txt
-a---    2011/11/24   20:26   12060 alias
-a---    2011/11/24   20:27   12060 alias.ps1
-a---    2011/11/23   17:25     0 b.txt
-a---    2011/11/23   17:25     0 c.txt
-a---    2011/11/23   17:25     0 d.txt
-a---    2011/11/25   11:20    556 employee.xml
-a---    2011/11/24   17:37    7420 name.html
-a---    2011/11/28   15:30     63 ping.bat
-a---    2011/11/24   17:44   735892 powershell_cmdlets.html
-a---    2011/11/28   17:03     60 test.ps1
-a---    2011/11/23   17:37    242 test.txt
-a---    2011/11/28   16:42    170 test.vbs

ps c:\test> $result=3000*(1/12+0.0075)
ps c:\test> $result
272.5

给多个变量同时赋值
赋值操作符不仅能给一个变量赋值,还可以同时给多个变量赋相同的值。

ps c:\test> $a=$b=$c=123
ps c:\test> $a
123
ps c:\test> $b
123
ps c:\test> $c
123

交换变量的值

要交换两个变量的值,传统的程序语言至少需要三步,并且还需定义一个中间临时变量。

$value1 = 10
$value2 = 20
$temp = $value1
$value1 = $value2
$value2 = $temp

在powershell中,交换两个变量的值,这个功能变得非常简单。

ps c:\test> $value1=10
ps c:\test> $value2=20
ps c:\test> $value1,$value2=$value2,$value1
ps c:\test> $value1
20
ps c:\test> $value2
10

查看正在使用的变量
powershell将变量的相关信息的记录存放在名为variable:的驱动中。如果要查看所有定义的变量,可以直接遍历variable:

ps c:\test> ls variable:

name              value
----              -----
"i"like $           mossfly
$               cls
?               true
^               cls
_
1               1
a               123
args              {}
b               123
c               123
confirmpreference       high
consolefilename
debugpreference        silentlycontinue
。。。

查找变量

因为有虚拟驱动variable:的存在,可以象查找文件那样使用通配符查找变量。例如要查询以value打头的变量名。

ps c:\test> ls variable:value*

name              value
----              -----
value1             20
value2             10

验证变量是否存在

验证一个变量是否存在,仍然可以象验证文件系统那样,使用cmdlet test-path。为什么?因为变量存在变量驱动器中。

ps c:\test> test-path variable:value1
true
ps c:\test> test-path variable:value2
true
ps c:\test> test-path variable:valueunkonw
false

删除变量

因为变量会在powershell退出或关闭时,自动清除。一般没必要删除,但是你非得删除,也可以象删除文件那样删除它。

ps c:\test> test-path variable:value1
true
ps c:\test> del variable:value1
ps c:\test> test-path variable:value1
false

使用专用的变量命令

为了管理变量,powershell提供了五个专门管理变量的命令clear-variable,get-variable,new-variable,remove-variable,set-variable。因为虚拟驱动器variable:的存在,clear,remove,set打头的命令可以被代替。但是get-variable,new-variable。却非常有用new-variable可以在定义变量时,指定变量的一些其它属性,比如访问权限。同样get-variable也可以获取这些附加信息。

变量写保护

可以使用new-variable 的option选项 在创建变量时,给变量加上只读属性,这样就不能给变量重新赋值了。

ps c:\test> new-variable num -value 100 -force -option readonly
ps c:\test> $num=101
cannot overwrite variable num because it is read-only or constant.
at line:1 char:5
+ $num <<<< =101   + categoryinfo     : writeerror: (num:string) [], sessionstateunauthorizedaccessexception   + fullyqualifiederrorid : variablenotwritable ps c:\test> del variable:num
remove-item : cannot remove variable num because it is constant or read-only. if the variable is read-only,
ration again specifying the force option.
at line:1 char:4
+ del <<<< variable:num
  + categoryinfo     : writeerror: (num:string) [remove-item], sessionstateunauthorizedaccessexcepti
  + fullyqualifiederrorid : variablenotremovable,microsoft.powershell.commands.removeitemcommand

但是可以通过删除变量,再重新创建变量更新变量内容。

ps c:\test> del variable:num -force
ps c:\test> $num=101
ps c:\test> $num
101

有没有权限更高的变量,有,那就是:选项constant,常量一旦声明,不可修改

ps c:\test> new-variable num -value "strong" -option constant

ps c:\test> $num="why? can not delete it."
cannot overwrite variable num because it is read-only or constant.
at line:1 char:5
+ $num <<<< ="why? can not delete it."   + categoryinfo     : writeerror: (num:string) [], sessionstateunauthorizedaccessexception   + fullyqualifiederrorid : variablenotwritable ps c:\test> del variable:num -force
remove-item : cannot remove variable num because it is constant or read-only. if the variable is read-only,
ration again specifying the force option.
at line:1 char:4
+ del <<<< variable:num -force
  + categoryinfo     : writeerror: (num:string) [remove-item], sessionstateunauthorizedaccessexcepti
  + fullyqualifiederrorid : variablenotremovable,microsoft.powershell.commands.removeitemcommand

变量描述
在new-variable 可以通过-description 添加变量描述,但是变量描述默认不会显示,可以通过format-list 查看。

ps c:\test> new-variable name -value "me" -description "this is my name"
ps c:\test> ls variable:name | fl *

pspath    : microsoft.powershell.corevariable::name
psdrive    : variable
psprovider  : microsoft.powershell.corevariable
psiscontainer : false
name     : name
description  : this is my name
value     : me
visibility  : public
module    :
modulename  :
options    : none
attributes  : {}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网