无法通过PowerShell创建注册表值

tpgth1q7  于 11个月前  发布在  Shell
关注(0)|答案(1)|浏览(262)

我有多台计算机需要启用证书填充。我的脚本如下所示:

$ComputerName = Read-Host "Please Enter Computer Name"

Invoke-Command -ComputerName $ComputerName -ScriptBlock {
    $RegPath1 = "Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Wintrust\Config"
    $RegPath2 = "HKLM:\Software\Wow6432Node\Microsoft\Cryptography\Wintrust\Config"
    $ValueName = "EnableCertPaddingCheck"
    $Value = "1"

    $RegistryKey = Test-Path -Path $RegPath1
    if ($RegistryKey -eq "True") {
        Write-Host -f Green "***Certificate Padding is Already Enabled***"
    }
    else {
            New-Item -Path $RegPath1
            New-ItemProperty -Path $RegPath1 -Name $ValueName -Value $Value

            New-Item -Path $RegPath2
            New-ItemProperty -Path $RegPath2 -Name $ValueName -Value
            if ($RegistryKey -eq "True") {
                Write-Host -f Green "Certificate Padding Has Been Enabled."
            }
            else {
                Write-Host -f Red "Something Went Wrong!"
            }
    }

字符串
然而,我在运行脚本时收到这些错误:

The registry key at the specified path does not exist.
    + CategoryInfo          : InvalidArgument: (HKEY_LOCAL_MACH...graphy\Wintrust:String) [New-Item], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.NewItemCommand
    + PSComputerName        : localhost
Cannot find path 'HKLM:\Software\Microsoft\Cryptography\Wintrust\Config' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (HKLM:\Software\...Wintrust\Config:String) [Set-ItemProperty], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetItemPropertyCommand
    + PSComputerName        : localhost
Cannot find path 'HKLM:\Software\Microsoft\Cryptography\Wintrust\Config' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (HKLM:\Software\...Wintrust\Config:String) [Set-ItemProperty], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetItemPropertyCommand
    + PSComputerName        : localhost

当我在远程计算机上输入pssession并尝试用途:

New-Item -Path "Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Wintrust\Config"


我收到第一条错误消息,提示路径不存在。这是因为我缺少父目录,而New-Item没有创建父目录吗?

myzjeezk

myzjeezk1#

$ComputerName = Read-Host "Please Enter Computer Name"

Invoke-Command -ComputerName $ComputerName -ScriptBlock {
    $RegPath1 = "Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Wintrust\Config"
    $RegPath2 = "HKLM:\Software\Wow6432Node\Microsoft\Cryptography\Wintrust\Config"
    $ValueName = "EnableCertPaddingCheck"
    $Value = "1"

    $RegistryKey = Test-Path -Path $RegPath1
    if ($RegistryKey -eq "True") {
        Write-Host -f Green "***Certificate Padding is Already Enabled***"
    }
    else {
            New-Item -Path $RegPath1 -Force | Out-Null
            New-ItemProperty -Path $RegPath1 -Name $ValueName -Value $Value

            New-Item -Path $RegPath2 -Force | Out-Null
            New-ItemProperty -Path $RegPath2 -Name $ValueName -Value
            if ($RegistryKey -eq "True") {
                Write-Host -f Green "Certificate Padding Has Been Enabled."
            }
            else {
                Write-Host -f Red "Something Went Wrong!"
            }
    }

字符串
这实际上是因为我需要指定父键的创建。这是新的脚本。感谢@Mathias R. Kristen确认这一点。

相关问题