PowerShell脚本的基础知识
参考文档https://learn.microsoft.com/zh-cn/powershell/scripting/samples/viewing-object-structure--get-member-?view=powershell-7.3
什么是powershell
Powershell 是使用 .NET 框架构建的 Windows 脚本语言和 shell 环境。
这也允许 Powershell 直接从其 shell 执行 .NET 函数。大多数 Powershell 命令(称为cmdlet)是用 .NET 编写的。
与其他脚本语言和 shell 环境不同,这些cmdlet的输出 是对象——使 Powershell 有点面向对象。
这也意味着运行 cmdlet 允许您对输出对象执行操作(这使得将输出从一个cmdlet传递 到另一个变得很方便)。
cmdlet的正常格式使用Verb-Noun表示;例如,用于列出命令的cmdlet 被称为Get-Command.
基本的powershell命令
使用获取帮助
Get-Help Command-Name
可以通过使用-examples命令了解如何使用
例:Get-Help Get-Command -Examples
使用命令
Get-Command 获取当前计算机上安装的所有cmdlet,
Get-Command Verb* 或者 Get-Command *-Noun
例:Get-Command New-*
以查看动词 new 的所有cmdlet 显示
对象操作
每个cmdlet的输出如何 是一个对象。管道 (|) 用于将输出从一个cmdlet传递 到另一个
powershell 不是将文本或字符串传递给管道后的命令,而是将对象传递给下一个 cmdlet。
与面向对象框架中的每个对象一样,对象将包含方法和属性。您可以将方法视为可应用于 cmdlet 输出的函数,
您可以将属性视为 cmdlet 输出中的变量。要查看这些详细信息,请将cmdlet的输出传递 给 Get-Member cmdlet。
Get-Member cmdlet 向你显示对象类型的正式名称及其成员的完整列表。
例:Get-Command | Get-Member -MemberType Method
从上面命令中的标志可以看出,方法和属性之间进行的选择。
从cmdlet创建对象
操作对象的一种方法是从 cmdlet 的输出中提取属性并创建一个新对象。
这是使用Select-Object cmdlet 完成的。
这是列出目录并仅选择模式和名称的示例:
Get-ChildItem | Select-Object -Property Mode, Name
还可以使用以下标志来选择特定信息:
first - 获取第一个 x 对象
last - 获取最后一个 x 对象
unique - 显示独特的对象
skip - 跳过 x 个对象
过滤对象
检索输出对象时,您可能希望选择与特定值匹配的对象。Where-Object您可以使用基于属性值的过滤器来 执行此操作。
一般格式:
Verb-Noun | Where-Object -Property PropertyName -operator Value
Verb-Noun | Where-Object {$_.PropertyName -operator Value}
-operator为以下运算符:
-Contains:如果属性值中的任何项目与指定值完全匹配
-EQ:如果属性值与指定值相同
-GT:如果属性值大于指定值
例:检查已停止进程
Get-Service | Where-Object -Property Status -eq Stopped
排序对象
通过将cmdlet的输出通过管道连接 到Sort-Object cmdlet来执行此操作。
命令的格式是
Verb-Noun | Sort-Object
例:对目录列表进行排序
Get-ChildItem | Sort-Object
实例
文件“interesting-file.txt”的位置是什么
# 列出参数
(Get-Command Get-ChildItem).Parameters
使用Path参数
# 递归查找文件 -Name
Get-ChildItem -Path C:\ -Name *interesting-file.txt* -File -Recurse
或 -Include
# 递归查找文件 无提示错误消息-ErrorAction SilentlyContinue
Get-ChildItem -Path C:\ -Include *interesting-file.txt* -File -Recurse -ErrorAction SilentlyContinue
指定此文件的内容
命令+路径
Get-Content 文件路径
系统上安装了多少个 cmdlet(仅 cmdlet,不包括函数和别名)?
先获取参数名称
Get-Command | Select-Object -First 1
参数名称计数
Get-Command | Where-Object -Property CommandType -eq cmdlet | measure
或
Get-Command -CommandType cmdlet | measure
获取 interesting-file.txt 的 MD5 散列
找到指定hash函数:Get-Command *hash*
列表参数:
(Get-Command Get-FileHash).Parameters
执行命令
Get-FileHash -path "C:\Program Files\interesting-file.txt.txt" -Algorithm md5
获取当前工作目录的命令是什么?
查找
Get-Command *location*
得
Get-Location
路径“C:\Users\Administrator\Documents\Passwords”是否存在(是/否)?
Test-Path C:\Users\Administrator\Documents\Passwords
您将使用什么命令向 Web 服务器发出请求?
Get-Command *web*
得
Invoke-WebRequest
Windows 上的 Base64 解码文件 b64.txt
查找文件位置
Get-ChildItem -Path C:\ -Include *b64.txt* -File -Recurse -ErrorAction SilentlyContinue
解码文件与certutil.exe
certutil -decode "C:\Users\Administrator\Desktop\b64.txt" out.txt
然后读取
Get-Content out.txt
枚举
机器上有多少用户?
Get-LocalUser | measure
这个SID(S-1-5-21-1394777289-3961777894-1791813945-501)属于哪个本地用户?
列出参数列表
(Get-Command Get-LocalUser).Parameters
匹配
Get-LocalUser | Where-Object -Property SID -eq S-1-5-21-1394777289-3961777894-1791813945-501
或
Get-LocalUser -SID "S-1-5-21-1394777289-3961777894-1791813945-501"
有多少用户的密码要求值设置为 False?
列出属性
Get-LocalUser | Get-Menber
过滤结果
Get-LocalUser | Where-Object -Property PasswordRequired -Match false
存在多少本地组?
统计
Get-LocalGroup | measure
使用什么命令获取 IP 地址信息?
Get-NetIPAddress
有多少端口被列为侦听?
列出所有连接:Get-NetTCPConnection
查找参数:Get-NetTCPConnection | Get-Member
查找属性:GEt-NetTCPConnection | Format-List -Property State
统计结果:GEt-NetTCPConnection | Where-Object -Property State -Match Listen | measure
侦听端口 445 的本地端口的远程地址是什么?
GEt-NetTCPConnection | Where-Object -Property State -Match Listen
应用了多少补丁?
Get-Hotfix | measure
ID为KB4023834的补丁是什么时候安装的?
Get-HotFix | Where-Object -Property HotFixID -eq KB4023834
或
Get-Hotfix -Id KB4023834
查找备份文件的内容。
# 查找
Get-ChildItem -Path C:\ -Include *.bak* -File -Recurse -ErrorAction SilentlyContinue
# 输出
Get-Content 路径
搜索所有包含 API_KEY 的文件
Get-ChildItem C:\* -Recurse | Select-String -pattern API_KEY
您执行什么命令来列出所有正在运行的进程?
Get-Process
名为new-sched-task的定时任务的路径是什么?
列出任务:Get-ScheduleTask
查看任务:Get-ScheduleTask -TaskName new-sched-task
谁是 C:\ 的所有者
Get-Acl c:/
脚本编写
什么文件包含密码?
一个简单的字符串搜索:
Get-ChildItem -Path "C:\Users\Administrator\Desktop\emails\*" -Recurse | Select-String -Pattern password
改为脚本
$path = "C:\Users\Administrator\Desktop\emails\*"
$string_pattern = "password"
$command = Get-ChildItem -Path $path -Recurse | Select-String -Pattern $String_pattern
echo $command
哪些文件包含 HTTPS 链接?
$path = "C:\Users\Administrator\Desktop\emails\*"
$string_pattern = "https://"
$command = Get-ChildItem -Path $path -Recurse | Select-String -Pattern $String_pattern
echo $command
您在 130 到 140(包括这两个)之间找到了多少个开放端口?
for($i=130; $i -le 140; $i++){
Test-NetConnection localhost -Port $i
}
本文作者:-Lucky-
本文链接:https://www.cnblogs.com/-Lucky-/p/17231199.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步