Updating values in XML

This topic contains 7 replies, has 3 voices, and was last updated by  kishor 2 months, 1 week ago.

  • Author
    Posts
  • #72367

    kishor
    Participant

    Hi,
    I am trying to update a value in XML file. I can get to that property as below, please help;

    PS C:\> ($xml2.unattend.settings.component).registeredorganization
    xxxx Group Headquarters, IT Services

    But when I try to update the value of that property with below command it gives me error ;

    PS C:\> $XMLTest= ($xml2.unattend.settings.component).registeredorganization
    PS C:\> $XMLTest.RegisteredOrganization = $registeredOrganization
    The property 'RegisteredOrganization' cannot be found on this object. Verify that the property exists and can be set.
    At line:1 char:1

    Thanks,
    Kishor

  • #72370

    Curtis Smith
    Participant

    There is not a sample of your XML, but looking at what you have, you have already included "registeredorganization" in your variable and there is probably not a registeredorganization property of registeredorganization like you are trying to set

    $XMLTest= ($xml2.unattend.settings.component).registeredorganization

    So putting RegisteredOrganization you are basically looking for this in your xmls
    ($xml2.unattend.settings.component).registeredorganization.RegisteredOrganization = $registeredOrganization

    You likely just need to drop the second RegisteredOrganization

    PS C:\> $XMLTest= ($xml2.unattend.settings.component).registeredorganization
    PS C:\> $XMLTest = $registeredOrganization
    
  • #72382

    kishor
    Participant

    Hi Curtis,
    I found the xml I am working with has a namespace.What do I need to do in this case?
    Below is the snap of my XML file, I need to update the value for 'RegisteredOrganization';

    Thanks,
    Kishor

  • #72440

    Curtis Smith
    Participant

    Hey Kishor, you will likely have to use a gist to post your xml. This forum tends to strip it out.

  • #72442

    Monty
    Participant

    mistake post. Please ignore

  • #72469

    kishor
    Participant

    Hi Curtis,
    Below is link for my xml;

    Thanks,
    Kishor

  • #72499

    Curtis Smith
    Participant

    There are some issues with your xml which I will assume are type-os in the provided example and not in your actual file. The issue you are having with setting your value is that you have multiple components that match.

    IE.
    $xml2.unattend.settings.component will match the first component in the file where name is blank, and it will match the second component in the file where name is "Microsoft-Windows-". The result is that at this point you have a collection of object that .registeredorganization is looking in, and you cannot set the value like this across a collection.

    What you need to do is select the specific component you need so that you have just the single object when you are trying to set the registeredorganization value.

    Results

    xxxx Group Headquarters, IT Services
    PowerShell.org
    • #72502

      kishor
      Participant

      Thanks a lot Curtis! your solution has solved my problem. Now I am able to get to the value without worrying about the namespaces involved in the XML file.

      Thanks,
      Kishor

You must be logged in to reply to this topic.