2016-04-03 41 views
0

我試圖添加一個路徑到PowerShell的註冊表添加/刪除部分,但獲取身份驗證錯誤。如果我使用憑據,它告訴我不要。如果我不這樣做,我會被拒絕。嘗試在Windows 10中添加卸載註冊表路徑與Powershell的憑據

該帳戶是本地帳戶,未連接到AD域。

我有以下腳本:

$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 Vista中的便攜式通過Windows 10最感激:)

回答

0

發生異常是因爲您的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問題;討論自升式腳本的主題可以找到here

注:樣本代碼片段已經過測試。

的Windows 10

Powershell的5.0.10586.122

相關問題