您的位置:首页 > 运维架构 > Shell

Windows PowerShell基本命令总结(二)

2011-05-25 16:25 155 查看
先来个开场白。问“忘情水是谁给的”,答“啊哈给的”。这是为什么呢,因为歌词“啊哈给我一杯忘情水”。

这个跟主题没关系哦,好的,我们继续聊聊PowerShell。这篇总结了十大参数,if和switch语句,条件和比较操作。

十大参数:

1) -ErrorAction SilentlyContinue //出现错误不提示
2) -MemberType (Get-Member) //get-member -MemberType 获取对象类型
3) -recurse with Get-ChildItem (Sub-directories) //子目录也显示
4) -force with Get-ChildItem (Lists hidden files) //隐藏文件也显示 Get-ChildItem -recurse -force
5) -auto (Adjust the width with Format-Table) // ft -auto
6) -groupBy (Collate similar items) //分组
7) -filter (Get-Wmiobject "Win32_Process")
8) -list with Get-WmiObject (Get-Eventlog -list)
9) -com (new-Object)
10) -whatif (Test before you commit)

知道某个命令,想知道这个命令的参数,那就用这个命令行:

get-help get-eventlog -full //添加-full参数能显示例子

知道某个参数,想知道哪些命令可以用这个参数,那就用这个命令行:

get-command | where {$_.parameters.keys -contains "groupby"}

if命令:

11) if(...) {...}

12) if else

13) if else if else

14) if -not

15) if -and

16) if -or

例1:

$Calendar = Get-Date
If ($Calendar.day -eq '24' -And $Calendar.Month -eq '12') {
"Christmas Day"
}
Else {
"Not Christmas today!"
}

switch命令

17) switch (...) {...}

Switch (3) { 1{ "Red" } 2{ "Yellow" } 3{ "Green" } }

conditional operators,like 和match的区别详见本文最下方的例子:

18) -like

19) -match

20) -contains

比较参数

21) -lt //less than

22) -gt //greater than

23) -eq //equal

like 和match的区别:

PS C:/Users/xxx> Get-Service |where {$_.name -match "wm"}

Status Name DisplayName
------ ---- -----------
Running nvspwmi Hyper-V Networking Management Service
Running wmiApSrv WMI Performance Adapter

PS C:/Users/xxx> Get-Service |where {$_.name -like "wm"}
PS C:/Users/xxx> Get-Service |where {$_.name -like "*wm*"}

Status Name DisplayName
------ ---- -----------
Running nvspwmi Hyper-V Networking Management Service
Running wmiApSrv WMI Performance Adapter

PS C:/Users/xxx> Get-Service |where {$_.name -like "*wm"}
PS C:/Users/xxx> Get-Service |where {$_.name -like "wm*"}

Status Name DisplayName
------ ---- -----------
Stopped wmiApSrv WMI Performance Adapter
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: