Search code examples
powershellprinters

Generating Printer Preferences XML


I'm writing a script that takes the name of a printer from a csv file and for each printer it is supposed to modify a xml printer preference, change the name of the printer, generate a new guid and output the new xml file. It almost works. Problem is that the script also changes the clsid, because it has the same regex as the uid. Only the uid is supposed to change, in other words: only the second occurrence of the matching regex.

Heres what the xml file looks like:

<SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="cop-fibu-eink" status="cop-fibu-eink" image="1" changed="2021-02-18 14:52:29" uid="{7D783062-5840-4CBD-BE9A-6334FD87A8D6}" removePolicy="1" userContext="1" bypassErrors="1"><Properties action="R" comment="" path="\\srv-print01\cop-fibu-eink" location="" default="1" skipLocal="0" deleteAll="0" persistent="0" deleteMaps="0" port=""/><Filters><FilterGroup bool="AND" not="0" name="EKBDOM1\cop-fibu-eink" sid="S-1-5-21-356607561-1380008178-1848903544-16152" userContext="0" primaryGroup="0" localGroup="0"/></Filters></SharedPrinter>

And this is the Powershell script:

Import-Csv .\printer_groups.csv | ForEach-Object { 
    $uid = ([guid]::NewGuid()).guid
    ((Get-Content -path C:\pc_inst\druckerliste\cop-it.xml -Raw) -replace "cop-fibu-eink", $($_.name)` -replace "\{[0-9a-z\-]*\}", $uid ) | Set-Content -Path .\xmls\$($_.name).xml 
}

I can't figure it out. Hope you guys can help me. (:


Solution

  • My colleague figured it out. In case anyone is wondering:

    Import-Csv C:\pc_inst\druckerliste\printer_groups.csv | ForEach-Object { 
        $uid = ([guid]::NewGuid()).guid
        ((Get-Content -path C:\pc_inst\druckerliste\cop-it.xml -Raw) -replace "cop-fibu-eink", $($_.name)` -replace 'uid="\{[0-9a-z\-]*\}"', ('uid="{' + $uid + '}"') ) | Set-Content -Path C:\pc_inst\druckerliste\drucker_xml_standard\$($_.name).xml 
    }