Powershell写入注册表的麻烦 [英] Trouble with Powershell writing to registry
问题描述
我正在编写一个脚本,它将修改Windows机器上的一些注册表设置。
我正在使用if来确定密钥是否/在写它们之前存在属性。它应该失败并写一个错误,如果它。这个块正在工作:
$ currentPath ='HKLM:\Software\Microsoft \ Windows \ CurrentVersion \POLicies\Ext'
$ pathTest = Test-Path $ currentPath
Write-Host -ForegroundColor Yellow'测试:'$ currentPath
If($ pathTest -ne'True')
{
New-Item -Path $ currentPath -Force | Out-Null
写主机-ForegroundColor Green'创建路径:'$ currentPath
}
否则{
Write-Host -ForegroundColor Red $ currentPath'已经存在。'
}
Write-Host -ForegroundColor黄色'禁用IE加载项性能通知(DisableAddonLoadTimePerformanceNotifications):'
If((Get-ItemProperty $ currentPath -Name'DisableAddonLoadTimePerformanceNotifications'-ea 0 '。DisableAddonLoadTimePerformanceNotifications')
{
Write-Host -ForegroundColor Red'AdapableAddonLoadTimePerformanceNotifications已经存在'
}
Else {
New-ItemProperty -Path $ currentPath -Name DisableAddonLoadTimePerformanceNotifications -PropertyType DWord -Value'1'| Out-Null
写入主机-ForegroundColor绿色'DisableAddonLoadTimePerformanceNotifications创建并设置为1'
}
如果键/属性它给我这个输出存在:
测试:HKLM:\软件\微软\ Windows \ CurrentVersion \POLicies\Ext
HKLM:\ Software \ Microsoft \ Windows \ CurrentVersion \POLicies \ Exxt已经存在。
禁用IE加载项性能通知(DisableAddonLoadTimePerformanceNotifications):
DisableAddonLoadTimePerformanceNotifications已存在
但是,此块不起作用:
$ currentPath ='HKLM:\Software \POLicies\Microsoft \Internet Explorer \ Main'
$ pathTest = Test-Path $ currentPath
Write-Host -ForegroundColor Yellow'测试:'$ currentPath
If($ pathTest -ne'True')
{
New-Item -Path $ currentPath -Force | Out-Null
写主机-ForegroundColor Green'创建路径:'$ currentPath
}
否则{
Write-Host -ForegroundColor Red $ currentPath'已经存在。'
}
If((Get-ItemProperty $ currentPath -Name'EnableAutoUpgrade'-ea 0)。'EnableAutoUpgrade')
{
Write-Host -ForegroundColor Red'EnableAutoUpgrade已经存在'
}
Else {
New-ItemProperty -Path $ currentPath -Name'EnableAutoUpgrade'-PropertyType DWord -Value'0'| Out-Null
写入主机-ForegroundColor绿色'EnableAutoUpgrade已创建并设置为0'
}
如果键/属性存在,则输出:
测试:HKLM:\软件\政策\微软\互联网资源管理器\主要
HKLM:\软件\政策\微软\ Internet Explorer \已经存在。
New-ItemProperty:该属性已存在。
在C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:ResourceExists:(HKEY_LOCAL_MACH ... t Explorer \ Main:字符串)[New-ItemProperty],IOException
+ FullyQualifiedErrorId:System.IO.IOException,Microsoft.PowerShell.Commands.NewItemPropertyCommand
创建EnableAutoUpgrade并设置为0
密钥/属性的输出不存在:
测试:HKLM:\ Software \POLicies\Microsoft \\ \\ Internet Explorer \ Main
创建路径:HKLM:\软件\POLicies\Microsoft \Internet Explorer \ Main
禁用IE自动升级(EnableAutoUpgrade):
EnableAutoUpgrade created and设置为0
所以,它会创建属性没问题,但看不出它是否已经存在。我我看起来像是If在这个失败,但我无法弄清楚为什么。它和它上面的一样。在完整脚本中,我运行了总共14个注册表项。高于
`EnableAutoUpgrade`的所有工作都可以。 "EnableAutoUpgrade"下面的那些没有,并且失败并出现同样的错误。
$
这种情况发生在运行Win8.1和PowerShell v4的多台机器上。 />
这真是令人头疼的问题。任何帮助将不胜感激。
这一行很奇怪:
If((Get-ItemProperty
currentPath -Name'EnableAutoUpgrade'-ea 0)。'EnableAutoUpgrade')
尝试将其替换为:
如果 (((Get-ItemProperty -Path
currentpath -Name EnableAutoUpgrade).EnableAutoUpgrade)-eq 0)
它更好吗?
I'm writing a script that will modify some registry settings on Windows machines.
I'm using an If to determine if the key/property exist before writing them. It should fail and write an error if it does. This chunk is working:
$currentPath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Ext' $pathTest = Test-Path $currentPath Write-Host -ForegroundColor Yellow 'Testing for: ' $currentPath If($pathTest -ne 'True') { New-Item -Path $currentPath -Force | Out-Null Write-Host -ForegroundColor Green 'Created Path: '$currentPath } Else{ Write-Host -ForegroundColor Red $currentPath ' already exists.' } Write-Host -ForegroundColor Yellow 'Disabling IE Add-On Performance Notifications (DisableAddonLoadTimePerformanceNotifications):' If((Get-ItemProperty $currentPath -Name 'DisableAddonLoadTimePerformanceNotifications' -ea 0).'DisableAddonLoadTimePerformanceNotifications') { Write-Host -ForegroundColor Red 'DisableAddonLoadTimePerformanceNotifications already exists' } Else { New-ItemProperty -Path $currentPath -Name DisableAddonLoadTimePerformanceNotifications -PropertyType DWord -Value '1' | Out-Null Write-Host -ForegroundColor Green 'DisableAddonLoadTimePerformanceNotifications created and set to 1' }
It gives me this output if the key/property exists:
Testing for: HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Ext HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Ext already exists. Disabling IE Add-On Performance Notifications (DisableAddonLoadTimePerformanceNotifications): DisableAddonLoadTimePerformanceNotifications already exists
However, this chunk does not work:
$currentPath = 'HKLM:\Software\Policies\Microsoft\Internet Explorer\Main' $pathTest = Test-Path $currentPath Write-Host -ForegroundColor Yellow 'Testing for: ' $currentPath If($pathTest -ne 'True') { New-Item -Path $currentPath -Force | Out-Null Write-Host -ForegroundColor Green 'Created Path: '$currentPath } Else{ Write-Host -ForegroundColor Red $currentPath ' already exists.' } If((Get-ItemProperty $currentPath -Name 'EnableAutoUpgrade' -ea 0).'EnableAutoUpgrade') { Write-Host -ForegroundColor Red 'EnableAutoUpgrade already exists' } Else{ New-ItemProperty -Path $currentPath -Name 'EnableAutoUpgrade' -PropertyType DWord -Value '0' | Out-Null Write-Host -ForegroundColor Green 'EnableAutoUpgrade created and set to 0' }
Output if the key/property exists:
Testing for: HKLM:\Software\Policies\Microsoft\Internet Explorer\Main HKLM:\Software\Policies\Microsoft\Internet Explorer\Main already exists. New-ItemProperty : The property already exists. At C:\ps\setup\2.ps1:42 char:10 + New-ItemProperty -Path $currentPath -Name 'EnableAutoUpgrade' -Property ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceExists: (HKEY_LOCAL_MACH...t Explorer\Main:String) [New-ItemProperty], IOException + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.NewItemPropertyCommand EnableAutoUpgrade created and set to 0
Output of the key/property does not exist:
Testing for: HKLM:\Software\Policies\Microsoft\Internet Explorer\Main Created Path: HKLM:\Software\Policies\Microsoft\Internet Explorer\Main Disabling IE Auto Upgrade (EnableAutoUpgrade): EnableAutoUpgrade created and set to 0
So, it'll create the property no problem, but can't see if it already exists.It looks to me like the If is failing on this one, but I can't figure out why. It's the same as one above it. In the full script I run a total of 14 registry entries. The ones above
`EnableAutoUpgrade` all work. The ones below `EnableAutoUpgrade` do not and fail with the same error.
This is happening on multiple machines all running Win8.1 and PowerShell v4.
This is a real head scratcher. Any help would be appreciated.
Hi,
This line is weird :
If((Get-ItemProperty
currentPath -Name 'EnableAutoUpgrade' -ea 0).'EnableAutoUpgrade')Try to replace it by :
If (((Get-ItemProperty -Path
currentpath -Name EnableAutoUpgrade).EnableAutoUpgrade)-eq 0)Is it better ?
这篇关于Powershell写入注册表的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!