PowerShell 初探

来源:互联网 发布:高仿香港身份证淘宝 编辑:程序博客网 时间:2024/06/05 03:54

1.PowerSehl 运行方式

命令控制台窗口中,输入powershell进入Powershell命令后环境。Windows中可以通过get-executionpolicy命令查看PowerShell运行的方式。

Windows中PowerShell默认运行方式是Restrict,也就是默认禁止PowerShell脚本执行。更改脚本执行方式可以通过set-executionpolicy unrestricted

2.Powershell变量

PowerShell中变量的命名是以$开头,字符数字等命名.如$str="Hello PowerShell".

3.PowerShell中函数

PowerShell中函数定义以Function为关键字。

Function LogW($str)

{

echo $str

 }

$logpath="d:\$(gc env:computername).log"  

definition:

Function LogWrite

{

Param([string]$logstring)

Add-content $logpath -value $logstring

 }

Use method 

LogWrite "Hello Powershell"

LogWrite "PS demo"

3. 条件/控制/循环语句

if(test-path $logpath)

{

echo "$logpath exists"

 }

else

{

echo "$logpath not exists"

 }

$n=2

switch

{

0 {"equal 0"}

1 {"equal 1"}

2 {"equal 2"}

  default {" unknown"}

 }

$arr=1..5

foreach ($n in $arr)

{

$n * $n

 }

4. 返回值

函数返回值,函数体内直接return 返回值,程序返回值exit 返回值.

...



 

0 0
原创粉丝点击