Help writing a script to change some of a file but not all.

GrandPappy00 1 Reputation point

We have an xml file that we need edited. Here is the body of the xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Item GUID="{678ff350-53b5-4bfa-8f4c-093f2f11590a}" ComputerID="XXXXXX" ComputerIP="\XXXXXXXXX\" UserName="XXXX\XXXX" Password="WkBickBDQGRAYnJA" Hasname="false" bIsDefault="0" />

We have a script that will change it to what we need but we want to leave {678ff350-53b5-4bfa-8f4c-093f2f11590a} this alone. We want the script to change the username and password but leave the GUID number alone. I am no guru by any means so please be nice. Any help would be greatly appreciated.

The tag I added doesnt really pertain to this but it needed something.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,627 questions
No comments
{count} votes

3 answers

Sort by: Most helpful
  1. DaveK 1,736 Reputation points


    You should be able to do something using the XML type accelerator. I'm sure there will be other ways but this is the route I've taken when I've needed to do a similar task:

    [xml]$xml = Get-Content C:\QA\data.xml  
    $xml.NasNode.Items.Item.Username = "New\User"  
    $xml.NasNode.Items.Item.Password = "NewPassword"  

    In my example data.xml is just a file with the example you gave.

  2. Limitless Technology 37,351 Reputation points


    Thanks for posting the query here and we are happy to help you.

    To load and modify the data from an existing XML, you can use the PowerShell script.

    Please refer following link to execute a PS script.

    Following script works fine. Kindly update the details for each field.

    Create xml doc - assumes your xml is in below locationl

    $xml = xml

    Update new element for <add>

    $xml.NasNode.Items.Item.Username = "User1"
    $xml.NasNode.Items.Item.ComputerID= "IDSample"

    Save to file


    Here is a helpful page with information about Windows PowerShell


    --If the reply is helpful, please Upvote and Accept as answer.--

  3. GrandPappy00 1 Reputation point

    I say we but it is actually the tech who is writing the script ended up using Find and Replace commands to looked for the things we needed replaced.

    $find = 'XXXXXXXXX'
    $replace = 'XXXXXXX'

    No comments