尝试使用Powershell在Windows 10中添加卸载注册表路径时的凭据
我正在尝试使用PowerShell将路径添加到注册表的“添加/删除”部分,但身份验证出错。如果我使用凭证,它会告诉我不要这样做。如果我不这样做,我就会被拒绝 该帐户是本地帐户,未连接到AD域 我有以下脚本:尝试使用Powershell在Windows 10中添加卸载注册表路径时的凭据,windows,powershell,registry,credentials,Windows,Powershell,Registry,Credentials,我正在尝试使用PowerShell将路径添加到注册表的“添加/删除”部分,但身份验证出错。如果我使用凭证,它会告诉我不要这样做。如果我不这样做,我就会被拒绝 该帐户是本地帐户,未连接到AD域 我有以下脚本: $cred = Get-Credential write-host "DEBUG: Without credentials" New-Item HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Test_App -Force
$cred = Get-Credential
write-host "DEBUG: Without credentials"
New-Item HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Test_App -Force
write-host "DEBUG: With -Credential"
New-Item HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Test_App -Force -Credential $cred
它在Windows 10上产生以下错误:
Supply values for the following parameters:
Credential
DEBUG: Without credentials
New-Item : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Test_App' is denied.
At Z:\test.ps1:4 char:1
+ New-Item HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Te ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (HKEY_LOCAL_MACH...nstall\Test_App:String) [New-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.NewItemcommand
DEBUG: With -Credential
The provider does not support the use of credentials. Perform the operation again without specifying credentials.
At Z:\test.ps1:7 char:1
+ New-Item HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Te ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotImplemented: (:) [], PSNotSupportedException
+ FullyQualifiedErrorId : NotSupported
建议可通过Windows 10跨Windows Vista移植:)发生异常是因为您的powershell实例没有以提升的权限运行 因此:以管理员身份运行powershell或者,如果您在编写脚本时正在使用诸如powershell ISE或PowerGui之类的调试器以管理员身份运行该程序 如果只想运行现有脚本并将其提升到管理级别,则可以按如下方式修改脚本:
#this line added to the beginning of the script.
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
#your original command
write-host "DEBUG: Without credentials"
New-Item HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Test_App -Force
一个相关的SO问题;讨论自我提升脚本的主题可以找到
注意:在上测试的示例代码段
Windows 10
Powershell 5.0.10586.122