How to change powershell script output behavior

Vijay khurava

I am not a scripter, please if anyone can help me with modify below script by removing UCPID value but keep only servername

Currently below script looking two columns from csv file, now I want to change the behavior to only look for ServerName because now CSV file have only one column which containing server only in each row and update related 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
}

I tried deleting below code but its not helping.

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

//CSV file content

ServerName
Server1
Server2
Server3
Server4
Server5

//XML - location where I want server to be copied

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

#computername# must be replaced with below-

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

If your XML looks like this:

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

Then here's two alternatives for you:

Method 1: use XML functionality of PowerShell

# 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')

Method 2: treat the xml as plain text and do a simple textual -replace on it

# 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

Result in both cases:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to run a PowerShell script with verbose output?

Get Output of a PowerShell Script in a HTA

format output of powershell script

Change Powershell Output Property

How to get output from powershell script over ssh?

How to Use onOpen Trigger Script to Change Google Sheet Behavior?

How to Hide/Remove the header from output csv via Powershell script

Powershell script, write-output

How to create a variable from specific part of command output in powershell script

How do i use Get-clipboard output in a powershell script?

How to run node js script from powershell script and use the output of node js script in powershell script?

Appending to the output of Powershell script

How to send powershell script output to printer with formatting?

No Output Powershell Script

Output of powershell script not working correctly?

How to pass output from a PowerShell cmdlet to a script?

Running powershell script within python script, how to make python print the powershell output while it is running

How to change bash script output when the script is running?

Upgraded output for powershell script

How to redirect or retrieve the output of a telnet process ran by a Powershell script?

How to run SQL Server Agent Powershell script with output and exit code

Formatting the Powershell script object output?

How to use output variable value from one powershell script to another powershell script

How to change PowerShell result behavior based on XML & CSV?

How to show output of xcopy in powershell script while it's running

How to change an output formed by hashstables Powershell

Change output results in PowerShell

Powershell script output to Discord

How can the output of an existing PowerShell script be displayed?