Powershell写入注册表

时间:2015-06-18 01:22:12

标签: powershell registry

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

我正在使用If在写入之前确定键/属性是否存在。它应该失败并写一个错误,如果它。这个块正在运行:

 
$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'
         }

如果键/属性存在,它会给我输出:

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

但是,这个块不起作用:

$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'
    }

如果键/属性存在,则输出:

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

密钥/属性的输出不存在:

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

所以,它会创建属性没有问题,但是看不出它是否已经存在。它看起来像是If在这个上失败了,但我无法弄清楚为什么。它和它上面的一样。在完整脚本中,我运行了总共14个注册表项。以上EnableAutoUpgrade的所有工作都可以。低于EnableAutoUpgrade的那些没有并且失败并出现相同的错误。

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

这是一个真正令人头疼的问题。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

试试这个:

$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'
}