Powershell:无法更新注册表路径,因为它不存在(但它确实存在)

时间:2017-01-26 22:58:29

标签: powershell internet-explorer azure-pipelines azure-pipelines-release-pipeline

我们正在Windows 8.1中对IE运行编码UI测试,我们通过Visual Studio Team Services进行测试。作为构建的一部分,我们运行一个Powershell脚本来禁用弹出管理器。我们用来禁用它的代码是:

Remove-ItemProperty "HKCU:\Software\Microsoft\Internet Explorer\New Windows" -Name "PopupMgr"
New-ItemProperty "HKCU:\Software\Microsoft\Internet Explorer\New Windows" -Name "PopupMgr" -Value 00000000 -PropertyType "DWord"

当我在Release Manager中创建和部署构建时,运行它会生成以下错误:

  

运行命令已停止,因为首选项变量" ErrorActionPreference"或者常用参数设置为停止:找不到路径' HKCU:\ Software \ Microsoft \ Internet Explorer \ New Windows'因为它不存在。

(强调我的)

我已登录虚拟机并查看了注册表,HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\New Windows绝对存在。我唯一能想到的是它的值不是DWORD而是字符串 - PopupMgr键的数据值为"是"而不是1或0.但这并不匹配错误消息 - 错误说它甚至找不到密钥的路径,而不是值类型不匹配。此外,代码会在插入新密钥之前删除现有密钥,因此我甚至不知道它会如何发现不匹配。

即使更奇怪,如果我在VM中打开Powershell并运行那两行(我复制并粘贴以避免拼写错误),它们运行得很好。

这个脚本完全适用于Windows 10,并且有一段时间了,所以我不确定这里发生了什么。该用户是管理员组的成员,因此我认为这不是权限问题。

任何人都可以对此有所了解吗?

3 个答案:

答案 0 :(得分:2)

我认为您正在尝试添加注册表项属性值 您需要测试是否存在注册表项。如果注册表项不存在,则需要创建注册表项,然后创建注册表项属性值。

您应该创建注册表项的路径,然后指定属性名称和我要分配的值。这包括三个变量,如下所示:

这可以帮助你:

$registryPath = "HKCU:\Software\Microsoft\Internet Explorer\New Windows"

$Name = "PopupMgr"

$value = "00000000"

IF(!(Test-Path $registryPath))

  {

    New-Item -Path $registryPath -Force | Out-Null

    New-ItemProperty -Path $registryPath -Name $name -Value $value `

    -PropertyType DWORD -Force | Out-Null}

 ELSE {

    New-ItemProperty -Path $registryPath -Name $name -Value $value `

    -PropertyType DWORD -Force | Out-Null}

希望它有所帮助。

答案 1 :(得分:0)

重命名值时,我遇到了同样的问题,所以我了解到可以(需要/应该)定义值,所以我不再尝试重命名这些值并开始定义它们了:)

只需尝试使用Set-ItemProperty来更改,设置和重置值...

Set-ItemProperty -Path "HKCU:\Software\Microsoft\Internet Explorer\New Windows" -Name PopupMgr -Value 00000000 -Type DWORD

答案 2 :(得分:0)

我将 Duttas 答案封装在一个易于使用的函数中:

Set_Registry_key -key "HKLM:\FULL\PATH\pad\pat\KEYNAME" `
                 -type String `
                 -value 'value'

函数定义:

function Set_Registry_key{
    # String REG_SZ; ExpandString: REG_EXPAND_SZ; Binary: REG_BINARY; DWord: REG_DWORD; MultiString: REG_MULTI_SZ; Qword: REG_QWORD; Unknown: REG_RESOURCE_LIST
    Param(
        [Parameter(Mandatory=$true)]
        [string]
        $key,
        [ValidateSet('String', 'DWord', 'ExpandString', 'Binary', 'MultiString', 'Qword', 'Unknown')]
        $type,
        [Parameter(Mandatory=$true)]
        $value
    )
    $registryPath = $key -replace "[^\\]*$", ""
    $name = $key -replace ".*\\", ""
    if(!(Test-Path $registryPath)){
        New-Item -Path $registryPath -Force | Out-Null
        New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType $type -Force | Out-Null
    }else {
        New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType $type -Force | Out-Null
    }
}