Hello, I need some help to solve/fix my Powershell function. I've 3 Powershell scripts: Spoiler - 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 : Spoiler - 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: Spoiler 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
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 } }
My pleasure i actually recently learned that the closing "@ should always be on the line beginning, without any leading space
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.