Powershell,从 XML 导入注册表值



这是我的第一篇文章,我对PowerShell有点陌生,所以对于任何发布问题,我深表歉意。

我在尝试从格式化的 XML 文件导入注册表值时遇到一些问题。我可以读取 XML 的内容,并使用 foreach 循环访问 XML 代码,但我在尝试构建 New-ItemProperty 命令时遇到了困难。下面是 XML 示例:

<?xml version="1.0" encoding="utf-8" ?>
<REGUPDATES>
<SASR>
<Reg>
<Key>HKLM:SomeKeyPath0011122222</Key>
<Name>Something.exe</Name>
<Type>DWORD</Type>
<Value>00000001</Value>
</Reg>
<Reg>
<Key>HKLM:SomeKeyPath33344455555</Key>
<Name>Something.exe</Name>
<Type>DWORD</Type>
<Value>00000002</Value>
</Reg>
</SASR>
</REGUPDATES>

下面是PowerShell代码示例:

[xml]$RegUpdates = Get-Content C:TempRegUpdates.xml 
$SASRReg = $RegUpdates.REGUPDATES.SASR.Reg
$SASRKey = $RegUpdates.Regupdates.SASR.Reg.Key
$SASRName = $RegUpdates.Regupdates.SASR.Reg.Name 
$SASRType = $RegUpdates.Regupdates.SASR.Reg.Type 
$SASRValue = $RegUpdates.Regupdates.SASR.Reg.Value 
foreach ($IAVA in ($SASRReg))
{
New-ItemProperty -Path ([string]$SASRKey) -Name ([string]$SASRName) -     
PropertyType ([string]$SASRType) -Value ([string]$SASRValue)

似乎发生的情况是注册表项值(以及其他变量(被串在一起。这是我看到的错误:

New-ItemProperty : Cannot find path 'HKLM:SomeKeyPath0011122222 
HKLM:SomeKeyPath33344455555' because it does not exist.

提前感谢您的任何帮助!

你需要一个循环来处理数组:

$xml = [xml](Get-Content -Path myfile.xml)
foreach ($update in $xml.REGUPDATES.SASR.REG) {
$propArgs = @{
Path         = $update.Key
Name         = $update.Name
Value        = $update.Value
PropertyType = $update.Type
Force        = $true
}
New-ItemProperty @propArgs
}

所有 xml 元素都已被视为字符串,因此无需强制转换它们。

最新更新