2016-09-02 149 views
0

我試圖運行一個腳本,將在重新啓動時運行另一個Powershell腳本。我正在使用HKLM \ Software \ Microsoft \ Windows \ CurrentVersion \ Run鍵。管理員腳本將不會重新啓動管理員重新啓動

我的問題是腳本在重新啓動後啓動,但是從非管理員PS窗口運行。這給了我「拒絕訪問」。我在系統上禁用了UAC,但得到相同的錯誤。 如何在重新啓動時從Admin Powershell窗口啓動它?

目前有:

REG ADD HKLM \軟件\微軟\的Windows \ CurrentVersion \ Run中/ V RunThis /噸REG_SZ/F/d「C:\ WINDOWS \ SYSTEM32 \ WindowsPowerShell.exe - ExecutionPolicy不受限制 - 文件C:\ script.ps1 -Verb的RunAs

+0

固定句型結構;收緊的措辭。 – Prune

回答

0

假設你沒有當你手動運行它得到Access denied,這應該可以解決這個問題,打開PowerShell的管理重點提示這 -

set-executionpolicy remotesigned 

現在,這增加了運行的關鍵 -

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file C:\script.ps1 
0

這真的取決於幾個因素:UAC設置;是機器域加入; etc ...

此腳本將在大多數情況下工作。注意:admin腳本完成執行後,admin powershell主機將關閉。

$currentUserID = [System.Security.Principal.WindowsIdentity]::GetCurrent() 
$currentUserPrincipal = New-Object System.Security.Principal.WindowsPrincipal($currentUserID) 

$adminPrincipal = [System.Security.Principal.WindowsBuiltInRole]::Administrator 

# If not already admin start a new process and pass the current session definition to the admin powershell instance. 
if(-not ($currentUserPrincipal.IsInRole($adminPrincipal))) { 
    $adminProc = New-Object System.Diagnostics.ProcessStartInfo "powershell" 
    $adminProc.Arguments = $MyInvocation.MyCommand.Definition 
    $adminProc.Verb = "runas" 

    [System.Diagnostics.Process]::Start($adminProc) 

    #If you don't need to keep the current session open uncomment this line. 
    #exit 
} 

# Any code executed at this point will execute in an admin PS session. 
ping google.com 
相關問題