0

I have a config XML , from which I am trying to read the information in Powershell script. The Powershell script takes input from user and then would need to navigate to each of the path. My configuration XML is as below:

<?xml version="1.0"?>
<Config>   
  <Action Name="Deploy">
            <CommandPath>path to navigate</CommandPath>
            <PackagePath>path of Application package</PackagePath>
        <ModelName>Name of model to be deployed</ModelName>
        <ServiceName>Name of Service</ServiceName>
            <DeploymentServer>
                <Server Name = 'Server1'></Server>
                <Server Name = 'Server2'></Server>
            </DeploymentServer>
  </Action>
  <Action Name="Update">
            <CommandPath>path to navigate</CommandPath>
            <PackagePath>path of Application package</PackagePath>
        <ModelName>Name of model to be updated</ModelName>
            <ServiceName>Name of Service</ServiceName>
            <DeploymentServer>
                <Server Name = 'Server1'></Server>                
            </DeploymentServer>
  </Action>     
</Config>

If the user inputs Deploy, the nodes under deploy need to be considered, if the user input is update, then the update path. The Powershell code should execute for each of the items under the DeploymentServer node for each of the action item. I have tried as below, but I am not able to get the correct node information in Powershell.

Set-ExecutionPolicy RemoteSigned

function RunConfiguration()
{
$XML = Select-Xml -Path $configFile -XPath 'Action' | Select-Object -ExpandProperty Node
do {
 $Answer = Read-Host -Prompt "Select required action ""$($XML.Action.Name -join '", "')"" "
} Until ($Answer -in ($XML.Action.Name))
Write-Host $XML.Action

$SelectedAnswer = $XML.Action | Where-Object {$_.Name -eq $UserOpt}
Write-Host $SelectedAnswer
$SelectedAnswer.DeploymentServer.Server | ForEach-Object {
    $ServerName = $_.Name
    Write-Host $ServerName
    Invoke-Command -ComputerName $ServerName -ScriptBlock {
        $Configuration = $args[0]   
        $Configuration.CommandPath
        $Configuration.PackagePath
        $Configuration.ModelName
        $Configuration.ServiceName
    } -ArgumentList $SelectedAnswer
   }
   }
}
}

$spAdminServiceName = "SPAdminV4"
$currentDir=(split-path $myinvocation.mycommand.path -parent)
$configFile = $currentDir
$configFile += "\DeployConfig.xml"
Execute-Commands $configFile

RunConfiguration()

Write-Host -f White "Press Enter key to exit..."
Read-Host

How to read the data? Thanks

1
  • 2
    Try changing -XPath 'Action' to -XPath '//Action' or -XPath './/Action' and see if it makes a difference. Commented Jul 13, 2020 at 13:51

1 Answer 1

1

Your XPath indeed needs //Action (see XPath Syntax), but most of all you need to be more consistent with the variable names.

At one point you capture the user input in variable $Answer, but next you use an undefined variable $UserOpt..

Try:

$configfile = 'D:\Test\config.xml'
# get an array of 'Action' XmlElements
$actionNodes = (Select-Xml -Path $configFile -XPath '//Action').Node

do {
    $Answer = Read-Host -Prompt "Select required action '$($actionNodes.Name -join "', '")'"
} Until ($Answer -in ($actionNodes.Name))

$selectedNode = $actionNodes | Where-Object {$_.Name -eq $Answer}
Write-Host $selectedNode.Name

$selectedNode.DeploymentServer.Server | ForEach-Object {
    $ServerName = $_.Name
    Write-Host $ServerName

    # do your Invoke-Command on the $ServerName here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.