命令的参数
开关参数
当它存在它就代表某个值为true,不存在则为false
比如 -recurse
位置参数
一些参数有固定的位置,另一些参数则有名字。可以使用help来看这些参数的Position属性(不过我用help没看到这些)
-path <string[]>
Specifies a path to one or more locations. Wildcards are
permitted. The default location is the current directory (.).
Required? false
Position? 1
Standard value used <NOTE: if not specified uses
the Current location>
Accept pipeline input? true (ByValue, ByPropertyName)
Accept wildcard characters? true
当一个命令的参数被解析的时候,它会先解析带名字的参数,然后把剩下的参数按照位置赋值。
例如
Get-ChildItem c:\windows *.exe -recurse -name
Get-ChildItem -recurse -name c:\windows *.exe
Get-ChildItem -name c:\windows *.exe -recurse
首先那些带名字的参数会先被解析:-recurse -name,
然后因为-path的位置是1,而且它还没有被赋值,所以c:\windows会被赋给-path,同理 *.exe会被赋给 -filter
但在实践中,为了可读性,不建议采用参数名缩写,方法别名或以上位置参数。
公共参数
你可以通过get-help get-childitem -detailed 看到公共参数的详细信息
This cmdlet supports the common parameters: -Verbose,
-Debug, -ErrorAction, -ErrorVariable, and -OutVariable.
For more information, type "get-help about_commonparameters".
方法别名
在ps中,很多方法都有别名,有时候是因为历史原因,有时候是为了简化写法,比如Get-ChildItem就有2个别名 Dir 和 ls
$alias:Dir
Get-ChildItem
$alias:ls
Get-ChildItem
你可以使用 Dir alias: 查看所有别名
如果你只想看某一个命令的别名可以使用
PS C:\Users\LIHUANG\Desktop\common> Get-Alias -Name dir
CommandType Name ModuleName
----------- ---- ----------
Alias dir -> Get-ChildItem
如果想知道get-childitem总共有多少个别名,稍微有一点复杂,需要用到管道命令,在第五章我们会讲到。
PS C:\Users\LIHUANG\Desktop\common> Get-Alias | Where-Object {$_.Definition -eq "Get-ChildItem"}
CommandType Name ModuleName
----------- ---- ----------
Alias dir -> Get-ChildItem
Alias gci -> Get-ChildItem
Alias ls -> Get-ChildItem
或者
PS C:\Users\LIHUANG\Desktop\common> Dir alias: | Out-String -Stream | Select-String "Get-ChildItem"
Alias dir -> Get-ChildItem
Alias gci -> Get-ChildItem
Alias ls -> Get-ChildItem
这条命令是在转换字符,out-strig将dir alias:转化成为字符串(本来是object),参数-stream保证每一个object转化完之后就直接传入后面的命令。
select-string 筛选出包含’get-childitem‘的字符串。
还有一个小例子可以演示ps的强大
PS C:\Users\LIHUANG\Desktop\common> Dir alias: | Group-Object definition
Count Name Group
----- ---- -----
2 ForEach-Object {%, foreach}
2 Where-Object {?, where}
1 Add-Content {ac}
1 Add-PSSnapIn {asnp}
3 Get-Content {cat, gc, type}
3 Set-Location {cd, chdir, sl}
1 Clear-Content {clc}
2 Clear-Host {clear, cls}
1 Clear-History {clhy}
1 Clear-Item {cli}
1 Clear-ItemProperty {clp}
1 Clear-Variable {clv}
1 Connect-PSSession {cnsn}
2 Compare-Object {compare, diff}
3 Copy-Item {copy, cp, cpi}
1 Copy-ItemProperty {cpp}
1 Convert-Path {cvpa}
....
set-alias edit notepad.exe
在使用set-alias之前,edit会让ps进入一个编辑器,你可以直接关闭ps退出这个编辑器,也可以使用Alt+F 然后 X 退出。
在使用set-alias之后,edit会启动记事本。
别名只在ps运行时有效,当ps退出别名就会失效。
如果你想一直使用别名,那么你可以每次都手动添加别名,或者把别名设置放在start profile里,(start profile会在第十章讲到),或者使用import/export方法。
Export-Alias 使用它的时候,会提示你需要设置一个名字。在这里我们设置为alias1
Import-Alias alias1. 这样就可以将之前使用到的alias导入了。
也许你会遇到错误
Import-Alias alias1
Import-Alias : Alias not allowed because an alias with the
name "ac" already exists.
At line:1 char:13
+ Import-Alias <<<< alias1
这是因为alias1里保存了所有的别名,包括系统中默认存在的。所以你可以使用
Import-Alias alias1 -Force 这样如果有别名已经存在就强制覆盖它。
删除别名
Del alias:edit
Del命令不仅可以删除别名,也可以删除文件和文件夹。
Tips:你可以使用以下代码找到所有空别名:
Get-Alias | ForEach-Object {if (!(Get-Command $_.Definition -ea SilentlyContinue)) {$_}}
方法:’扩展的’别名
有一个大家都很熟悉的方法ping,假设你需要经常使用下面的代码
Ping -n 1 -w 100 10.10.10.10
你想给它设置别名,因为参数每次都是一样的。但别名无法起作用。此时就可以定义一个新的方法。
>function quickping { ping -n 1 -w 100 $args }
>quickping 10.10.10.10
Pinging 10.10.10.10 with 32 bytes of data:
Reply from 10.10.10.10: bytes=32 time<1ms TTL=128
Ping statistics for 10.10.10.10:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
>Set-Alias qp quickping
>qp 10.10.10.10
Pinging 10.10.10.10 with 32 bytes of data:
Reply from 10.10.10.10: bytes=32 time<1ms TTL=128
$args 表示你可以添加任意多的参数(>= 0)
方法的存活时间也跟别名一样,随着ps的关闭而失效。
调用方法和脚本
调用方法和脚本和调用文件的时候一样,你需要指定一个路径,相对路径或者绝对路径。
在之前的cmd中,如果在当前路劲下有一个名为ping.bat的文件,当你要执行它的时候,你可以直接输入
ping
但在ps中,你必须得指定路径,所以是
.\ping
这样做的好处是,它可以避免一些陷阱。假设有人写了一个病毒程序,名字叫做ping.bat,当你每次想运行ping 10.10.10.10的时候,该程序就会运行,而在ps中,它只会执行真正ping。
调用vb脚本或者ps自己的脚本的时候,需要带后缀名.vbs/.ps1.
如果在执行脚本的时候遇到错误,有可能是因为policy不允许,
你可以采用get-ExecutionPolicy 看看你当前的设置是什么。
一般来说设置成 RemoteSigned 是比较合适的,意思是只限制从远程获得的脚本。
关于ExecutionPolicy 会在第十章讲到