PowerShell脚本模版
PowerShell 是一个强大的脚本环境,用于系统管理和其他任务自动化。
# 脚本名称: <脚本名称>
# 描述: <脚本描述>
# 版本: <版本>
# 作者: Cikaros
# 邮箱: Cikaros<at>qq.com
# 日期: <日期>
# 版权: (C) 2024 Your Company. All rights reserved.
# 定义参数
param (
[string]$Param1, # 第一个参数
[string]$Param2, # 第二个参数
[switch]$Flag1, # 开关参数1
[switch]$Flag2, # 开关参数2
[int]$Number, # 数字参数
[string[]]$List, # 列表参数
[switch]$Help # 显示帮助信息
)
# 显示帮助信息
function Show-Help {
Write-Output "Usage: script.ps1 -Param1 <value> -Param2 <value> -Flag1 -Flag2 -Number <value> -List <value1,value2,...> -Help"
Write-Output ""
Write-Output "Parameters:"
Write-Output " -Param1 第一个参数"
Write-Output " -Param2 第二个参数"
Write-Output " -Flag1 开关参数1"
Write-Output " -Flag2 开关参数2"
Write-Output " -Number 数字参数"
Write-Output " -List 列表参数(逗号分隔)"
Write-Output " -Help 显示帮助信息"
}
# 检查是否需要显示帮助信息
if ($Help) {
Show-Help
exit
}
# 处理参数
if ($Param1) {
Write-Output "Param1: $Param1"
}
if ($Param2) {
Write-Output "Param2: $Param2"
}
if ($Flag1) {
Write-Output "Flag1 is set"
}
if ($Flag2) {
Write-Output "Flag2 is set"
}
if ($Number) {
Write-Output "Number: $Number"
}
if ($List) {
Write-Output "List: $($List -join ', ')"
}
常用的功能型脚本模板
1. 获取操作系统信息
$os = Get-CimInstance -ClassName Win32_OperatingSystem
Write-Output "操作系统名称: $($os.Caption)"
Write-Output "版本: $($os.Version)"
Write-Output "安装日期: $($os.InstallDate)"
2. 检查服务状态
$serviceName = 'Spooler' # 打印队列服务
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if ($service) {
Write-Output "服务 '$($service.Name)' 当前状态: $($service.Status)"
} else {
Write-Output "未找到服务 '$serviceName'"
}
3. 创建目录并设置权限
$folderPath = 'C:\Temp\NewFolder'
if (-not (Test-Path -Path $folderPath)) {
New-Item -ItemType Directory -Path $folderPath
$acl = Get-Acl -Path $folderPath
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Users", "ReadAndExecute", "ContainerInherit,ObjectInherit", "None", "Allow")
$acl.SetAccessRule($rule)
Set-Acl -Path $folderPath -AclObject $acl
Write-Output "目录已创建并设置了权限"
} else {
Write-Output "目录已存在"
}
4. 判断变量是否为空
$variable = 'Hello World'
if ([string]::IsNullOrEmpty($variable)) {
Write-Output "变量是空的"
} else {
Write-Output "变量不是空的,值为: $variable"
}
5. 判断文件是否存在
$filePath = 'C:\Path\To\File.txt'
if (Test-Path -Path $filePath) {
Write-Output "文件存在"
} else {
Write-Output "文件不存在"
}
6. 判断网络连接
$pingResult = Test-Connection -ComputerName 'www.example.com' -Count 1 -Quiet
if ($pingResult) {
Write-Output "网络连接成功"
} else {
Write-Output "无法连接到网络"
}
7. 使用 Switch 进行多条件判断
$number = 2
switch ($number) {
1 { Write-Output "数字是1" }
2 { Write-Output "数字是2" }
3 { Write-Output "数字是3" }
default { Write-Output "数字不在1, 2, 3中" }
}
8. 判断文件(夹)是否存在
$filePath = 'C:\Path\To\File.txt'
$folderPath = 'C:\Path\To\Folder'
if (Test-Path -Path $filePath) {
Write-Output "文件存在"
} else {
Write-Output "文件不存在"
}
if (Test-Path -Path $folderPath) {
Write-Output "文件夹存在"
} else {
Write-Output "文件夹不存在"
}
9. 判断是否为普通文件
$filePath = 'C:\Path\To\File.txt'
if (Test-Path -Path $filePath -PathType Leaf) {
Write-Output "这是一个普通文件"
} elseif (Test-Path -Path $filePath -PathType Container) {
Write-Output "这是一个文件夹"
} else {
Write-Output "路径不存在"
}
10. 判断变量是否为空
$variable = $null
if ([string]::IsNullOrEmpty($variable)) {
Write-Output "变量是空的"
} else {
Write-Output "变量不是空的,值为: $variable"
}
11. 用户输入数据
$ userInput = Read-Host "请输入一些文本"
Write-Output "您输入的是: $userInput"
12. 判断指令是否存在
$command = 'Get-Process'
if (Get-Command -Name $command -ErrorAction SilentlyContinue) {
Write-Output "命令 '$command' 存在"
} else {
Write-Output "命令 '$command' 不存在"
}
13. 判断关键字是否存在
$text = "这是一个测试字符串"
$keyword = "测试"
if ($text -match $keyword) {
Write-Output "关键字 '$keyword' 存在"
} else {
Write-Output "关键字 '$keyword' 不存在"
}
14. 文件夹递归处理
$folderPath = 'C:\Path\To\Folder'
if (Test-Path -Path $folderPath) {
Get-ChildItem -Path $folderPath -Recurse | ForEach-Object {
if (Test-Path -Path $_.FullName -PathType Leaf) {
Write-Output "文件: $($_.FullName)"
} else {
Write-Output "文件夹: $($_.FullName)"
}
}
} else {
Write-Output "文件夹不存在"
}
15. 下载文件
$url = 'https://example.com/path/to/file.zip'
$destination = 'C:\Path\To\DownloadedFile.zip'
Invoke-WebRequest -Uri $url -OutFile $destination
Write-Output "文件已下载到: $destination"
PowerShell脚本模版
https://blog.cikaros.top/doc/7da56ef8.html