[SOLVED] Create an XML file using Powershell according to parameter (OS type : w7, w81, w10)

Discussion in 'Scripting' started by Kenafri, Sep 24, 2019.

  1. Kenafri

    Kenafri MDL Member

    Jul 22, 2014
    103
    49
    10
    #1 Kenafri, Sep 24, 2019
    Last edited: Sep 24, 2019
    Hello,

    I need some help to solve/fix my Powershell function.

    I've 3 Powershell scripts:
    - w7xml.ps1
    Code:
    $w7xml = @"
    <?xml version="1.0" encoding="UTF-8"?>
    
    <Wallpapers>
        <OperatingSystem Type="Clients" ApplicabilityInfo="Windows 6.1 Client" Edition="ALL" Build="ALL" Language="Neutral" Architecture="ALL">
            <Image Index="1" Name="Windows 7 Wallpapers - Architecture" />
            <Image Index="2" Name="Windows 7 Wallpapers - Characters" />
            <Image Index="3" Name="Windows 7 Wallpapers - Landscapes" />
            <Image Index="4" Name="Windows 7 Wallpapers - Nature" />
            <Image Index="5" Name="Windows 7 Wallpapers - Scenes" />
            <Image Index="6" Name="Windows 7 Wallpapers - Windows (Default Wallpaper)" />
        </OperatingSystem>
    </Wallpapers>
    "@
    
    $w7xml | out-file "$env:userprofile\desktop\Custom-w7xml.xml"
    

    - w81xml.ps1
    Code:
    $w81xml = @"
    <?xml version="1.0" encoding="UTF-8"?>
    
    <Wallpapers>
        <OperatingSystem Type="Clients" ApplicabilityInfo="Windows 8.1 Client" Edition="ALL" Build="ALL" Language="Neutral" Architecture="ALL">
            <Image Index="1" Name="Windows 8.1 Wallpapers - Screen" />
            <Image Index="2" Name="Windows 8.1 Wallpapers - Theme1" />
            <Image Index="3" Name="Windows 8.1 Wallpapers - Theme2" />
            <Image Index="4" Name="Windows 8.1 Wallpapers - Windows" />
        </OperatingSystem>
    </Wallpapers>
    "@
    
    $w81xml | out-file "$env:userprofile\desktop\Custom-w81.xml"
    

    - w10xml.ps1
    Code:
    $w10xml = @"
    <?xml version="1.0" encoding="UTF-8"?>
    
    <Wallpapers>
        <OperatingSystem Type="Clients" ApplicabilityInfo="Windows 10.0 Client" Edition="ALL" Build="ALL" Language="Neutral" Architecture="ALL">
            <Image Index="1" Name="Windows 10 Wallpapers - 4K" />
            <Image Index="2" Name="Windows 10 Wallpapers - Screen" />
            <Image Index="4" Name="Windows 10 Wallpapers - Theme1" />
            <Image Index="5" Name="Windows 10 Wallpapers - Theme2" />
            <Image Index="6" Name="Windows 10 Wallpapers - Windows" />
        </OperatingSystem>
    </Wallpapers>
    "@
    $w10xml | out-file "$env:userprofile\desktop\Custom-w10.xml"
    

    Each one generate an XML file according to OS type :
    - w7_CustomItems.xml (output from w7xml.ps1 script)
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <Wallpapers>
        <OperatingSystem Type="Clients" ApplicabilityInfo="Windows 6.1 Client" Edition="ALL" Build="ALL" Language="Neutral" Architecture="ALL">
            <Image Index="1" Name="Windows 7 Wallpapers - Architecture" />
            <Image Index="2" Name="Windows 7 Wallpapers - Characters" />
            <Image Index="3" Name="Windows 7 Wallpapers - Landscapes" />
            <Image Index="4" Name="Windows 7 Wallpapers - Nature" />
            <Image Index="5" Name="Windows 7 Wallpapers - Scenes" />
            <Image Index="6" Name="Windows 7 Wallpapers - Windows (Default Wallpaper)" />
        </OperatingSystem>
    </Wallpapers>
    
    - w81_CustomItems.xml (output from w81xml.ps1 script)
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <Wallpapers>
        <OperatingSystem Type="Clients" ApplicabilityInfo="Windows 8.1 Client" Edition="ALL" Build="ALL" Language="Neutral" Architecture="ALL">
            <Image Index="1" Name="Windows 8.1 Wallpapers - Screen" />
            <Image Index="2" Name="Windows 8.1 Wallpapers - Theme1" />
            <Image Index="3" Name="Windows 8.1 Wallpapers - Theme2" />
            <Image Index="4" Name="Windows 8.1 Wallpapers - Windows" />
        </OperatingSystem>
    </Wallpapers>
    
    - w10_CustomItems.xml (output from w10xml.ps1 script)
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <Wallpapers>
        <OperatingSystem Type="Clients" ApplicabilityInfo="Windows 10.0 Client" Edition="ALL" Build="ALL" Language="Neutral" Architecture="ALL">
            <Image Index="1" Name="Windows 10 Wallpapers - 4K" />
            <Image Index="2" Name="Windows 10 Wallpapers - Screen" />
            <Image Index="4" Name="Windows 10 Wallpapers - Theme1" />
            <Image Index="5" Name="Windows 10 Wallpapers - Theme2" />
            <Image Index="6" Name="Windows 10 Wallpapers - Windows" />
        </OperatingSystem>
    </Wallpapers>
    

    I'm tryin' to merge all 03 Powershell scripts in 01 named [Generate_XML.ps1] like this:
    Code:
    Function Generate-XMList {
        [CmdletBinding()]
    
        Param (
          # [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
          [Parameter(Mandatory=$True)]
          [ValidateSet('w7', 'w81', 'w10', IgnoreCase=$True)]
          [String]$SourceOS
        )
    
        Switch ($SourceOS){
            'w7'{' $w7xml = @"
                <?xml version="1.0" encoding="UTF-8"?>
    
                <Wallpapers>
                    <OperatingSystem Type="Clients" ApplicabilityInfo="Windows 6.1 Client" Edition="ALL" Build="ALL" Language="Neutral" Architecture="ALL">
                        <Image Index="1" Name="Windows 7 Wallpapers - Architecture" />
                        <Image Index="2" Name="Windows 7 Wallpapers - Characters" />
                        <Image Index="3" Name="Windows 7 Wallpapers - Landscapes" />
                        <Image Index="4" Name="Windows 7 Wallpapers - Nature" />
                        <Image Index="5" Name="Windows 7 Wallpapers - Scenes" />
                        <Image Index="6" Name="Windows 7 Wallpapers - Windows (Default Wallpaper)" />
                    </OperatingSystem>
                </Wallpapers>
                "@
                $w7xml | out-file "$env:userprofile\desktop\w7_CustomItems.xml"
            '}
            'w81'{' $w81xml = @"
                <?xml version="1.0" encoding="UTF-8"?>
    
                <Wallpapers>
                    <OperatingSystem Type="Clients" ApplicabilityInfo="Windows 8.1 Client" Edition="ALL" Build="ALL" Language="Neutral" Architecture="ALL">
                        <Image Index="1" Name="Windows 8.1 Wallpapers - Screen" />
                        <Image Index="2" Name="Windows 8.1 Wallpapers - Theme1" />
                        <Image Index="3" Name="Windows 8.1 Wallpapers - Theme2" />
                        <Image Index="4" Name="Windows 8.1 Wallpapers - Windows" />
                    </OperatingSystem>
                </Wallpapers>
                "@
                $w81xml | out-file "$env:userprofile\desktop\w81_CustomItems.xml"
            '}
            'w10'{' $w10xml = @"
                <?xml version="1.0" encoding="UTF-8"?>
    
                <Wallpapers>
                    <OperatingSystem Type="Clients" ApplicabilityInfo="Windows 10.0 Client" Edition="ALL" Build="ALL" Language="Neutral" Architecture="ALL">
                        <Image Index="1" Name="Windows 10 Wallpapers - 4K" />
                        <Image Index="2" Name="Windows 10 Wallpapers - Screen" />
                        <Image Index="3" Name="Windows 10 Wallpapers - Theme1" />
                        <Image Index="4" Name="Windows 10 Wallpapers - Theme2" />
                        <Image Index="5" Name="Windows 10 Wallpapers - Windows" />
                    </OperatingSystem>
                </Wallpapers>
                "@
                $w10xml | out-file "$env:userprofile\desktop\w10_CustomItems.xml"
            '}
        }
    }
    

    To create w7_CustomItems.xml, i'd like to use it as this :
    Code:
    PS>.\Generate_XML.ps1 -SourceOS w7
    

    BUT, none xml file is generated.

    Any help will be appreciated.

    THANX
     
  2. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    11,066
    47,199
    340
    Code:
    Param (
        [Parameter(Mandatory=$True)]
          [ValidateSet('w7', 'w81', 'w10', IgnoreCase=$True)]
          [String]$SourceOS
    )
    
    Switch ($SourceOS){
        w7{ $w7xml = @"
    <?xml version="1.0" encoding="UTF-8"?>
    
    <Wallpapers>
        <OperatingSystem Type="Clients" ApplicabilityInfo="Windows 6.1 Client" Edition="ALL" Build="ALL" Language="Neutral" Architecture="ALL">
            <Image Index="1" Name="Windows 7 Wallpapers - Architecture" />
            <Image Index="2" Name="Windows 7 Wallpapers - Characters" />
            <Image Index="3" Name="Windows 7 Wallpapers - Landscapes" />
            <Image Index="4" Name="Windows 7 Wallpapers - Nature" />
            <Image Index="5" Name="Windows 7 Wallpapers - Scenes" />
            <Image Index="6" Name="Windows 7 Wallpapers - Windows (Default Wallpaper)" />
        </OperatingSystem>
    </Wallpapers>
    "@
        $w7xml | out-file "$env:userprofile\desktop\w7_CustomItems.xml" -Encoding utf8 -NoNewline
        }
        w81{ $w81xml = @"
    <?xml version="1.0" encoding="UTF-8"?>
    
    <Wallpapers>
        <OperatingSystem Type="Clients" ApplicabilityInfo="Windows 8.1 Client" Edition="ALL" Build="ALL" Language="Neutral" Architecture="ALL">
            <Image Index="1" Name="Windows 8.1 Wallpapers - Screen" />
            <Image Index="2" Name="Windows 8.1 Wallpapers - Theme1" />
            <Image Index="3" Name="Windows 8.1 Wallpapers - Theme2" />
            <Image Index="4" Name="Windows 8.1 Wallpapers - Windows" />
        </OperatingSystem>
    </Wallpapers>
    "@
        $w81xml | out-file "$env:userprofile\desktop\w81_CustomItems.xml" -Encoding utf8 -NoNewline
        }
        w10{ $w10xml = @"
    <?xml version="1.0" encoding="UTF-8"?>
    
    <Wallpapers>
        <OperatingSystem Type="Clients" ApplicabilityInfo="Windows 10.0 Client" Edition="ALL" Build="ALL" Language="Neutral" Architecture="ALL">
            <Image Index="1" Name="Windows 10 Wallpapers - 4K" />
            <Image Index="2" Name="Windows 10 Wallpapers - Screen" />
            <Image Index="3" Name="Windows 10 Wallpapers - Theme1" />
            <Image Index="4" Name="Windows 10 Wallpapers - Theme2" />
            <Image Index="5" Name="Windows 10 Wallpapers - Windows" />
        </OperatingSystem>
    </Wallpapers>
    "@
        $w10xml | out-file "$env:userprofile\desktop\w10_CustomItems.xml" -Encoding utf8 -NoNewline
        }
    }
     
  3. Kenafri

    Kenafri MDL Member

    Jul 22, 2014
    103
    49
    10
    Hi!

    Many thanx for quick help, abbodi1406!
    Really appreciated. As always
     
  4. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    11,066
    47,199
    340
    My pleasure

    i actually recently learned that the closing "@ should always be on the line beginning, without any leading space
     
  5. Kenafri

    Kenafri MDL Member

    Jul 22, 2014
    103
    49
    10
    Hi abbodi1406!

    I've realised that code like below:
    Code:
    $w10xml | out-file "$env:userprofile\desktop\w10_CustomItems.xml" -Encoding utf8 -NoNewline
    
    dosn't produce an UTF-8 encoded file but an UTF-8 with BOM file.

    Any tip/code to fix it?

    Thanx again.
     
  6. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    11,066
    47,199
    340
    Replace utf8 with ascii
     
  7. Kenafri

    Kenafri MDL Member

    Jul 22, 2014
    103
    49
    10
    Works like a charm!
    Thanx again