免责声明
本文仅用于技术讨论与学习,利用此文所提供的信息而造成的任何直接或者间接的后果及损失,均由使用者本人负责,文章作者及本公众号不为此承担任何责任。
PowerShell脚本
$folderPath = "C:MyFolder"
Get-ChildItem -Path $folderPath | Remove-Item
C:PathToYourScript.ps1
变量
$myVariable = "This is my variable."
Write-Output $myVariable
$myArray = 1,2,3,4,5
$myHashTable = @{Name="PowerShell"; Version="7.0"}
对象
$car = New-Object -TypeName psobject
Add-Member -InputObject $car -MemberType NoteProperty -Name Make -Value 'Toyota'
Add-Member -InputObject $car -MemberType NoteProperty -Name Model -Value 'Corolla'
Add-Member -InputObject $car -MemberType NoteProperty -Name Year -Value 2020
条件判断
$number = 10
if ($number -gt 5) {
Write-Output "Number is greater than 5"
} elseif ($number -eq 5) {
Write-Output "Number is equal to 5"
} else {
Write-Output "Number is less than 5"
}
循环
for ($i = 0; $i -lt 10; $i++) {
Write-Output $i
}
$numbers = 1,2,3,4,5
foreach ($number in $numbers) {
Write-Output $number
}
$i = 0
while ($i -lt 10) {
Write-Output $i
$i++
}
$i = 0
do {
Write-Output $i
$i++
} while ($i -lt 10)
编写PowerShell脚本
# 获取CPU和内存信息
$cpu = Get-WmiObject -Class Win32_Processor
$physicalMemory = Get-WmiObject -Class Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | % {[Math]::Round(($_.sum / 1GB), 2)}
# 判断是否可能是虚拟机
if ($cpu.NumberOfLogicalProcessors -eq 1 -or $physicalMemory -lt 2) {
Write-Output "This might be a virtual machine."
}
# 搜集已安装的补丁
$hotfixes = Get-WmiObject -Class WIN32_QuickFixEngineering | Select-Object -Property Description,HotFixID,InstalledOn
# 格式化并写入文件
$output = @"
System Information
==================
Number of Logical Processors: $($cpu.NumberOfLogicalProcessors)
Physical Memory (GB): $physicalMemory
Installed Hotfixes
==================
$($hotfixes | Format-Table | Out-String)
"@
$output | Out-File -FilePath .system_info.txt
总结
原文始发于微信公众号(赛博安全狗):【权限维持技术】编写PowerShell脚本
- 左青龙
- 微信扫一扫
- 右白虎
- 微信扫一扫
评论