Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Convert a ini-file to XLM-Array , filter specific Values in the XML Array and write the output to XML and back to ini-file

$
0
0

Hello,

I have recently posted a question related to ini-file manipulations with powershell, but i got no anser in this case. Obviously there is no chance to solve the problem with the ini-file in powershell. I got an advice to do the needed job by Powershell in XML-File instead of manipulating the ini-File.

Therefore i have converted the ini-file to XML-Array by Powershell in order to manipulate and filter out some sentences easier with the methods of XML than in ini-file. Converting to XML and back to ini-file is working well already, but my knowledge in Powershell and XML is too poor that i could do the filtering and manipulations needed.

Needed manipulations in XML

1. Actually i need help to get the output of the XML-Strings to a xlm - file  that i read from an ini-file below in a XML file format for supervising and testing. 

2. Inside XML Array, I want to filter out all the values, (strings) in bracket of the former INI-Key Arguments= [path\script.vbs....] if the value occour in multiple sections.

3. In addition I want to filter out all values (strings) in brackets inside XML Array, of the former INI-Key Groups= [Domain\Path] if the value occour in multiple sections.

4. If the described criterias in 2. and 3. are matching i want to eliminate all redundant sections.

Please can somebody help me ???Smile

Input file (part of original Ini-file, where the filter cases will match)

Search criteria: Always when both of the keys UserGroup and in the keys Arguments= have similar entries in different sections, (entries marked underlined) (occuring in different sections SASIMAP and SASOLAP), then the complete redundant sections should be deleted.

[SASIMAP]
FileName=C:\Program Files (x86)\VBScript\CScript.exe
Arguments="C:\Windows\Application Compatibility Scripts\Logon\SASEGUIDEADM4.vbs" //B //NoLogo //T:60
UserGroups=Domainname\gl_w_SASIMAP
Description=SASIMAP
Wait=0
WindowStyle=1
[SASOLAP]
FileName=C:\Program Files (x86)\VBScript\CScript.exe
Arguments="C:\Windows\Application Compatibility Scripts\Logon\SASEGUIDEADM4.vbs" //B //NoLogo //T:60
UserGroups=Domainname\gl_w_SASIMAP
Description=SASOLAP
Wait=0
WindowStyle=1

Program Code

Convert ini-File to XML (it workes already)

Function Parse-IniFile ($file)
{
    [String]$iniXMLString = '<?xml version="1.0" ?><ini>'
    $opentag = ""
    switch -regex -file $file
    {
        "^\[(.+)\]$"
        {
            if ($opentag -ne "") { $iniXMLString += "</" + $opentag + ">" }
            $section = $matches[1].Trim()
            $iniXMLString += "<" + $section + ">"
            $opentag = $section
        }
        "^\s*([^#].+?)\s*=\s*(.*)"
        {
            $name, $value = $matches[1..2]
            $iniXMLString += "<" + $name + ">" + $value + "</" + $name + ">"
        }
    }
    if ($tagopen -ne "") { $iniXMLString += "</" + $opentag + ">" }
    $iniXMLString += "</ini>"
    Return $iniXMLString
}
# Parse Ini / Ini-File einlesen
$iniXMLString = Parse-IniFile "C:\Program Files\visionapp\LoginManager\LoginMgr_old.ini"
[xml]$iniXML = $iniXMLString

# Funktion Loop XML (Einlesen Child Nodes)
Function Loop-XML($node, $first)
{
    $returnValue = ""
    if ($node.HasChildNodes)
    {
        if ($node.FirstChild.Name -eq "#text")
        {
            $returnValue += ($node.name + "=" + $node.FirstChild.value) + "`r`n"
        }
        else
        {
            if (!$first)
            {
                $returnValue += ("[" + $node.name + "]") + "`r`n"
            }
        }
        foreach ($item in $node.ChildNodes)
        {
            $returnValue += (Loop-XML $item $FALSE)
        }
    }
Return $returnValue
}

Converting XML Array back to a new ini-file (it workes already)

$newIni = Loop-XML $iniXML.ini $true
Set-Content "C:\Program Files\visionapp\LoginManager\New_LoginMgr.ini" $newIni


Viewing all articles
Browse latest Browse all 6937

Trending Articles