导图社区 PowerShell
这是一篇关于PowerShell的思维导图,主要内容有基础语法、PowerShell的配置文件、WMI对象(各种设备)、ADSI对象(活动目录)。
编辑于2022-07-17 17:06:56PowerShell
基础语法
常量、变量、数组、哈希
各种字符串操作
字符连接
$strC = $strA + $strB
字符串替换
$strB = $strA -replace "hi!", "Hello"
使用常量和变量
所有的变量以“$”开头,用“=”来给变量赋值
使用"set-variable"
set-variable -name StrUser -value "MaRui"
使用“set-variable”命令时在变量名前不需要使用“$”
屏幕输出变量值
write-output $StrUser
$StrUser
各种运算符和表达式
逻辑运算符
• -eq 判断是否等于(equal)
• -lt 判断时候小于(less than)
• -gt 判断是否大于(greater than)
• -ge 判断是否大于或等于(greater of equal)
• -le 判断是否小于或等于(less or equal)
• -ne 判断是否不等于(no equal)
"MaRui" -eq "marui" <enter>
结果:"True"
"MaRui" -ieq "MARUI" <enter>
结果:"True"
"MaRui" -ceq "MARUI" <enter>
结果:"False"
• -and 与
• -or 或
• -not 非
• ! 非
创建、修改、合并数组或哈希表
数组
$strUsers=@("user1","user2","user3")
在PowerShell中,声明一个变量为数组时,需要使用符号"@"
数组合并
+
哈希表
$age=@{"MaRui"=21;"Lee"=27;"Tom"=53}
声明哈希表变量时,同样需要用到符号"@"
查询
$age["MaRui"]
增加(注意,增加的同时一定要赋值,否则不会添加新条目)
$age["Ma"]=24
修改
$age["Ma"]=25
删除(注意,使用圆括号)
$age.remove("Ma")
清除哈希表
$age.clear()
判断
if (条件) {代码} elseif (条件) {代码} else (条件) {代码} else {代码}
switch (表达式) { (表达式) {代码} 值 {代码} default {默认执行代码} }
switch ((Get-WmiObject -Class win32_ComputerSystem).domainRole) { 0 {Write-Host "Standalone Workstation"} 1 {Write-Host "Member Workstation"} 2 {Write-Host "Standalone Server"} 3 {Write-Host "Member Server"} 4 {Write-Host "Backup Domain Controller"} 5 {Write-Host "Primary Domain Controller"} default {Write-Host "Cannot determine domain role"} }
switch (100) { (99 + 1) {Write-Host "99+1=100"} (1 + 100) {Write-Host "1+100=100"} (50*2) {Write-Host "50*2=100"} (33.333*3) {Write-Host "33.333*3=100"} }
循环
for (初值;表达式;赋值语句) {代码}
foreach (成员变量 in 数组) {代码}
foreach-object
对一组输入的每个对象执行运算,$_代表每一个对象
30000,56798,12432 | foreach-object -process {$_/1024}
get-childitem d:\ | foreach-object -process { $_.length / 1024 }
while(表达式) {代码}
do {代码} while(表达式)
do {代码} until(表达式)
do { "Quit Now? (Y/N)" $input=Read-Host } until($input -eq "Y")
模块化
组织、调整代码
使用恰当的数据类型
调用其他脚本函数
类对象
Get-Member 获取类的属性和方法
get-service | get-member
Get-Service | Get-Member -MemberType Property
Get-Service | Get-Member -MemberType Method
cmdlet指令返回的都是类
Get-ChildItem -Path d:\ -Recurse | Where-Object {$_.LastWriteTime -gt "08/01/2013"}
首先,"get-childitem"是用来枚举我们的文件系统的,使用"-path"参数,将路径指向"d:\",使用"-recurse"参数,意味着将显示所有的文件,甚至是子目录下的。接下来,我们将结果使用管道符传递给循环声明"where-object"中,用来筛选出符合条件的结果
Providers
get-psprovider
get-psdrive 获取当前驱动器列表
cd env: 环境变量
ls OS
ls OS | format-list *
new-item -path . -Name EnvName -Value "NewEnv" 创建环境变量,.为当前目录
cd Function:
get-content clear-host 获取clear-host的内容
cd HKLM:
ls -Recurse | Export-CSV "d:\Cert.csv" 导出注册表
格式化输出 Get-Command Format-*
format-table
Format-Table cmdlet 将命令输出的格式设置为表,表中每列显示选定的对象属性。对象类型可确定默认布局以及每列中显示的属 性,但也可以使用 Property 参数来选择要查看的属性。
get-childitem c:\windows | format-table
get-childitem c:\windows | format-table -autosize
format-custom
Format-Custom cmdlet 将根据可选视图中的定义来设置命令输出的格式。Format-Custom 用于显示不是仅为表格或仅为列表的视图 。可使用 Windows PowerShell 目录中的 *format.PS1XML 文件中定义的视图,或者可在新的 PS1XML 文件中创建自己的视图,并 使用 Update-FormatData cmdlet 将它们添加到 Windows PowerShell 中。
get-childitem c:\windows | format-custom
format-list
Format-List cmdlet 将命令输出的格式设置为属性列表,其中每个属性均各占一行进行显示。可使用 Format-List 将对象的全部 或所选属性设置为列表格式 (format-list *) 并进行显示。
get-childitem c:\windows | format-list
get-childitem c:\windows | format-list -Property FullName
Get-ChildItem C:\Windows -Recurse | Format-List -Property FullName,CreationTime,LastWriteTime
format-wide
Format-Wide cmdlet 将对象的格式设置为只能显示每个对象的一个属性的宽表。可以使用 Property 参数来确定要显示的属性。
Get-ChildItem C: | Format-Wide -Column 3
get-childitem c:\windows | format-wide
其他格式输出
Group-Object
Get-Process | Group-Object Company
Sort-Object
Get-EventLog System | Group-Object eventid
Get-EventLog System | Group-Object eventid | Sort-Object Count -descending
Convertto-HTML
Get-Process | ConvertTo-html
Get-Process | ConvertTo-html | out-file "Processes.html"
Export-CSV
Get-Process | Export-CSV Processes.csv
打开文件
Invoke-Item Processes.csv
查看帮助
get-help
Aliases 别名
Get-Alias
Set-Alias gs Get-Service
Export-Alias -Path a.txt
Import-Alias -Path a.txt 导入只需要导入系统没有的
PowerShell的配置文件
$Profile
PowerShell执行策略分级
Restricted - 不能运行任何脚本和配置文件
AllSigned - 所有脚本和配置文件必须拥有受信任的发布者的签名
RemoteSigned - 所有脚本和配置文件从可以是互联网上下载,但必须拥有受信任的发布者的签名
Unrestricted - 所有脚本和配置文件都将运行,从互联网上下载的脚本在运行前会有提示。
Get-ExecutionPolicy
Set-ExecutionPolicy Unrestricted
验证$Profile是否存在
test-path $Profile
false
New-Item -Path $Profile -ItemType file -Force
test-path $Profile
true
notepad $Profile
WMI对象(各种设备)
WMI命名空间的概念
WMI所提供的功能
查看、使用的WMI命名空间
WMI的类
"get-wmiobject"
查询WMI
ADSI对象(活动目录)
Active Directory对象的概念
ADSI所提供的功能
Active Directory命名空间
创建和修改Active Directory对象