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

GrandPappy00 1 Reputation point
2022-06-17T20:56:32.633+00:00

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" ?>
<NasNode>
<Items>
<Item GUID="{678ff350-53b5-4bfa-8f4c-093f2f11590a}" ComputerID="XXXXXX" ComputerIP="\XXXXXXXXX\" UserName="XXXX\XXXX" Password="WkBickBDQGRAYnJA" Hasname="false" bIsDefault="0" />
</Items>
</NasNode>

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
    2022-06-17T22:32:22.833+00:00

    Hi,

    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"  
    $xml.save("C:\QA\new.xml")  
    

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


  2. Limitless Technology 37,351 Reputation points
    2022-06-21T07:53:59.747+00:00

    Hello,

    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.
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_run_with_powershell?view=powershell-7.2

    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

    $xml.Save("C:\Users\userme\Desktop\PS-Scripts\new.xml")

    Here is a helpful page with information about Windows PowerShell

    https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/powershell

    -------------------------------------------------------------------------------------------------------------------------------------------------

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


  3. GrandPappy00 1 Reputation point
    2022-06-22T19:06:02.327+00:00

    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