每次运行时的注册表值

时间:2019-05-17 17:21:17

标签: powershell

我开始创建此脚本,并且陷入了我想完成的一件事。该脚本运行良好。但是我真正想做的是,当该脚本多次运行时,我想将值更改为该脚本的使用次数。我该怎么做?

$RegistryPath = "HKLM:\SOFTWARE\"
$NewRegKey = "SCCM"
$NewRegKeyPath = "HKLM:\SOFTWARE\SCCM"
$KeyName = "Attempts" 
$Value = "1"
New-Item -Path $RegistryPath -Name $NewRegKey -Force 
New-ItemProperty -Path $NewRegKeyPath -Name $KeyName -Value $Value -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null

新脚本: (工作中)

#This is creating the SCCM Hive and add the key W10_IPU_Attempts
If (Get-ItemProperty -Path 'HKLM:\SOFTWARE\SCCM' -ErrorAction SilentlyContinue) {

} Else {
$RegistryPath = "HKLM:\SOFTWARE\"
$NewRegKey = "SCCM"
New-Item -Path $RegistryPath -Name $NewRegKey -Force 
$NewRegKeyPath = "HKLM:\SOFTWARE\SCCM"
$KeyName = "W10_IPU_Attempts"
$Value = "0"
New-ItemProperty -Path $NewRegKeyPath -Name $KeyName -Value $Value -PropertyType DWORD -Force | Out-Null

}

#Set value in increment of 1 each time this script is ran!
$i= 
$Value =(Get-Itemproperty 'HKLM:\SOFTWARE\SCCM').W10_IPU_Attempts

If ($i -eq $value){

$i++
Set-ItemProperty -Path 'HKLM:\SOFTWARE\SCCM' -Name W10_IPU_Attempts -Value $i 
}
else {
$i++
Set-ItemProperty -Path 'HKLM:\SOFTWARE\SCCM' -Name W10_IPU_Attempts -Value $i
}

1 个答案:

答案 0 :(得分:0)

我建议将splatting与一堆参数一起使用。

否则,按照建议进行操作,(检查)读取当前值设置的增量值, 如果不存在,请创建密钥

## Q:\Test\2019\05\17\SO_56190969.ps1
$RegistryPath  = "HKLM:\SOFTWARE\"
$NewRegKey     = "SCCM"
$NewRegKeyPath = "HKLM:\SOFTWARE\SCCM"
$KeyName       = "Attempts"

if ($Value=[Int](Get-ItemPropertyValue $NewRegKeyPath -Name $KeyName -EA 0)){
    Set-ItemProperty -Path $NewRegKeyPath -Name $KeyName -Value (++$Value) -Type DWord
} else {
    New-Item -Path $RegistryPath -Name $NewRegKey -Force | Out-Null
    $params = @{
        Path        = $NewRegKeyPath
        Name        = $KeyName
        Value       = 1
        PropertyType= 'DWORD'
        Force       = $True
        ErrorAction = 'SilentlyContinue'
    }
    New-ItemProperty @params | Out-Null
}

"Key    : {0}`nKeyname: {1}`nValue  : {2}" -f $NewRegKeyPath,$KeyName,
    (Get-ItemPropertyValue $NewRegKeyPath -Name $KeyName)
#

两个示例运行:

> Q:\Test\2019\05\17\SO_56190969.ps1
Key    : HKLM:\SOFTWARE\SCCM
Keyname: Attempts
Value  : 1

> Q:\Test\2019\05\17\SO_56190969.ps1
Key    : HKLM:\SOFTWARE\SCCM
Keyname: Attempts
Value  : 2