reference/docs/powershell.md

419 lines
8.6 KiB
Markdown
Raw Normal View History

2024-10-29 20:09:26 +08:00
PowerShell 备忘清单
===
2024-10-29 20:01:50 +08:00
2024-10-30 04:16:36 +08:00
PowerShell 用于自动化任务和配置管理的常用命令的备忘清单,可帮助系统管理员快速参考常用操作。
2024-10-29 20:09:26 +08:00
常用操作
---
2024-10-29 20:01:50 +08:00
2024-10-29 20:09:26 +08:00
### 辅助命令
2024-10-29 21:27:34 +08:00
<!--rehype:wrap-class=row-span-2-->
2024-10-29 20:01:50 +08:00
2024-10-29 20:09:26 +08:00
**_PowerShell 的命令遵循动词-名词格式_** 一些常见的动词:
2024-10-29 20:01:50 +08:00
| 动词 | 描述 |
| ------- | ------------------------ |
| Get | 用于检索信息 |
| Set | 用于配置或更改设置 |
| New | 用于创建新对象实例 |
| Remove | 用于删除或移除项目 |
| Invoke | 用于执行特定的操作或动作 |
| Start | 用于启动进程或操作 |
| Stop | 用于停止或终止进程或操作 |
| Enable | 用于激活或启用功能 |
| Disable | 用于停用或禁用功能 |
| Test | 用于执行测试或检查 |
| Update | 用于更新或刷新数据或配置 |
列出可用模块
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Get-Module --ListAvailable
```
列出可用的 cmdlet 和函数
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Get-Command -Module ActiveDirectory
```
2024-10-29 20:09:26 +08:00
列出别名及其对应的 cmdlet 名称
```PowerShell
Get-Alias | Select-Object Name, Definition
```
2024-10-29 20:01:50 +08:00
获取帮助
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Get-Help <cmd>
Get-Help <cmd> -Examples
Get-Help -Name Get-Process -Parameter Id
```
**Get-Member:** 显示对象的属性和方法
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Get-Process | Get-Member
```
### 对象操作
2024-10-29 21:27:34 +08:00
<!--rehype:wrap-class=col-span-2-->
2024-10-29 20:01:50 +08:00
**Select-Object:** 选择对象的特定属性或自定义其显示
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Get-Process | Select-Object Name, CPU
```
**Where-Object:** 根据指定条件过滤对象
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Get-Service | Where-Object { $PSItem.Status -eq 'Running' }
#OR
Get-Service | ? { $_.Status -eq 'Running' }
```
**Measure-Object:** 计算对象属性的统计信息,如总和、平均值和计数
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Get-Process | Measure-Object -Property WorkingSet -Sum
```
**ForEach-Object:** 对集合中的每个对象执行操作(注意:以下命令将为当前目录中的文件/文件夹添加前缀)
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Get-ChildItem | ForEach-Object { Rename-Item $_ -NewName "Prefix_$_" }
```
**Sort-Object:** 按指定属性对对象进行排序
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Get-ChildItem | Sort-Object Length -Descending
```
**Format-Table:** 将输出格式化为带有指定列的表格
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Get-Service | Format-Table -AutoSize # ft alias
```
**Format-List:** 将输出格式化为属性和值的列表
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Get-Process | Format-List -Property Name, CPU # fl alias
```
2024-10-29 20:09:26 +08:00
### 文件系统
2024-10-29 21:27:34 +08:00
<!--rehype:wrap-class=col-span-2-->
2024-10-29 20:01:50 +08:00
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
New-Item -path file.txt -type 'file' -value 'contents'
New-Item -path file.txt -type 'dir'
Copy-Item <src> -destination <dest>
Move-Item -path <src> -destination <dest>
Remove-Item <file>
Test-Path <path>
Rename-Item -path <path> -newname <newname>
# using .NET Base Class Library
[System.IO.File]::WriteAllText('test.txt', '')
[System.IO.File]::Delete('test.txt')
Get-Content -Path "test.txt"
Get-Process | Out-File -FilePath "processes.txt"# 输出到文件
Get-Process | Export-Csv -Path "processes.csv" # 输出到 CSV
$data = Import-Csv -Path "data.csv" # 从 CSV 导入
```
2024-10-29 20:09:26 +08:00
系统管理
---
### 获取信息
2024-10-29 20:01:50 +08:00
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
# 获取 BIOS 信息
Get-CimInstance -ClassName Win32_BIOS
# 获取本地连接的物理磁盘设备信息
Get-CimInstance -ClassName Win32_DiskDrive
# 获取安装的物理内存RAM信息
Get-CimInstance -ClassName Win32_PhysicalMemory
# 获取安装的网络适配器(物理 + 虚拟)信息
Get-CimInstance -ClassName Win32_NetworkAdapter
# 获取安装的图形/显卡GPU信息
Get-CimInstance -ClassName Win32_VideoController
2024-10-29 20:09:26 +08:00
```
### 命名空间 & 类
2024-10-29 20:01:50 +08:00
2024-10-29 21:27:34 +08:00
列出所有类名
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Get-CimClass | Select-Object -ExpandProperty CimClassName
2024-10-29 21:27:34 +08:00
```
<!--rehype:className=wrap-text-->
探索 root\cimv2 命名空间中的各种 WMI 类
```PowerShell
2024-10-29 20:01:50 +08:00
Get-CimClass -Namespace root\cimv2
2024-10-29 21:27:34 +08:00
```
<!--rehype:className=wrap-text-->
探索 root\cimv2 命名空间下的子 WMI 命名空间
```PowerShell
2024-10-29 20:01:50 +08:00
Get-CimInstance -Namespace root -ClassName __NAMESPACE
```
2024-10-29 21:27:34 +08:00
<!--rehype:className=wrap-text-->
2024-10-29 20:01:50 +08:00
### 网络管理
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
# 测试与远程主机的网络连接
Test-Connection -ComputerName google.com
# 获取网络适配器信息
Get-NetAdapter
# 获取 IP 地址信息
Get-NetIPAddress
# 获取路由表信息
Get-NetRoute
# 测试远程主机上的端口是否开放
Test-NetConnection google.com -Port 80
```
### 用户和组管理
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
# 获取本地用户账户信息
Get-LocalUser
# 创建新的本地用户账户
New-LocalUser -Name NewUser -Password (ConvertTo-SecureString "Password123" -AsPlainText -Force)
# 删除本地用户账户
Remove-LocalUser -Name UserToRemove
# 获取本地组信息
Get-LocalGroup
# 将成员添加到本地组
Add-LocalGroupMember -Group Administrators -Member UserToAdd
```
### 安全性和权限
2024-10-29 21:27:34 +08:00
获取文件/目录的访问控制列表
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Get-Acl C:\Path\To\File.txt
2024-10-29 21:27:34 +08:00
```
设置文件/目录的访问控制列表
2024-10-29 20:01:50 +08:00
2024-10-29 21:27:34 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
Set-Acl -Path C:\Path\To\File.txt -AclObject $aclObject
```
2024-10-29 21:27:34 +08:00
<!--rehype:className=wrap-text-->
2024-10-29 20:01:50 +08:00
### 注册表管理
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
# 获取注册表键值
Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Select DisplayName, DisplayVersion
# 设置注册表键值
Set-ItemProperty -Path "HKLM:\Software\MyApp" -Name "SettingName" -Value "NewValue"
# 创建新的注册表键值
New-ItemProperty -Path "HKCU:\Software\MyApp" -Name "NewSetting" -Value "NewValue" -PropertyType String
# 删除注册表键值
Remove-ItemProperty -Path "HKCU:\Software\MyApp" -Name "SettingToRemove"
# 检查注册表键是否存在
Test-Path "HKLM:\Software\MyApp"
```
## 脚本
2024-10-29 20:09:26 +08:00
### 变量
2024-10-29 20:01:50 +08:00
初始化变量,指定或不指定类型:
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
$var = 0
[int] $var = 'Trevor' # (抛出异常)
[string] $var = 'Trevor' # (不会抛出异常)
$var.GetType()
# 多重赋值
$a,$b,$c = 'a','b','c'
# 创建数组
$arrayvar = @('va1','va2')
# 创建字典
$dict = @{k1 = 'test'; k2 = 'best'}
```
变量命令
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
New-Variable -Name FirstName -Value Trevor
New-Variable FirstName -Value Trevor -Option <ReadOnly/Constant>
Get-Variable
Get-Variable | ? { $PSItem.Options -contains 'constant' }
Get-Variable | ? { $PSItem.Options -contains 'readonly' }
Remove-Variable -Name firstname
# 删除只读变量
Remove-Variable -Name firstname -Force
```
变量类型int32, int64, string, bool
### 运算符
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
# 运算符
# (a <op> b)
= , += / -= , ++ / --
-eq / -ne , -lt / -gt , -le / -ge
$FirstName = 'Trevor'
$FirstName -like 'T*'
$true; $false # 布尔值 true/false
# 三元运算符
$FoodToEat = $BaconIsYummy ? 'bacon' : 'beets'
# -notin 或 -in
'Celery' -in @('Bacon', 'Sausage', 'Steak')
# 输出: True
5 -is [int32]
# 正则表达式匹配,可以使用数组
'Trevor' -match '^T\w*'
# 查找多个匹配项
$regex = [regex]'(\w*)'
$regex.Matches('this is test').Value
```
### Structure
#### 输入输出操作
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
"This displays a string"
Write-Host "color" -ForegroundColor Red
$age = Read-host "Enter age"
$pwd = Read-host "password" -asSecureString
Clear-Host
```
#### 流控制
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
IF(<#Condition#>){
<#Commands#>}ELSEIF(){}ELSE{}
Switch($var){
2024-10-29 20:09:26 +08:00
"val1"{<#Commands#>; break}
2024-10-29 20:01:50 +08:00
"val2"{<#Commands#>; break}
}
For($ct=0;$ct -le 3;$ct++){}
ForEach($var in $arr){}
while($var -ne 0){}
Do{}While()
```
### 函数 / 模块
#### 示例 1
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
function funcname{
[CmdletBinding()]
2024-10-29 20:09:26 +08:00
param(
[Parameter(Mandatory)]
[String]$user
)
Write-Host "welcome " $user
2024-10-29 20:01:50 +08:00
return "value"
}
$var = funcname -user pcb
```
#### 示例 2
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
function Get-EvenNumbers {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true)]
[int] $Number
)
begin {<#command#>}
process {
if ($Number % 2 -eq 0) {
Write-Output $Number
}
}
end {<#command#>}
}
1..10 | Get-EvenNumbers
```
#### 模块
2024-10-29 20:09:26 +08:00
```PowerShell
2024-10-29 20:01:50 +08:00
# PowerShell 在路径中查找模块
$env:PSModulePath
# 列出系统上安装的所有模块
Get-Module -ListAvailable
# 列出当前会话中导入的模块
Get-Module
Import-Module <moduleName>
Remove-Module <moduleName>
Find-Module -Tag cloud
Find-Module -Name ps*
# 创建一个内存中的 PowerShell 模块
New-Module -Name trevor -ScriptBlock {
function Add($a,$b) { $a + $b } }
```
### 注意
- 在大多数语言中,转义字符是反斜杠 **\\**,而在 PowerShell 中是反引号 **`**
## 参考
2024-10-29 20:09:26 +08:00
- [Microsoft PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/samples/sample-scripts-for-administration?view=powershell-7.3) _(learn.microsoft.com)_
2024-10-29 20:01:50 +08:00
- [cheatsheets](https://cheatsheets.zip/powershell)