Author Posts

May 16, 2017 at 1:45 pm

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

May 16, 2017 at 2:21 pm

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