Powershell 在没有 UAC 对话框的情况下以管理员模式打开 powershell 控制台并执行一些任务
Powershell to open a powershell console in administrator mode without the UAC dialog box and perform some task
我使用具有管理员角色的用户。但是,默认情况下,脚本 运行 处于 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,方法与 Windows 10 中的相同)。但是,我没有权利对系统进行这样的更改,因为 DevOps 禁止我这样做。有没有其他方法可以通过 powershell 脚本来处理这个问题?
我使用具有管理员角色的用户。但是,默认情况下,脚本 运行 处于 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,方法与 Windows 10 中的相同)。但是,我没有权利对系统进行这样的更改,因为 DevOps 禁止我这样做。有没有其他方法可以通过 powershell 脚本来处理这个问题?