1
votes

Suddenly this code ain't working anymore. I'm trying to google this but I can't seem to find any solution how to fix this.

I can't just do

[xml]($deploymentXml.configuration.services).AppendChild($newService)

or

$deploymentServicesNode = [xml]$deploymentXml.configuration.services
$deploymentServicesNode.AppendChild($newService)

Does anyone know what's going on and why this ain't working anymore?


deployment.xml

<?xml version="1.0" encoding="utf-8"?>
<configuration description="Services deployment server">
    <services>
    </services>
</configuration>

Method invocation failed because [System.String] does not contain a method named 'AppendChild'. At F:\USB\host.ps1:153 char:9 + $deploymentXml.configuration.services.AppendChild($service ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

The part of host.ps1

#Location of the file
$deploymentFile = $PSScriptRoot + "\USB\Configuration\Deployment.xml";
$deploymentXml = [xml](Get-Content $deploymentFile);

# check if we got a file, if not do nothing.
if ($deploymentXml) {
    Write-Verbose "deployment xml: $deploymentFile"

    foreach($service in $Services) {
        # Create the new service
        $newService = $deploymentXml.CreateElement("service");
        $deploymentXml.configuration.services.AppendChild($newService)

        Write-Verbose "Adding service: $service"

        $newService.SetAttribute("name", $service);   
    }

    $deploymentXml.Save($deploymentFile);
}
1
That fixed it! Do you got any documentation about this? I had no idea that I should see it as an array.StuiterSlurf

1 Answers

1
votes

Before getting your XML content, try adding this:

$deploymentFile = $PSScriptRoot + "\USB\Configuration\Deployment.xml";
[System.Xml.XmlDocument]$deploymentXml = New-Object System.Xml.XmlDocument
$deploymentXml = [xml](Get-Content $deploymentFile);

I've ran into some issues with the type not getting set in some environments, i.e.- SQL job PowerShell step.