[英]How to elevate a Powershell script from within a script
我是 Powershell 的新手,但在很多方面都非常喜歡它。 由於某些原因,我創建了一個腳本,該腳本從硬盤驅動器上的文件加載一組本地管理員憑據,並創建一個 PSCredential 對象 $MyCred。
我想使用 $MyCred 來提升一個單獨的腳本(對注冊表進行一些更改以打開 RDP 連接)。 我嘗試將 $MyCred 傳遞給 Start-Process cmdlet:
Start-Process Powershell.exe -Credential $MyCredential
然后我收到以下錯誤:
Start-Process :此命令無法運行,因為錯誤:目錄名稱無效。
如果我使用 -credential 和一個空變量運行 Start-Process,系統會提示我輸入用戶名和密碼。 當我輸入它們時,我會得到一個提升的 powershell 提示,沒有任何問題,並且能夠在注冊表中對我的測試系統進行更改。
我已經驗證了 $myCred 的內容,並且它正確存儲了 U/N 和 P/W(與我手動輸入的內容相同)。 使用 New-PSSEssion 之類的
New-PSSession -Credential $MyCredential
返回訪問被拒絕,我讀過的很多系統默認情況下也禁用了訪問。
在理想的世界中,代碼如下所示:
Start-Process Powershell.exe -Credential $MyCredential -File C:\\MyScript.ps1
這應該啟動一個提升的 powershell,它在第二個腳本中運行一些命令,然后終止。 我在這里想念什么?
感謝我能得到的所有幫助,謝謝! 我可能是完全顯而易見的。
背景是,我們有一些我們自己無法使用的計算機,也無法通過 RDP 訪問。 我們在站點上確實有可以為我們運行腳本的用戶。 但重要的是他們沒有獲得本地管理員密碼。 因此,我們想創建一個腳本來讀取加密的密碼文件,生成本地管理員 PSCredential 對象,並將其傳遞給一個腳本,該腳本進行必要的注冊表更改以允許 RDP 訪問。
有很多關於這個主題的用例文章。 使用“PowerShell 自提升腳本”進行快速網絡搜索將顯示它們。 甚至直接來自 MS
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
{
# We are running "as Administrator" - so change the title and background color to indicate this
$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
$Host.UI.RawUI.BackgroundColor = "DarkBlue"
clear-host
}
else
{
# We are not running "as Administrator" - so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
# Specify the current script path and name as a parameter
$newProcess.Arguments = $myInvocation.MyCommand.Definition;
# Indicate that the process should be elevated
$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::Start($newProcess);
# Exit from the current, unelevated, process
exit
}
# Run your code that needs to be elevated here
Write-Host -NoNewLine "Press any key to continue..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
此代碼來自有用的指南: 如何自我提升 PowerShell 腳本
它會檢查當前腳本的安全性,如果需要提升,腳本將以管理員身份重新啟動。 如果啟用了 UAC,它會提示您確認。 重新啟動后,它將擁有必要的訪問權限並在檢查后運行代碼。
# Self-elevate the script if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
Exit
}
}
# Remainder of script here
更新:我發現這不會在提升后維護當前的工作目錄。
您在這里缺少 NoExit 參數。
Start-Process -Filepath "Powershell.exe" -ArgumentList "-NoExit -File "C:\myscript.ps1" -Credential $MyCredential
這只是對@mbomb007 答案的增強,由網絡上的其他示例和我自己努力傳遞所有<\/em>原始參數(包括命名參數和位置參數)拼湊而成。 請注意,添加的函數不會對參數值中可能使用的特殊字符進行編碼。 大多數帖子奇怪地沒有傳遞論點。
function Pass-Parameters {
Param ([hashtable]$NamedParameters)
return ($NamedParameters.GetEnumerator()|%{"-$($_.Key) `"$($_.Value)`""}) -join " "
}
# Self-elevate the script if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + (Pass-Parameters $MyInvocation.BoundParameters) + " " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
Exit
}
}
# Remainder of script here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.