有没有办法将 Azure AD 管理员凭据嵌入到脚本中以绕过 UAC 提示? (PS)

问题描述 投票:0回答:1

我创建了一个 PowerShell 脚本来清理加入 Entra 的端点设备,但在简化 DISM 等管理进程的 UAC 凭据条目时遇到问题。使用命令 (Start-Process PowerShell -verb runas -ArgumentList '-noexit','-File','Script.ps1') 以管理员身份运行脚本只会导致 UAC 提示输入 Entra 凭据。使用 -verb RunAs 参数时也是如此。如果 Entra 管理员登录端点设备,UAC 提示只需单击“是”,但如果普通用户登录,则要求提供完整凭据。我想知道是否有一种方法可以将管理员凭据嵌入到脚本中,以将最终用户的 UAC 提示简化为单击“是”,而不是输入凭据。如有任何帮助,我们将不胜感激!

azure powershell uac
1个回答
0
投票

我不认为你可以完全绕过UAC提示,因为它是一个内置的Windows安全功能,可以防止未经授权的进程获得提升的权限。但是,作为解决方法,您有两种选择

选项1- 具有最高权限的计划任务

选项2- 用户帐户控制:管理员的提升提示行为**,并将其设置为“提升而不提示”。

您可以参考我上面分享的链接并创建一个将运行提升任务的脚本

Write-Host "Starting DISM Cleanup..."
DISM /Online /Cleanup-Image /RestoreHealth
Write-Host "Cleaning up temporary files..."
Remove-Item -Path "C:\Windows\Temp\*" -Recurse -Force
Write-Host "Maintenance completed successfully!"

将 AD (Entra) 管理员凭据存储在 Windows 凭据管理器

cmdkey /add:AzureAD\AdminUsername /user:AdminUsername /pass:AdminPassword

安排任务以使用管理员权限运行脚本

$scriptPath = "C:\Scripts\Script.ps1"
$trigger = New-ScheduledTaskTrigger -AtStartup
$principal = New-ScheduledTaskPrincipal -UserId "AzureAD\AdminUsername" -LogonType Password -RunLevel Highest
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File $scriptPath"
Register-ScheduledTask -TaskName "Run-AdminScript" -Trigger $trigger -Principal $principal -Action $action

由于我不在 AD 上并且没有额外的凭证,因此我使用本地管理员来自动执行任务以简化此过程,但您可以使用您的额外凭证。将

"AzureAD\AdminUsername"
替换为您的实际 Azure 内部管理详细信息。

将脚本部署到您的虚拟机

az vm run-command invoke \
  --resource-group arkorg \
  --name arkovm \
  --command-id RunPowerShellScript \
  --scripts "New-Item -Path C:\Scripts -ItemType Directory; Set-Content -Path C:\Scripts\Script.ps1 -Value '<script_content_here>'"

enter image description here

设置任务后,您可以验证其状态

az vm run-command invoke \
  --resource-group arkorg \
  --name arkovm \
  --command-id RunPowerShellScript \
  --scripts "Get-ScheduledTaskInfo -TaskName 'Run-AdminScript'"

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.