PowerShell个人总结

mac2022-06-30  123

//很多函数前面要加- //行开头函数要大写,非行开头函数可以不大写 //函数可以大写也可以小写 $a="dqwd" $b=123 Set-Variable a 100 /*给a赋值100*/ Set-Variable b "dqwd" get-variable a,b /*获取多个值,以报表的形式打印*/ clear-variable a /*清空了之后,在此输入$a就无反应*/ remove-variable a /*删除变量的值与清空好像一样,都无反应,以后在研究*/ $c=$a+"and"+$b /*连接字符串a和b,并增加and*/ $date=Get-Date /*将当前时间赋给date*/ $date.AddDays(3) /*将当前天加3*/ $date.AddYears(2) /*将当前年加2*/ $date.AddMonths(4) /*将当前月加4*/ $Get-History /*获取你曾经输过的*/ Get-Date /*获取当前日期*/ Get-Host /*获取表示当前主机程序的对象*/ Get-Member /*获取当前对象的属性和方法*/ /*$a=3 $a|get-member*/ Get-Random /*获取随机数*/ Set-Date /*修改当前系统事件*/ $^ /*前一命令行的第一个标记*/ $$ /*前一命令行的最后一个标记*/ $? /*前一命令的执行成功与否,成功返回true,失败返回false*/ $Error /*列出你之前做错的*/ $Home /*列出当前目录*/ $a="1","2","3" /*这是数组 用a[0]输出第一个元素*/ # /*注释*/ eq:equal gt:gather than 大于 It:less than 小于 contains:包含 -eq -ne -gt -it -ge -le 1,2,3,4,5 -eq 3 /*-eq与左边有空格,与右边没有*/ $a.substring(0,3) /*取字符串的第0位到2位*/ $a="dqwd"*2 /*让这个字符串重复3次*/ "abcd" -replace "bc","test" /*把abcd中的bc用test替换*/ $a=100 $a -is [int] /*-is判断某某是不是这种类型*/ 1 -as [string] /*把1当作字符串处理 接下来再用as判断是不是string会false*/ Write-Host /*类似于C语言的printf*/ >变红 /*相当于嵌套*/ (1) $a=10 function one("this is china $a") function two($a=20;one) one /*this is china 10*/ two /*this is china 20*/ one /*this is china 10*//*没有改变变量的原始值*/ (2) $a=10 function one("this is china $a") function two($Script:a=20;one) one /*this is china 10*/ two /*this is china 20*/ one /*this is china 20*/ foreach类型:(3) $a=7..10 foreach($n in $a) { $n*$n } 输出结果是:49 64 81 100 foreach($file in dir c:\Python27) { if($file.Length -gt 1mb) { $File.Name } } while类型:(4) $a=0 while($a -le 3) { write-host $n $n++ } do-while类型(5) $n=0 do { write-host $n $n++ } while($n -ne 3) do-until类型:(6) $n=0 do { write-host $n $n++ } until($n -gt 3)
最新回复(0)