Question

I'm trying to extract static routes from all AD computer using the Get-NetRoute cmdlet. It works well when run outside a workflow, but as soon as I try to run the same code within a workflow it fails with the following exception:

System.Management.Automation.ParameterBindingException: Parameter set cannot be resolved using the specified named parameters.

I could trace this back to the Where-Object "?" filter. The "? {}" section in commented line of code makes it fail. Without that filter it works perfectly.

Code is here:

cd C:\Users\Public\Documents

workflow Get-StaticRoutes  {
    # Change the error action to 'stop' to get try/catch working
    # Get-NetRoute -Protocol NetMgmt -AddressFamily IPv4 | ? { $_.DestinationPrefix -ne "0.0.0.0/0" } | % {
    Get-NetRoute -Protocol NetMgmt -AddressFamily IPv4 | % {
        [PSCustomObject] @{
            ComputerName      = $env:COMPUTERNAME
            InterfaceName     = $_.InterfaceAlias
            InterfaceIndex    = $_.InterfaceIndex
            DestinationPrefix = $_.DestinationPrefix
            NextHop           = $_.NextHop
            Comment           = ""
        }
    }
}

# Get all computers from AD
$computers = Get-ADComputer -Filter * | % { $_.Name }

# Retrieve IP config
Get-StaticRoutes -PSComputerName $computers | Export-Csv ".\StaticRoutes.csv" -NoTypeInformation

I could filter after the workflow to fix this problem, but I would like to understand what I do wrong as this ParameterBindingException is rather obscure.

Thanks,

Olivier.

Was it helpful?

Solution

To run commands or expressions in a workflow that are valid in Windows PowerShell, but not valid in workflows, run the commands in an inlineScript activity. You can use also an inlineScript activity to run Windows PowerShell scripts (.ps1 files) in a workflow.

Try this ( not tested )

workflow Get-StaticRoutes  
{   
   inlinescript { Get-NetRoute -Protocol NetMgmt -AddressFamily IPv4 |
                     ? { $_.DestinationPrefix -ne "0.0.0.0/0" } | 
                % {
                    [PSCustomObject] @{
                                        ComputerName      = $env:COMPUTERNAME
                                        InterfaceName     = $_.InterfaceAlias
                                        InterfaceIndex    = $_.InterfaceIndex
                                        DestinationPrefix = $_.DestinationPrefix
                                        NextHop           = $_.NextHop
                                        Comment           = ""
                                       }
                   }
                }
}

side note:

  • $env:computername outside the inlinescipt activity resolve to local computer name. Inside the inlinescipt activity resolve to the remote computer name.

    • the object returned by a workflow is a serialized object and not the object created in the inlinescript activity or workflow process ( this means, in simple terms, that you can't have object method but only the properties of the object )

OTHER TIPS

Remember that with Workflows you need to use named parameters. When you run something like this:

$a = $b | ?{$_.Name -eq "John"}

You're really running this:

$a = $b | where-object -FilterScript {$_.Name -eq "John"}

The latter works fine in a workflow without using those pesky inlinescripts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top