我使用具有管理员角色的用户 . 但是,默认情况下,脚本以UAC模式运行,而不是以管理员身份运行 . 是否可以在没有UAC对话框的情况下使用powershell脚本打开powershell控制台?
我试着提升我想要做的任务,但是它给了我一个需要参加的对话框:
# 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,这样做与_2367099中完成的方式相同) . 但是,我没有权利对系统进行这样的更改,因为DevOps不会这样做 . 有没有其他方法通过PowerShell脚本来处理这个问题?
1 回答
您可以 - 作为一种解决方法 - 创建一个“计划”任务,该任务设置为运行提升并从初始PowerShell触发该任务 .
有关如何设置此类任务,请参阅here;有关如何触发该任务,请参阅here .