《Learn Windows PowerShell in a Month of Lunches Third Edition》读书笔记——CHAPTER 4 Running commands

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_41104353/article/details/82156771

4.2 The anatomy of a command

这里写图片描述

4.4 Aliases: nicknames for commands

对于过长的命令(Set-WinDefaultInputMethodOverride),我们可以使用 命令 get-alias 查看别名,如:

PS C:\> get-alias -Definition "Get-Service"
Capability      Name
----------      ----
Cmdlet          gsv -> Get-Service

值得注意的是:PowerShell里的别名并不允许跟随参数!

当使用 help + 别名 ,如 help gsv ,的时候。结果和不用别名是一样的。

Above and beyond

我们可以使用命令 New-Alias 来创建属于我们的别名。
命令 Export-Alias 来导出创建的别名
命令 Import-Alias 来导入写好的别名
值得注意的是,自创的别名只存在于当前的窗口中,一旦关闭,别名就不能用了。
建议不要自己创建别名

4.5 Taking shortcuts

除了使用别名,我们还有三种更简短的形式来写命令名字,分别如下:

4.5.1 Truncating parameter names (截短参数名字)

我们不必打全参数名字,只要前面几个字母能够唯一确定参数名字即可,如我们可以用 -comp 代替 -computerName 。但是因为有 -computerName 参数、-common 参数和 -composite 参数,所以我们至少要输入 -compu-commo-compo 来区分他们。

4.5.2 Using parameter name aliases

参数也有他们自己的别名,使用类似以下命令可以查看:

PS C:\> (get-command `get-eventlog` | select -ExpandProperty parameters).`computername`.aliases

高亮的词组是可以替换的

4.5.3 Using positional parameters

查看一个参数的位置可以在其帮助文件内按照其出现的顺序查看。
或者使用 help-full 参数查看其 position 值。

4.6 Cheating a bit: Show-Command

如果有时候我们记不住命令的参数或者不想输入,这时,我们可以使用cmdlet Show-Command 来帮助我们:
使用 show-command Get-EventLog 会弹出以下窗口
这里写图片描述
当填写完里面的参数的值后,我们可以直接运行或者复制出来再粘贴到PowerShell里运行。

4.7 Support for external commands

我们除了可以在PowerShell里面运行其内建的命令,还可以运行额外的命令行工具,如 Ping, Nslookup, Ipconfig, Net等 。不过输入这些命令的时候,PowerShell会启动Cmd.exe。

PowerShell里的cmdlet有些时候会比老的命令行更好用,比如 Test-ConnectionPing 有更多的选项。

对于外来的命令,我们得使用如下方式来让PowerShell明白:

$exe = "C:\Vmware\vcbMounter.exe"
$host = "server"
$user = "joe"
$password = "password"
$machine = "somepc"
$location = "somelocation"
$backupType = "incremental"
`&` $exe -h $host -u $user -p $password -s "name:$machine" -r $location -t
$backupType

这个 vcbMounter.exe 是一个外来的命令,其参数如下:

-h for the host name
-u for the user name
-p for the password
-s for the server name
-r for a location
-t for a backup type

对于v3以上的版本,我们只需要在外来的命令前面加上 --% 即可。
错误的:

PS C:\> $n = "bits"
PS C:\> C:\windows\system32\sc.exe --% qc $n
[SC] OpenService FAILED 1060:
The specified service does not exist as an installed service.

正确的:

PS C:\> C:\windows\system32\sc.exe --% qc bits
[SC] QueryServiceConfig SUCCESS

4.9.2 Typing parameters

PowerShell isn’t normally picky about upper- and lowercase.

猜你喜欢

转载自blog.csdn.net/sinat_41104353/article/details/82156771
今日推荐