[TOC]
前言
人生莫大的痛苦,莫过于学习下面这些命令参数,但是为了更好的掌握它又不得不去了解;
PS脚本语言语Linux上的Shell有一致之处,这也是我为什么脱离了Bat而进入PS坑的原因;
进程与服务
-Process
进程常用cmdlet命令:1
2
3
4
5
6
7#1.获取进程相关的cmdlet命令
PS > (Get-Command *-Process).Name #值得学习
Debug-Process
Get-Process
Start-Process
Stop-Process
Wait-Process
基础示例:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#1.获取进程进行 (gps / ps)
get-Process
get-Process -Name notepad #获取指定运行经常的消息
# Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
# ------- ------ ----- ----- ----- ------ -- -----------
# 467 37 20916 50980 248 2.01 5600 notepad
#2.启动进程( saps / start)
Start-Process C:\Windows\System32\notepad.exe 1.txt -WindowStyle Maximized # hidden
#3.使进程等待 (关闭)
Wait-Process -Id 5600
Wait-Process -Name notepad -Timeout 10
#4.停止进程 ( spps / kill)
Stop-Process -ID 7960
Stop-Process -Name notepad -Force #强制结束
-Services
服务常用的cmdlet命令:1
2
3
4
5
6
7
8
9> (Get-Command *-Service).Name
Get-Service
New-Service
Restart-Service
Resume-Service #恢复挂起服务
Set-Service
Start-Service
Stop-Service
Suspend-Service #延缓挂起服务
基础信息:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#1.服务信息获取
Get-Service -Name RpcSs
# Status Name DisplayName
# ------ ---- -----------
# Running RpcSs Remote Procedure Call (RPC)
Get-Service RpcSs -RequiredServices #查看其被依赖的的服务
# Running DcomLaunch DCOM Server Process Launcher
# Running RpcEptMapper RPC Endpoint Mapper
#2.启动服务
Start-Service RpcSs
Start-Service -Name RpcSs
#3.暂停服务
Stop-Service RpcSs -Force
网络连接
Test-Connection
描述:可以类比于cmd中的nbtstat进行获取局域网中的指定计算机名的IPv4/6地址信息以及MAC地址;
1 | #1.获取本机计算机名相关信息 |
变量操作
描述:为了管理变量PS提供了五个专门管理变量的命令
- Get-Variable
- Set-Variable
- New-Variable
- Remove-Variable
- Clear-Variable
GetType
描述:获取变量的类型1
2
3
4
5PS C:\Users\WeiyiGeek> Get-Help gettype*
PS C:\Users\WeiyiGeek> $var=1024;$var.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
New-Variable
描述:以在定义变量时指定变量的一些其它属性,比如访问权限描述;
变量的选项是一个枚举值包含:
- “None”:默认设置
- “ReadOnly”:变量只读,但是可以通过-Force 选项更新。
- “Constant”:常量一旦声明,在当前控制台不能更新。
- “Private”:只在当前作用域可见,不能贯穿到其它作用域
- “AllScope”:全局,可以贯穿于任何作用域
基础实例:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#1.使用New-Variable命令实例
PS C:\test> New-Variable num -Value 100 -Force -Option readonly #option选项 在创建变量时给变量加上只读属性
PS C:\test> new-variable num -Value "strong" -Option constant #常量一旦声明不可修改,权限更高的变量选项Constant,
PS C:\test> $num=101
# Cannot overwrite variable num because it is read-only or constant.
#2.变量描述可以通过-description 添加变量描述,但是变量描述默认不会显示,可以通过Format-List 查看。
PS C:\test> new-variable name -Value "me" -Description "This is my name"
PS C:\test> ls Variable:name | fl *
# PSPath : Microsoft.PowerShell.CoreVariable::name
# PSDrive : Variable
# PSProvider : Microsoft.PowerShell.CoreVariable
# PSIsContainer : False
# Name : name
# Description : This is my name
Set-Variable
描述:设置变量1
2
3#1.设置变量为只读属性并且进行描述强制执行
$var="SetVariable"
Set-Variable var -Option "ReadOnly" -Description "This is readOnly" -Force
Get-Variable
描述:利用该命令获取创建的变量相关的信息;
1 | #0.常规查看变量 |
时间日期
Get-Date
描述:时间日期对象可以直接利用其属性计算出当前年中的天数以及几天前和一天后的日期等等;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34Get-Date | Format-Custom {$_} #$_表示管道中当前对象
class DateTime
{
$_ = class DateTime
{
Day = 27
DayOfWeek = Wednesday
DayOfYear = 331
Hour = 15
Kind = Local
Millisecond = 132
Minute = 41
Month = 11
Second = 5
Ticks = 637104660651327983
TimeOfDay =
class TimeSpan
{
Ticks = 564651327983
Days = 0
Hours = 15
Milliseconds = 132
Minutes = 41
Seconds = 5
TotalDays = 0.653531629609954
TotalHours = 15.6847591106389
TotalMilliseconds = 56465132.7983
TotalMinutes = 941.085546638333
TotalSeconds = 56465.1327983
}
Year = 2019
DateTime = 2019年11月27日 15:41:05
}
}
1 | #系统当前时间 |
[DateTime]类
1 | #时间加减 |
[Math] 类
描述:这是一个数学类里面定义很多实用的静态方法;1
2
3
4
5
6
7#例如.求绝对值,三角函数,取整
PS > [Math]::Abs(-10.89)
10.89
PS > [Math]::Sin([Math]::PI/2)
1
PS > [Math]::Truncate(2012.7765)
2012
[NET] 类
描述:.NET支持成千上万的类型,有了这些类型可以做许多事情,幸运的是Powershell恰好支持这些类型。
1 | #1.例如使用System.Net.IPAddress类将字符串IP地址转换成一个IPAddress实例 |
[AppDomain] 类
.NET中的类型定义在不同的程序集中,首先得知道当前程序已经加载了那些程序集;
AppDomain类可以完成这个需求,因为它有一个静态成员CurrentDomain,CurrentDomain中有一个GetAssemblies()方法。1
2
3
4
5
6
7
8PS > [AppDomain]::CurrentDomain
PS > [AppDomain]::CurrentDomain.GetAssemblies()
GAC Version Location
--- ------- --------
True v4.0.30319 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll
PS > [AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object { $_.GetExportedTypes() } | Where-Object { $_ -like $searchtext } | ForEach-Object { $_.FullName }
[Environment] 类
1 | PS > [Environment]::UserDomainName #当前登录域 |