在PowerShell中作为工作流运行时,Where Object子句失败

在PowerShell中作为工作流运行时,Where Object子句失败,powershell,workflow,Powershell,Workflow,我正在尝试使用Get-NetRoute cmdlet从所有AD计算机提取静态路由。当在工作流外部运行时,它工作得很好,但一旦我尝试在工作流中运行相同的代码,它就会失败,出现以下异常: System.Management.Automation.ParameterBindingException: Parameter set cannot be resolved using the specified named parameters. 我可以追溯到Where对象“?”过滤器。注释代码行中的“{}

我正在尝试使用Get-NetRoute cmdlet从所有AD计算机提取静态路由。当在工作流外部运行时,它工作得很好,但一旦我尝试在工作流中运行相同的代码,它就会失败,出现以下异常:

System.Management.Automation.ParameterBindingException: Parameter set cannot be resolved using the specified named parameters.
我可以追溯到Where对象“?”过滤器。注释代码行中的“{}”部分使其失败。没有那个过滤器,它工作得很好

代码如下:

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
我可以在工作流之后进行过滤以解决此问题,但我想了解我做错了什么,因为此ParameterBindingException相当模糊

谢谢


Olivier。

要在工作流中运行在Windows PowerShell中有效但在工作流中无效的命令或表达式,请在inlineScript活动中运行这些命令。您还可以使用inlineScript活动在工作流中运行Windows PowerShell脚本(.ps1文件)

试试这个(未测试)

旁注:

  • $env:computername
    在inlinescipt活动外部解析为本地 计算机名。在inlinescipt活动内解析到远程 计算机名

    • 工作流返回的对象是序列化对象,而不是在inlinescript活动或工作流过程中创建的对象(简单地说,这意味着您不能使用对象方法,只能使用对象的属性)

请记住,对于工作流,您需要使用命名参数。 当您运行类似这样的操作时:

$a = $b | ?{$_.Name -eq "John"}
你真的在运行这个:

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

后者在工作流中工作正常,无需使用那些讨厌的inlinescript。

谢谢,将块放入inlinescript可以修复它。当PSComputerName是我运行脚本的计算机时,它就会失败。获取“接收作业:连接到远程服务器GB1IVJUMPBOX02失败,错误消息如下:访问被拒绝”,但这是另一个问题。上一个问题已修复,我必须以管理员身份运行PowerShell才能在self上不获取该错误。谢谢你的帮助@奥利维尔很乐意帮忙!
$a = $b | where-object -FilterScript {$_.Name -eq "John"}