Error Setting XML Node Properties

Welcome Forums General PowerShell Q&A Error Setting XML Node Properties

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
2 months ago.

  • Author
    Posts
  • #70757

    Participant
    Points: 0
    Rank: Member

    I'm trying to create a script to generate a temporary license file for an internal application. The requirements to generate the file are the hostname and mac address of the system that the application is on, which gets saved to an XML file. Traditionally we have manually opened the file and edited each time we want to generate a license, so I'm trying to simplify the process. Anyhow, this is what I've come up with for editing the file:

    $hostname = read-host "Enter the hostname of the ICA Server"
    $MACAddress1 = read-host "Enter the MAC Address of the ICA Server"
    $DefaultDays = read-host "Do you want to set the license for the default 30 days? Y/N"
        If($DefaultDays -eq 'N'){
        $LicenseDays = Read-Host "Enter number of days for temporary license (1-999)"}
        else
        {$LicenseDays = '30'}
    
    [xml]$inputFile = gc $FilePath
    $inputfile.LicenseRequest.Server.hostname = $hostname
    $inputfile.licenserequest.server.macaddr = $MACAddress
    $inputFile.LicenseRequest.License.Duration.value = $LicenseDays
    
    $inputFile.Save($FilePath)
    

    Right now when I run it it is showing this as the error:

    Cannot set "hostname" because only strings can be used as values to set XmlNode properties.
    At line:12 char:1
    + $inputfile.LicenseRequest.Server.hostname = $hostname
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], SetValueException
    + FullyQualifiedErrorId : XmlNodeSetShouldBeAString

    Cannot set "macaddr" because only strings can be used as values to set XmlNode properties.
    At line:13 char:1
    + $inputfile.licenserequest.server.macaddr = $MACAddress
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], SetValueException
    + FullyQualifiedErrorId : XmlNodeSetShouldBeAString

    I've run $hostname | get-member and it's showing as TypeName: System.String.

    For grins, I've also changed the variables to $hostname1 and $MACAddress1, then included $hostname = $hostname1 |out-string and $MACAddress = $MACAddress1 | out-string but I'm still seeing the same error. Any idea what I'm doing wrong?

    Edit: I just realized it might help to see my XML file...

  • #70774

    Participant
    Points: 0
    Rank: Member

    I figured it out! I needed quotes around the variables.

  • #111986

    Participant
    Points: 0
    Rank: Member

    Thanks Chris for the solution!!!,

    first I tried with node.Attributes['name'].innerText = 'Value' but also did not work.
    More strange, in console/debug mode (in ISE) the change worked, running the script in full kept giving the only strings are allowed message....

    ps: The XML has a small syntax error for hostname end tag and the Powershell script is missing the filepath but that's easy to fix 🙂

    grtz,

    Bart.

     

You must be logged in to reply to this topic.