如何添加或更新注册表项? [英] How to add or update registry keys?
本文介绍了如何添加或更新注册表项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 Powershell 脚本,它从 JSON 文件中读取内容并添加注册表值.如果注册表中的键存在,我的脚本运行良好.如果密钥不存在,如何让它创建密钥,然后向其插入值?
I have a Powershell script that reads content from JSON file and adds registry values. My script works well if the key in the registry exists. How can I make it to create the key if it doesn't exist, and then insert a value to it?
Powershell:
Powershell:
$jsonFile = "c:\temp\story\test2.json"
$customObject = Get-Content $jsonFile | ConvertFrom-Json
$customObject.PSObject.Properties | ForEach-Object {
[void]( New-ItemProperty -LiteralPath $_.Value.path -Name $_.Value.name -Value $_.Value.value -PropertyType $_.Value.type -Force)
}
杰森:
{
"reg1": {
"path": "HKLM:\\SOFTWARE\\Clients\\test",
"name": "test",
"value": "0000",
"type": "String"
},
"reg2": {
"path": "HKLM:\\SOFTWARE\\Clients\\test",
"name": "test2",
"value": "111",
"type": "String"
}
}
推荐答案
可以使用Test-Path
来测试注册表路径是否存在.
You can use Test-Path
to test if the registry path exists or not.
Get-Content -Path 'c:\temp\story\test2.json' -Raw | ConvertFrom-Json | ForEach-Object {
foreach ($reg in $_.PSObject.Properties.value) {
if (!(Test-Path -Path $reg.path)) { $null = New-Item -Path $reg.path -Force }
$null = New-ItemProperty -Path $reg.path -Name $reg.name -Value $reg.value -PropertyType $reg.type -Force
}
}
这篇关于如何添加或更新注册表项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文