生成打印机首选项 XML

Generating Printer Preferences XML

我正在编写一个脚本,该脚本从 csv 文件中获取打印机的名称,并且应该为每台打印机修改 xml 打印机首选项,更改打印机名称,生成新的guid 并输出新的 xml 文件。它几乎可以工作。问题是脚本还更改了 clsid,因为它与 uid 具有相同的正则表达式。只有 uid 应该改变,换句话说:只有匹配正则表达式的第二次出现。

xml 文件如下所示:

<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>

这是 Powershell 脚本:

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 
}

我想不通。希望你们能帮助我。 (:

我的同事想通了。如果有人想知道:

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 
}

如果您的文件内容是 xml,为什么不将其视为 xml 而不是纯文本?

$oldText = @"
<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>
"@

$xml = [xml] $oldText;

然后您可以像这样轻松更改属性值:

$xml.SharedPrinter.uid = "{" + [Guid]::NewGuid().ToString().ToUpperInvariant() + "};

并将其序列化回 xml:

$newText = $xml.OuterXml;

write-host $newText;

注意 - 这可能无关紧要,但您的原始 uid 属性包含大括号和大写字母 (uid="{7D783062-5840-4CBD-BE9A-6334FD87A8D6}"),因此我已将其匹配到新值中...

我用过类似的东西。它的结构不是很好,但可以正常工作(tm)。它完成了 GPP 的所有工作而不是你的问题,但你可以得到整体的想法。

# Configuration variables
$PrintServer = ""
$CSVFilePath = "printers.csv"
$XMLFilePath = "printers.xml"


# The code

Import-Module ActiveDirectory
$Domain = (Get-ADDomain).name

$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$Now = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")

# Set The Formatting
$XMLSettings = New-Object System.Xml.XmlWriterSettings
$XMLSettings.Indent = $true
$XMLSettings.IndentChars = "    "

# Set the File Name Create The Document
$XMLWriter = [System.XML.XmlWriter]::Create($XMLfilePath, $XMLSettings)

# Write the XML Declaration
$XMLWriter.WriteStartDocument()

# Start the Root Element
$XMLWriter.WriteComment('Root of the policy')
$XMLWriter.WriteStartElement('Printers') # Start root
$XMLWriter.WriteAttributeString('clsid', '{1F577D12-3D1B-471e-A1B7-060317597B9C}')


Import-Csv (Join-Path -Path $ScriptPath -ChildPath $CSVFilePath) | ForEach-Object {
    $GroupName = $_.Group
    [Microsoft.ActiveDirectory.Management.ADGroup]$Group = Get-ADGroup -Filter { name -like $_.GroupName }

    # Start SharedPrinter Element
    $XMLWriter.WriteStartElement('SharedPrinter') # Start SharedPrinter
    $XMLWriter.WriteAttributeString('clsid', '{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}')
    $XMLWriter.WriteAttributeString('name', $_.Printer)
    $XMLWriter.WriteAttributeString('status', $_.Printer)
    $XMLWriter.WriteAttributeString('image', '1')
    $XMLWriter.WriteAttributeString('bypassErrors', '1')
    $XMLWriter.WriteAttributeString('changed', $Now)
    $XMLWriter.WriteAttributeString('uid', [System.Guid]::NewGuid().ToString())

        # Start Properties Element
        $XMLWriter.WriteStartElement('Properties') # Start Properties
        $XMLWriter.WriteAttributeString('action', 'R')
        $XMLWriter.WriteAttributeString('comment', '')
        $XMLWriter.WriteAttributeString('path', "\$PrintServer$($_.Printer)")
        $XMLWriter.WriteAttributeString('location', '')
        $XMLWriter.WriteAttributeString('default', $_.Default)
        $XMLWriter.WriteAttributeString('skipLocal', '0')
        $XMLWriter.WriteAttributeString('deleteAll', '0')
        $XMLWriter.WriteAttributeString('persistent', '0')
        $XMLWriter.WriteAttributeString('deleteMaps', '0')
        $XMLWriter.WriteAttributeString('port', '')
        $XMLWriter.WriteEndElement() # End Properties   

        # Start Filters Element
        $XMLWriter.WriteStartElement('Filters') # Start Filters

            # Start FilterGroup Element
            $XMLWriter.WriteStartElement('FilterGroup')
            $XMLWriter.WriteAttributeString('bool', 'AND')
            $XMLWriter.WriteAttributeString('not', '0')
            $XMLWriter.WriteAttributeString('name', "$Domain$($Group.Name)")
            $XMLWriter.WriteAttributeString('sid', $Group.SID)
            $XMLWriter.WriteAttributeString('userContext', '1')
            $XMLWriter.WriteAttributeString('primaryGroup', '0')
            $XMLWriter.WriteAttributeString('localGroup', '0')
            $XMLWriter.WriteEndElement() # End FilterGroup

        $XMLWriter.WriteEndElement() # End Filters

    $XMLWriter.WriteEndElement() # End SharedPrinter
}

$XMLWriter.WriteEndElement() # End root

# End, Finalize and close the XML Document
$XMLWriter.WriteEndDocument()
$XMLWriter.Flush()
$XMLWriter.Close()