Powershell写入注册表时出现问题

Powershell写入注册表时出现问题,powershell,registry,Powershell,Registry,我正在编写一个脚本,它将修改Windows计算机上的一些注册表设置 在编写键/属性之前,我使用If来确定它们是否存在。它应该失败,如果失败,则写入错误。此区块正在工作: $currentPath='HKLM:\Software\Microsoft\Windows\CurrentVersion\Policys\Ext' $pathTest=测试路径$currentPath 写入主机-ForegroundColor黄色“测试:”$currentPath 如果($pathTest-ne“True”)

我正在编写一个脚本,它将修改Windows计算机上的一些注册表设置

在编写键/属性之前,我使用If来确定它们是否存在。它应该失败,如果失败,则写入错误。此区块正在工作:

$currentPath='HKLM:\Software\Microsoft\Windows\CurrentVersion\Policys\Ext'
$pathTest=测试路径$currentPath
写入主机-ForegroundColor黄色“测试:”$currentPath
如果($pathTest-ne“True”)
{
新项目-路径$currentPath-强制|输出空值
写入主机-ForegroundColor绿色“创建路径:”$currentPath
}
否则{
写入主机-ForegroundColor红色$currentPath“已存在”
}
写入主机-ForegroundColor黄色“禁用IE加载项性能通知(DisableAddonLoadTimePerformanceNotifications):”
If((获取ItemProperty$currentPath-Name'DisableAddonLoadTimePerformanceNotifications'-ea 0)。'DisableAddonLoadTimePerformanceNotifications')
{
写入主机-ForegroundColor红色“DisableAddonLoadTimePerformanceNotifications已存在”
}
否则{
New ItemProperty-Path$currentPath-Name DisableAddonLoadTimePerformanceNotifications-PropertyType DWord-Value“1”| Out Null
写入主机-ForegroundColor绿色“DisableAddonLoadTimePerformanceNotifications已创建并设置为1”
}
如果键/属性存在,它将提供此输出:

测试:HKLM:\Software\Microsoft\Windows\CurrentVersion\Policys\Ext HKLM:\Software\Microsoft\Windows\CurrentVersion\Policys\Ext已存在。 禁用IE加载项性能通知(DisableAddonLoadTimePerformanceNotifications): DisabledUnloadTimePerformanceNotifications已存在 但是,此块不起作用:

$currentPath='HKLM:\Software\Policys\Microsoft\Internet Explorer\Main'
$pathTest=测试路径$currentPath
写入主机-ForegroundColor黄色“测试:”$currentPath
如果($pathTest-ne“True”)
{
新项目-路径$currentPath-强制|输出空值
写入主机-ForegroundColor绿色“创建路径:”$currentPath
}
否则{
写入主机-ForegroundColor红色$currentPath“已存在”
}
If((获取ItemProperty$currentPath-Name'EnableAutoUpgrade'-ea 0)。'EnableAutoUpgrade')
{
写入主机-ForegroundColor红色“EnableAutoUpgrade已存在”
}
否则{
New ItemProperty-Path$currentPath-Name'EnableAutoUpgrade'-PropertyType DWord-Value'0'| Out Null
写入主机-ForegroundColor绿色“EnableAutoUpgrade已创建并设置为0”
}
如果键/属性存在,则输出:

测试:HKLM:\Software\Policys\Microsoft\Internet Explorer\Main HKLM:\Software\Policys\Microsoft\Internet Explorer\Main已存在。 New ItemProperty:该属性已存在。 在C:\ps\setup\2.ps1:42字符:10 +新项目属性-路径$currentPath-名称“EnableAutoUpgrade”-属性。。。 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:ResourceExists:(HKEY_LOCAL_MACH…t Explorer\Main:String)[New ItemProperty],IOException +FullyQualifiedErrorId:System.IO.IOException,Microsoft.PowerShell.Commands.NewItemPropertyCommand 已创建EnableAutoUpgrade并将其设置为0 键/属性的输出不存在:

测试:HKLM:\Software\Policys\Microsoft\Internet Explorer\Main 创建路径:HKLM:\Software\Policys\Microsoft\Internet Explorer\Main 禁用IE自动升级(启用自动升级): 已创建EnableAutoUpgrade并将其设置为0 所以,它会创建属性没有问题,但是看不到它是否已经存在。在我看来,if在这个属性上失败了,但我不知道为什么。和上面的一样。在完整的脚本中,我总共运行了14个注册表项。上面的
启用自动升级
所有功能。下面的
enableauto-upgrade
没有,并且失败,并出现相同的错误

这在多台运行Win8.1和PowerShell v4的计算机上发生

这真是令人头痛。任何帮助都将不胜感激

试试这个:

$currentPath = "HKLM:\Software\Policies\Microsoft\Internet Explorer\Main"

Write-Host -ForegroundColor Yellow "Testing for: " $currentPath
If(Test-Path "HKLM:\Software\Policies\Microsoft\Internet Explorer\Main")
{
Write-Host $currentPath " already exists." -ForegroundColor 'Red' 
}
Else{
   New-Item -Path $currentPath | Out-Null
   Write-Host  "Created Path: "$currentPath -ForegroundColor 'Green'
}