如何从数组和另一个哈希表

时间:2017-06-14 16:10:44

标签: arrays powershell

我想将键和值添加到数组和另一个哈希表中的新哈希表中。使用我的代码,我可以将哈希表值添加为新键,但我无法将每个数组元素添加为新值。我试图通过使用变量作为索引号来访问每个数组元素,但PowerShell不允许这样做。还有另一种方法吗?

$targetFile = "C:\test\Target.xml"
$targetFile = [xml](Get-Content $targetFile)

$IPlookup = @{}

$targetFile.Deployment.Target | Foreach {
$IPlookup.Add($_.hostname, $_.IP)
}

$targetList = @()

$targetList = "C:\Target 1\database","C:\Target 2\database"

$deployList = @{}

X=0

$IPlookup.Keys | foreach{
$deployList.Add($_, $targetList[X])
X += 1
}

$deployList | Out-File C:\Temp\DeployList.txt

# I would like the HashTable to have this information:
# Key            Value        
# 192.168.1.10   C:\Target 1\database
# 192.168.1.11   C:\Target 2\database

0 个答案:

没有答案