如何更改 powershell 脚本输出行为

时间:2021-06-10 15:56:40

标签: powershell

我不是脚本编写者,请如果有人可以通过删除 UCPID 值来帮助我修改以下脚本,但只保留 servername

目前在从 csv 文件中查找两列的脚本下面,现在我想改变行为以只查找 ServerName 因为现在 CSV 文件只有一列,其中每行只包含服务器并且更新相关XML。

$data = Import-Csv .\MyFile.csv
$luTable = @{}

# Create Keys in Lookup Table
$data | % {
    if (!$luTable.ContainsKey("$($_.ServerName)")) { $luTable["$($_.UCPID)"] = New-Object System.Collections.ArrayList }
}

$luTable.Keys | % {
    $key = $_ # Store Key
    $data | where UCPID -Match $_ | select ServerName | % {
        $luTable[$key].Add($_.ServerName)
    }
}

# Build XML Files

$luTable.Keys | % {
    $key = $_

    $filetext = gc ".\MyXML.xml"
    $filetext = $filetext.Replace("#Title#", $key)

    $targets = ""
    
    $luTable[$key] | % {
        $targets += "<ComputerName>$($_)</ComputerName>"
    }

    $filetext = $filetext.Replace("#computername#", $targets)

    sc -Path ".\$($key).xml" -Value $filetext
}

我尝试删除以下代码,但没有帮助。

# Create Keys in Lookup Table
    $data | % {
        if (!$luTable.ContainsKey("$($_.ServerName)")) { $luTable["$($_.UCPID)"] = New-Object System.Collections.ArrayList }
    }

//CSV 文件内容

ServerName
Server1
Server2
Server3
Server4
Server5

//XML - 我想要复制服务器的位置

        <AnnounceOffer>false</AnnounceOffer>
            <OfferCategory>false</OfferCategory>
            <OfferDescriptionHTML>false</OfferDescriptionHTML>
        </SettingsLocks>
        <IsUrgent>false</IsUrgent>
        <Target>
            #computername#
        </Target>
    </SingleAction>
</BES>

#computername# 必须替换为下面-

<ComputerName>Server1</ComputerName>
<ComputerName>Server2</ComputerName>
<ComputerName>Server3</ComputerName>
<ComputerName>Server4</ComputerName>

1 个答案:

答案 0 :(得分:1)

如果您的 XML 如下所示:

<BES>
    <SingleAction>
        <SettingsLocks>
            <AnnounceOffer>false</AnnounceOffer>
            <OfferCategory>false</OfferCategory>
            <OfferDescriptionHTML>false</OfferDescriptionHTML>
        </SettingsLocks>
        <IsUrgent>false</IsUrgent>
        <Target>
            #computername#
        </Target>
    </SingleAction>
</BES>

那么这里有两种选择:

方法 1:使用 PowerShell 的 XML 功能

# load the xml from file
$xml= New-Object System.XML.XMLDocument
$xml.Load("D:\Test\MyXML.xml")

# select the node with the #computername# placeholder inside
$targetNode = $xml.SelectSingleNode('//BES/SingleAction/Target')
$targetNode.'#text' = ''  # remove the placeholder text

# read the servernames from file and create and insert new nodes for each of them
(Import-Csv -Path 'D:\Test\AllServers.csv').ServerName | 
  ForEach-Object { 
    $newNode = $xml.CreateElement('ComputerName')
    $newNode.InnerText = $_
    $targetNode.AppendChild($newNode)
  }

# save the updated XML
$xml.Save('D:\Test\NewXml.xml')

方法二:把xml当成纯文本,做一个简单的文本替换

# read the XML as multiline text
$xml = Get-Content -Path 'D:\Test\MyXML.xml' -Raw

# find the line where the #computername# placeholder is and get the indentation value
$indent = ' ' * ($xml | Select-String -Pattern '(?m)^(\s+)#computername#').Matches[0].Groups[1].Length

# read the servernames from file and construct a multiline string
$servers = ((Import-Csv -Path 'D:\Test\AllServers.csv').ServerName | 
             ForEach-Object { "$indent<ComputerName>$_</ComputerName>" }) -join [environment]::NewLine

# now replace in the xml and write to (new) file
$xml -replace "(?m)^$indent#computername#", $servers | Set-Content -Path 'D:\Test\NewXml.xml' -Encoding UTF8

两种情况下的结果:

<BES>
    <SingleAction>
        <SettingsLocks>
            <AnnounceOffer>false</AnnounceOffer>
            <OfferCategory>false</OfferCategory>
            <OfferDescriptionHTML>false</OfferDescriptionHTML>
        </SettingsLocks>
        <IsUrgent>false</IsUrgent>
        <Target>
            <ComputerName>Server1</ComputerName>
            <ComputerName>Server2</ComputerName>
            <ComputerName>Server3</ComputerName>
            <ComputerName>Server4</ComputerName>
            <ComputerName>Server5</ComputerName>
        </Target>
    </SingleAction>
</BES>