如何使用管理员凭据提权运行PowerShell网关脚本?
问题:如何使用管理员凭据正确运行网关管理脚本?
背景
我需要先部署两个PowerShell脚本(Remove-Gateway.ps1和Recover-Gateway.ps1),然后使用管理员凭据运行它们——当前执行部署的用户没有管理员权限,但尝试的运行代码无法正常工作。
部署脚本代码
用于生成两个目标脚本的代码:
# Create Remove-Gateway.ps1 and Recover-Gateway.ps1 in the Script folder $remove = @' $newGateway = "0.0.0.0" $destination = "0.0.0.0/0" $index = (Get-NetRoute -DestinationPrefix "0.0.0.0/0" -NextHop "192.168.*").ifIndex $gateway = (Get-NetRoute -InterfaceIndex $index -DestinationPrefix $destination).NextHop $gateway | Out-File -FilePath "$env:PUBLIC\gateway.txt" # Remove actual gateway and set new route Remove-NetRoute -InterfaceIndex $index -NextHop $gateway -Confirm:$false New-NetRoute -InterfaceIndex $index -NextHop $newGateway -DestinationPrefix $destination -Confirm:$false '@ $remove | Out-File "$folderPath\Remove-Gateway.ps1" $recover = @' # Get 0.0.0.0 IP Gateway $oldGateway = "0.0.0.0" $destination = "0.0.0.0/0" $index = (Get-NetRoute -DestinationPrefix $destination -NextHop $oldGateway).ifIndex $oldGateway = (Get-NetRoute -InterfaceIndex $index -DestinationPrefix $destination).NextHop # Get old Gateway $file = "$env:Public\gateway.txt" $newgateway = Get-Content $file # Recover old gateway Remove-NetRoute -InterfaceIndex $index -NextHop $oldgateway -DestinationPrefix $destination -Confirm:$false New-NetRoute -InterfaceIndex $index -NextHop $newgateway -DestinationPrefix $destination -Confirm:$false Remove-Item -Path "$env:PUBLIC\gateway.txt" '@ $recover | Out-File "$folderPath\Recover-Gateway.ps1"
注:已修正原代码的字符串拼接语法错误,使用Here-String避免引号转义问题。
尝试的运行代码(无法正常工作)
$folderPath = "C:\Scripts" $removegw = "$folderPath"\Remove-Gateway.ps1" $recovergw = "$folderPath"\Recover-Gateway.ps1" $user = "User" $password= "Complex Password" $credential = New-Object System.Management.Automation.PSCredential -ArgumentList @($user,(ConvertTo-SecureString -String $password -AsPlainText -Force)) Start-Process Powershell.exe -Credential $credential -ArgumentList "-noprofile -file $removegw -verb RunAs"
之前成功的测试命令
禁用网卡的提权命令可以正常运行:
powershell -Command "Start-Process powershell.exe -ArgumentList '-ExecutionPolicy Bypass -NoExit -Command Disable-Netadapter -name Wi-Fi -confirm:`$false' -Verb RunAs"
解决方案
错误分析
- 参数位置错误:
-Verb RunAs是Start-Process的参数,不是powershell.exe的启动参数,不能放在-ArgumentList中。 - 路径语法错误:
"$folderPath"\Remove-Gateway.ps1"存在语法问题,正确的路径拼接应该是"$folderPath\Remove-Gateway.ps1"或用Join-Path函数。 - 路径未转义:如果脚本路径包含空格,需要在参数中用引号包裹路径,避免解析错误。
修正后的运行代码
方法1:指定管理员凭据运行
$folderPath = "C:\Scripts" # 用Join-Path安全拼接路径 $removegw = Join-Path $folderPath "Remove-Gateway.ps1" $recovergw = Join-Path $folderPath "Recover-Gateway.ps1" $user = "User" $password= "Complex Password" $credential = New-Object System.Management.Automation.PSCredential -ArgumentList $user, (ConvertTo-SecureString -String $password -AsPlainText -Force) # 正确传递参数,用转义引号包裹脚本路径,添加ExecutionPolicy Bypass确保脚本能执行 Start-Process powershell.exe -Credential $credential -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$removegw`""
方法2:强制提权(触发UAC)+ 指定凭据
如果需要明确触发管理员权限提示,同时指定管理员账号,可结合-Verb RunAs和-Credential(需确保目标账号有管理员权限):
Start-Process powershell.exe -Credential $credential -Verb RunAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$removegw`""
额外注意事项
- 部署脚本中的Recover-Gateway.ps1原代码存在变量大小写不一致问题:
New-NetRoute使用了$newGateway,但实际获取的变量是$newgateway,已在上面的部署脚本中修正。 - 确保目标管理员账号有权限修改网络路由,且
C:\Scripts文件夹对当前用户有写入权限,对管理员账号有读取权限。
内容的提问来源于stack exchange,提问作者jjquintero245
相关产品推荐
相关产品推荐

