2012-01-19 16 views
2

我目前正在嘗試訪問註冊表,獲取所有與它的適當值的子鍵,然後用XML配置替換這些值。Powershell - 使用註冊表值查找和替換XML配置數據

例如:

在XML文檔下面的值存儲:

<Name = "Test" Value = "\\somelocation\TOKEN\Application" /> 
<Name = "Test1" Value = "\\somelocation\TOKEN\Deployment" /> 

註冊表項持有令牌值:

TOKEN = LifeCycleManagement

因此,我想使用「\ somelocation \ LifeCycleManagement *」替代「\ somelocation \ TOKEN *」的powershell

有什麼想法嗎?

目前我想下面的代碼:

$lineElement = @() 

$regItems = Get-ItemProperty registrylocation 
Get-ItemProperty registrylocation > c:\DEV\output.txt 
$contents = Get-Content c:\DEV\output.txt 

foreach ($line in $contents) 
{ 
    $line = $line -split(":") 
    $lineElement += $line[0] 
} 

foreach ($element in $lineElement) 
{ 
    $element 
    $regItems.$element 
} 

的$ regItems $元素不會返回任何結果。

回答

2

在你的代碼,通常是$line最初通常是這樣的:

Token........: LifeCycleManagement。當你在:上劃線時,拿第一部分時你會得到Token..........是空格)。顯然$regItems.Token.........不是你以後的。你應該擺脫$line末尾的空格。這可以使用Trim()完成。下面的示例代碼將解決您的問題。

$lineElement = @() 

$regItems = Get-ItemProperty registrylocation 
Get-ItemProperty registrylocation > c:\DEV\output.txt 
$contents = Get-Content c:\DEV\output.txt 

foreach ($line in $contents) 
{ 
    $line = $line -split(":") 
    $lineElement += ($line[0]).Trim() 
} 

foreach ($element in $lineElement) 
{ 
    $element 
    $regItems.$element 
} 
相關問題