使用 PowerShell 设置 UAC 级别

Jen*_*nte 4 windows-registry windows-7 uac powershell

我正在寻找一种使用 Powershell/命令提示符设置 UAC 级别的方法。我知道注册表中的“EnableLUA”值,但这不会设置级别。这只是真的或假的。

有没有办法用Powershell设置UAC级别?级别,我的意思是UAC的四个级别。它们从“从不通知”变为“始终通知”。

谢谢你。

Pat*_*our 5

有几个注册表值控制用户帐户控制:

  1. 过滤器管理员令牌
  2. 同意提示行为管理员
  3. 同意提示行为用户
  4. 启用安装程序检测
  5. 验证管理代码签名
  6. 启用LUA
  7. PromptOnSecureDesktop
  8. 启用虚拟化

这些值的组合控制 GUI 中的滑块,反之亦然。

参考:http : //msdn.microsoft.com/en-us/library/cc232771.aspx


小智 5

实际上已经有一个外卖的 powershell 脚本可供您使用。

您可以在如何切换UAC级别轻松找到它们

希望它能有所帮助。

编辑

上述 Microsoft Technet 站点中的代码实现了这些 cmdlet:

  • Set-UACLevel()
  • 获取UACLevel()

但此操作系统尚未确认它们(2017 年 1 月 12 日):

  • Windows Server 2012 R2
  • Windows Server 2008
  • Windows 7的

代码片段:

New-Variable -Name Key 
New-Variable -Name PromptOnSecureDesktop_Name 
New-Variable -Name ConsentPromptBehaviorAdmin_Name 
 
Function Set-RegistryValue($key, $name, $value, $type="Dword") {  
  If ((Test-Path -Path $key) -Eq $false) { New-Item -ItemType Directory -Path $key | Out-Null }  
       Set-ItemProperty -Path $key -Name $name -Value $value -Type $type  
}  
 
Function Get-RegistryValue($key, $value) {  
   (Get-ItemProperty $key $value).$value  
}  
 
$Key = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" 
$ConsentPromptBehaviorAdmin_Name = "ConsentPromptBehaviorAdmin" 
$PromptOnSecureDesktop_Name = "PromptOnSecureDesktop" 
 
Function Get-UACLevel(){ 
    $ConsentPromptBehaviorAdmin_Value = Get-RegistryValue $Key $ConsentPromptBehaviorAdmin_Name 
    $PromptOnSecureDesktop_Value = Get-RegistryValue $Key $PromptOnSecureDesktop_Name 
    If($ConsentPromptBehaviorAdmin_Value -Eq 0 -And $PromptOnSecureDesktop_Value -Eq 0){ 
        "Never notIfy" 
    } 
    ElseIf($ConsentPromptBehaviorAdmin_Value -Eq 5 -And $PromptOnSecureDesktop_Value -Eq 0){ 
        "NotIfy me only when apps try to make changes to my computer(do not dim my desktop)" 
    } 
    ElseIf($ConsentPromptBehaviorAdmin_Value -Eq 5 -And $PromptOnSecureDesktop_Value -Eq 1){ 
        "NotIfy me only when apps try to make changes to my computer(default)" 
    } 
    ElseIf($ConsentPromptBehaviorAdmin_Value -Eq 2 -And $PromptOnSecureDesktop_Value -Eq 1){ 
        "Always notIfy" 
    } 
    Else{ 
        "Unknown" 
    } 
} 
     
Function Set-UACLevel() { 
    Param([int]$Level= 2) 
 
    New-Variable -Name PromptOnSecureDesktop_Value 
    New-Variable -Name ConsentPromptBehaviorAdmin_Value 
 
    If($Level -In 0, 1, 2, 3) { 
        $ConsentPromptBehaviorAdmin_Value = 5 
        $PromptOnSecureDesktop_Value = 1 
        Switch ($Level)  
        {  
          0 { 
              $ConsentPromptBehaviorAdmin_Value = 0  
              $PromptOnSecureDesktop_Value = 0 
          }  
          1 { 
              $ConsentPromptBehaviorAdmin_Value = 5  
              $PromptOnSecureDesktop_Value = 0 
          }  
          2 { 
              $ConsentPromptBehaviorAdmin_Value = 5  
              $PromptOnSecureDesktop_Value = 1 
          }  
          3 { 
              $ConsentPromptBehaviorAdmin_Value = 2  
              $PromptOnSecureDesktop_Value = 1 
          }  
        } 
        Set-RegistryValue -Key $Key -Name $ConsentPromptBehaviorAdmin_Name -Value $ConsentPromptBehaviorAdmin_Value 
        Set-RegistryValue -Key $Key -Name $PromptOnSecureDesktop_Name -Value $PromptOnSecureDesktop_Value 
 
        Get-UACLevel 
    } 
    Else{ 
        "No supported level" 
    } 
     
} 
 
Export-ModuleMember -Function Get-UACLevel 
Export-ModuleMember -Function Set-UACLevel
Run Code Online (Sandbox Code Playgroud)