Je vais créer un fichier xml. Les éléments du fichier xml sont choisis dans le fichier .ini. J'ai plus d'un fichier .ini. J'ai essayé, mais je ne peux toujours créer qu'un seul élément.
Il y a deux types de fichiers de contenu dans mon fichier .ini. Le premier.
[Product]
Name = NB A
String = Defaults and Exit
[Controller1]
Desc = Embedded Intel RAID
[Controller2]
Desc = Intel Optane Device
2ème.
[Product]
Name = NB A
String = Defaults and Exit
[Controller1]
Desc = Embedded SATA
Je m'attends à ce que la sortie du XML soit de deux types. Le premier.
<Product Name=NB A>
<Controller controllertype="raid">
<Controller controllertype="optane">
</Product>
2ème.
<Product Name=NB A>
<Controller controllertype="raid">
</Product>
Le code que j'ai écrit jusqu'à présent :
$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()
Toute personne peut aider, s'il vous plaît. Les conseils et les solutions sont les bienvenus. Merci de votre aide.