检查注册表项,然后在Powershell中递增

时间:2012-11-16 23:22:15

标签: windows powershell windows-7 registry

我正在尝试检查是否存在reg密钥,然后增加一个reg密钥。我确认如果我直接输入值,我可以设置注册表值。即输入值3。

$path = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\SharedDLLs"
$psv = Get-ItemProperty -path $path
$value = $psv."c:\windows\system32\test.dll"

if(!(Test-Path $value))
    {
    Set-ItemProperty -path $path -name $key -Type DWORD -value $value++
    }
Else
    {
    echo "error 1"
    }

2 个答案:

答案 0 :(得分:1)

$value是一个数字,因此Test-Path $value总是错误的。你想检查哪条路?文件系统路径c:\ windows \ system32 \ test.dll或注册表项路径?如果它是注册表键路径,您知道它存在,因为您在其上执行了Get-ItemProperty。

答案 1 :(得分:1)

作为管理员,请使用您自己的值更改$dllToCheck并尝试以下操作:

$regPath = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\SharedDLLs"
$dllToCheck = "C:\Program Files (x86)\Hewlett-Packard\Shared\CaslVer.exe"

$exist =  get-itemproperty $regPath -name $dllToCheck -ErrorAction silentlycontinue
if ($exist -ne $null)
{
  $currentValue = $exist.$dllToCheck
  $nextValue = $currentValue + 1
  Set-ItemProperty $regPath -name $dllToCheck -Value $nextValue
}