Я собираюсь создать xml файл. Элемент xml, который я выбираю из файла .ini. У меня есть файл .ini больше 1. Я пробую это, но все еще могу создать только 1 элемент.
В моем файле .ini есть два типа файлов содержимого типа. 1-й.
[Product]
Name = NB A
String = Defaults and Exit
[Controller1]
Desc = Embedded Intel RAID
[Controller2]
Desc = Intel Optane Device
2-й.
[Product]
Name = NB A
String = Defaults and Exit
[Controller1]
Desc = Embedded SATA
Мой ожидаемый вывод XML, будет 2 типа. 1-й.
<Product Name=NB A>
<Controller controllertype="raid">
<Controller controllertype="optane">
</Product>
2-й.
<Product Name=NB A>
<Controller controllertype="raid">
</Product>
Код, который я написал до сих пор:
$ProductListXML = "D:\TST.XML"
$driver = "D:\ver.xml"
# get an XMLTextWriter to create the XML
$XmlWriter = New-Object System.XMl.XmlTextWriter($ProductListXML,$Null)
# choose a pretty formatting:
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"
# write the header
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteStartElement('ProductList')
$xmlWriter.WriteAttributeString('Version', '200422a')
# load it into an XML object:
$xml = New-Object -TypeName XML
$xml.Load($driver)
$FamilyCode = $Xml.drivergroup.family | Select-Object -ExpandProperty Code
foreach ($fc in $FamilyCode)
{
$FCDesc = $Xml.drivergroup.family | Where-Object { $_.code -eq "$fc" }| Select-Object -ExpandProperty description
$SystemID = $Xml.drivergroup.family | Where-Object { $_.code -eq "$fc" } | Select-Object -ExpandProperty Product | Select-Object -ExpandProperty systemid
$IDSplit = $SystemID -split "/"
$XmlWriter.WriteStartElement('Family')
$XmlWriter.WriteAttributeString('code', "$fc")
$XmlWriter.WriteAttributeString('decription', "$FCDesc")
foreach($sid in $IDSplit)
{
$ID = Get-ChildItem -path "D:\product\*$sid*" | Select-Object -ExpandProperty BaseName
foreach ($id in $ID)
{
$File = Get-ChildItem -path "D:\product\*$id*"
$ReadINI = @{}
Get-Content "$File" | ForEach-Object {
$_.Trim()
} | Where-Object {
$_ -notmatch '^(;|$)'
} | ForEach-Object {
if ($_ -match '^\[.*\]$') {
$section = $_ -replace '\[|\]'
$ReadINI[$section] = @{}
} else {
$key, $value = $_ -split '\s*=\s*', 2
$ReadINI[$section][$key] = $value
}
}
$ProductName = $ReadINI["Product"]["Name"]
$TechType = $ReadINI["Controller1"]["Node"]
if($TechType -eq "Intel")
{
$TechType = "Intel"
}
else{
$TechType = "AMD"
}
$FactoryDefaultsString = $ReadINI["Product"]["String"]
$YearPath = "D:\*.txt"
$YearMapping = Select-String -Path $YearPath -Pattern "$id"
if($YearMapping -like "*2017*")
{
$Year = "2017"
}
elseif($YearMapping -like "*2018*")
{
$Year = "2018"
}
elseif($YearMapping -like "*2019*")
{
$Year = "2019"
}
elseif($YearMapping -like "*2020*")
{
$Year = "2020"
}
elseif($YearMapping -like "*2021*")
{
$Year = "2021"
}
$XmlWriter.WriteStartElement('Product')
$XmlWriter.WriteAttributeString('Name', "$ProductName")
$XmlWriter.WriteAttributeString('SSID', "$id")
$XmlWriter.WriteAttributeString('TechType', "$TechType")
$XmlWriter.WriteAttributeString('Year', "$Year")
$XmlWriter.WriteAttributeString('FactoryDefaultsString', "$FactoryDefaultsString")
$Controller2 = Select-String -Path $File -Pattern "Controller2" | Select-Object -ExpandProperty Filename
foreach ($cont2 in $Controller2)
{
$file = Get-ChildItem "D:\product\$cont2"
$ReadTechTp = @{}
Get-Content "$file" | ForEach-Object {
$_.Trim()
} | Where-Object {
$_ -notmatch '^(;|$)'
} | ForEach-Object {
if ($_ -match '^\[.*\]$') {
$section = $_ -replace '\[|\]'
$ReadTechTp[$section] = @{}
} else {
$key, $value = $_ -split '\s*=\s*', 2
$ReadTechTp[$section][$key] = $value
}
}
$Desc = $ReadTechTp["Controller2"]["Desc"]
if($Desc -like "*RAID*" -or $Controller1 -like "*SATA*")
{
$ControllerType = "RAID"
}
else{
$ControllerType = "OPTANE"
}
$XmlWriter.WriteStartElement('Controller')
$XmlWriter.WriteAttributeString('ControllerType', "$ControllerType")
}
$xmlWriter.WriteEndElement()
}
}
$xmlWriter.WriteEndElement()
}
$xmlWriter.WriteEndElement()
# finalize the document:
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
Любой может помочь, пожалуйста. Советы и решения действительно ценятся. Спасибо