1

I need to pick all the nodes from below xml where application name = "secureApps" via powerShell.

I am using below script but receiving error.

$xml = [xml](Get-Content AppsConfiguration.xml)

foreach ($node in $xml.SelectNodes('/configurations/application/app')| Where 
$xml.configurations.application.name -eq "SecureApps" ) {

    $app_name = $node.app_name
    $app_path = $node.app_path

    # Create directory if not found
    if(-Not (Test-Path -Path $app_path)) {
    New-Item $app_path -Type Directory
    }

    # Convert directory to virtual directory
    New-WebVirtualDirectory -Site $secure_website_name.website_name -Name 
    $app_name -PhysicalPath $app_path

    $site_path = $secure_iis_site_location + $app_path
    ConvertTo-WebApplication -PSPath $site_path
}

The problem is in the line:

foreach ($node in $xml.SelectNodes('/configurations/application/app')| Where $xml.configurations.application.name -eq "SecureApps" ) {

The XML i am using is as below:

<?xml version="1.0"?>
<configurations>
<application name="SecureApps">
    <app id= "1">
        <app_name>atl</app_name>
        <app_path>D:\inetpub\secure\admin\atl</app_path>
    </app>
    <app id= "2">
        <app_name>corporate</app_name>
        <app_path>D:\inetpub\secure\admin\corporate</app_path>
    </app>
    <app id= "3">
        <app_name>f2l</app_name>
        <app_path>D:\inetpub\secure\admin\f2l</app_path>
    </app>
    <app id= "4">
        <app_name>GPModality</app_name>
        <app_path>D:\inetpub\secure\admin\GPModality</app_path>
    </app>
</application>
<application name="webApps">
    <app id= "1">
        <app_name>locations</app_name>
        <app_path>D:\inetpub\web\locations</app_path>
    </app>
</application>

1
  • $xml.SelectNodes('/configurations/application[@name="SecureApps"]/app') Commented May 28, 2018 at 14:34

1 Answer 1

1

You have several problems here.

First your where condition does not work this way. If you use where without a script block, you have only three parameters: A property name from the current pipeline object, comparison operator and a fixed value.

What you use as first parameter in your where condition is an expression that delivers an object array containing the strings "SecureApps" and "webApps".

PowerShell expects a string containing a property name as first positional parameter of the where command, cannot convert the array to such a string and thus fails with an error message.

Trying to correct this error while keeping the simple syntax you would have to find a property in the pipeline object that you can compare against.

Your pipeline object is an "app" XML node, so you could try ParentNode.Name -eq ... but dotted properties also don't work with the simple where syntax, only plain property names.

So, to fix this particular problem you could fall back to the script block syntax, like this

... | Where { $_.ParentNode.Name -eq "SecureApps" }

This is not optimal though, because you loop through every app node and only then check the parent application node. Better to filter for the correct application node first and then go through all the app subnodes:

foreach ($node in ($xml.configurations.application | Where name -eq "SecureApps").app ) {

There is also the possibility that Ansgar mentioned in his comment, to use the fact that .NETs SelectNodes uses XPath expressions, so you can use an XPath condition instead of the PowerShell mechanism:

foreach ($node in $xml.SelectNodes("/configurations/application[@name='SecureApps']/app")) {
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.