console
# 检查是否以管理员身份运行
function Test-Administrator {
$currentUser = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
# 如果不是管理员,重新启动脚本
if (-not (Test-Administrator)) {
Write-Host "请求管理员权限..." -ForegroundColor Yellow
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
exit
}
# 以下是需要管理员权限的代码
Write-Host "当前以管理员身份运行!" -ForegroundColor Green
# 你的代码...