Powershell以管理员模式打开PowerShell控制台而不使用UAC对话框并执行一些任务

问题描述:

我使用具有管理员角色的用户。但是,默认情况下,脚本以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

您可以 - 作为解决方法 - 创建一个'预定'任务,该任务设置为从初始PowerShell运行提升并触发该任务。

有关如何设置此任务的信息,请参见here,关于如何触发它,请参阅here

+1

感谢@DAXholic为您的答案。不过,我已经理清了这一点,尽管我没有机会添加它,是的,自动化团队也提出了和你一样的建议。 – Rishi

+0

Btws,@DAXholic,我需要这样做:http://*.com/questions/39108473/unable-to-disable-the-microsoft-office-customization-installer-while-automating。因为,我对任务调度器很陌生,你能否让我领先一步,以达到我所要求的目标?我觉得应该有一个任务调度程序本身的方式... – Rishi