对于我的powershell体验,这个是一个棘手的问题。如果有人能提供帮助,那就太棒了。
我有什么:
$csvdata = Import-CSV 'file.csv'
| Location | Description
| /Texas/Bryan | Pond Anup
| /Texas/Bryan | Pond Charlie
| /Texas/Killen/ | Lake Snoopy
示例:output.xml
<groups>
<group name="Texas">
<site name="Bryan">Pond Anup</site>
</group>
</groups>
我要做的是循环通过csv导入并执行以下操作:
我是PowerShell的新手,这与AD无关,因为我看到很多这方面的例子。我希望已经创建了一个脚本或我可以调用的cmdlet。
由于
答案 0 :(得分:0)
以下是您需要用来实现此目的的基本循环和逻辑:
$csvdata = Import-CSV "file.csv"
$outfile = "output.xml"
"<groups>" | Out-File $outfile #start groups
$lastGroup = ""
$lastSite = ""
$csvdata | foreach {
$group = ($_.Location.Split('/'))[1]
$site = ($_.Location.Split('/'))[2]
$child = $_.Description
if ($group -ne $lastGroup) #if we're not in the same group
{
if ($lastGroup -ne "") {" </group>" | Out-File $outfile -Append }
" <group name=`"" + $group + "`">" | Out-File $outfile -Append
}
if ($site -ne $lastSite) #if we're not in the same site
{
if ($lastSite -ne "") {" </site>" | Out-File $outfile -Append}
" <site name=`"" + $site + "`">" | Out-File $outfile -Append
}
# now write child:
" <child name=`"" + $child + "`"></child>" | Out-File $outfile -Append
$lastGroup = $group
$lastSite = $site
}
#write final end tags
" </site>" | Out-File $outfile -Append
" </group>" | Out-File $outfile -Append
"</groups>" | Out-File $outfile -Append #end groups
代码将为您提供如下输出:
<groups>
<group name="Texas">
<site name="Bryan">
<child name="Pond Anup"></child>
<child name="Pond Charlie"></child>
</site>
<site name="Killen">
<child name="Lake Snoopy"></child>
</site>
</group>
</groups>
希望这能让你开始朝着正确的方向前进。