PowerShell 基本语法

前言全局说明

PowerShell 基本语法


一、运算符

#加 减 乘 除 取余
+ - * / %

#自增、自减运算符,和C#或C++完全一致
++$i
$i++
$i--
--$i
#且
-and

#并
-or

#非
-not
!

#样例
$true -and $false -or -not 0
!$true
#输出
True
False
#等于 (equal to)
-eq
#不等于 (not equal to)
-ne
#大于 (greater than)
-gt
#大于等于 (greater than or equal to)
-ge
#小于 (less than)
-lt
#小于等于 (less than or equal to)
-le

#对于字符串类型,可以在比较运算符前加i或c表示是否区分大小写,
#i表示对大小写不敏感,c表示敏感,不写默认不敏感
"a" -eq "A"
"a" -ceq "A"
#输出
True
False
#判断变量是否为兼容类型(同一类型或其父类型),类型格式为 [类型名]
-is
#示例
$a=0
$a -is [int]
$b=1,2,3
$b -is [array]
$a -is [ValueType]
#输出
True
True
True # System.Int32类继承自System.ValueType类,因此int是其父类型ValueType的子类型

二、逻辑判断

2.1 if-else、elseif

2.1.1. 示例
if($true -and $true) {
    $a=0
}
elseif($a -eq $b) {
    $a=1
}
else {
    $a=2
}

2.2 for 循环

2.2.1 示例
For($i=0;$i -lt $args.Count; $i++)
{
   Write-Host "parameter $i : $($args[$i])"
}
2.2.2 结果
PS E:> .MyScript.ps1 www moss fly com
parameter 0 : www
parameter 1 : moss
parameter 2 : fly
parameter 3 : com

三、



四、




免责声明:本号所涉及内容仅供安全研究与教学使用,如出现其他风险,后果自负。




参考、来源:
https://zhuanlan.zhihu.com/p/76708298?utm_id=0 (PowerShell入门指南(三)·一门新的编程语言)
https://www.pstips.net/powershell-pass-args-to-scripts.html



posted @ 2024-01-31 11:47  悟透  阅读(725)  评论(0)    收藏  举报