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
这个跟主题没关系哦,好的,我们继续聊聊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
相关文章推荐
- 数据库基本命令的总结
- linux常用基本命令总结
- Mac 下终端命令行之基本命令总结(持续更新)
- Python基础-基本操作命令总结
- 对MySQL日志操作的一些基本命令总结
- MySQL 基本操作命令总结
- 自我总结:Linux系统基本命令及目录
- docker 基本命令总结(一)
- MySQL学习总结2之对表的基本命令操作
- Linux基本命令总结
- Linux基本命令总结
- 数据库基本命令的总结
- mysql desc esc 基本命令总结
- Hbase 建表基本命令总结
- git的一些基本命令总结
- Linux 基本命令 总结
- hbase--Hbase 建表基本命令总结
- Hbase 建表基本命令总结
- mysql学习总结一:mysql的安装,介绍,基本命令操作
- 项目总结之——基本命令