0

我使用具有管理員角色的用戶。但是,默認情況下,腳本以UAC模式運行,而不是以管理員身份運行。是否有可能打開PowerShell控制檯與PowerShell腳本沒有UAC對話框?Powershell以管理員模式打開PowerShell控制檯而不使用UAC對話框並執行一些任務

我想提升我想要做如下的任務,但它給了我一個需要出席一個對話框:

# 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 an administrator 
if ($myWindowsPrincipal.IsInRole($adminRole)) 
{ 
    # We are running as an administrator, so change the title and background colour to indicate this 
    $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"; 
    $Host.UI.RawUI.BackgroundColor = "DarkBlue"; 
    Clear-Host; 
} 
else { 
    # We are not running as an administrator, so relaunch as administrator 

    # Create a new process object that starts PowerShell 
    $newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell"; 


    # 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; 
} 

#DOING SOME TASK HERE 

然而,這打開了,如果我想它確認UAC對話框以管理員模式打開PowerShell。

我嘗試過的另一種方法是在管理員模式下默認打開PowerShell控制檯(我使用WS 2012,方法與Windows 10相同)。但是,由於DevOps不允許我這樣做,所以我沒有權利對系統進行更改。有沒有其他方式通過PowerShell腳本來處理這個問題?

+0

往哪投票?我請求選民提供相同的理由。 – Rishi

+0

昨天我在短時間內收到了一些降薪 - 不知道那傢伙有什麼問題或者有人被黑客攻擊 – DAXaholic

回答

0

您可以 - 作爲解決方法 - 創建一個'預定'任務,該任務設置爲從初始PowerShell運行提升並觸發該任務。

有關如何設置此任務的信息,請參見here,關於如何觸發它,請參閱here

+1

感謝@DAXholic爲您的答案。不過,我已經理清了這一點,儘管我沒有機會添加它,是的,自動化團隊也提出了和你一樣的建議。 – Rishi

+0

Btws,@DAXholic,我需要這樣做:http://stackoverflow.com/questions/39108473/unable-to-disable-the-microsoft-office-customization-installer-while-automating。因爲,我對任務調度器很陌生,你能否讓我領先一步,以達到我所要求的目標?我覺得應該有一個任務調度程序本身的方式... – Rishi

相關問題