2014-04-01 26 views
1

我试图使用Get-NetRoute cmdlet从所有AD计算机中提取静态路由。当工作流之外运行,但只要我尝试它失败,出现以下异常的工作流程中运行相同的代码,它工作得很好:“?”当在PowerShell中作为工作流运行时,Where-Object子句失败

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

我可以追溯这一回位置对象过滤。注释代码行中的“?{}”部分使其失败。没有这个过滤器,它完美的作品

代码是在这里:

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默默无闻。

谢谢,

奥利维尔。

回答

0

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

试试这个(未测试)

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   = "" 
             } 
        } 
       } 
} 

侧面说明:

  • $env:computername的inlinescipt活动的决心外本地 计算机名称。 inlinescipt活动内部解析为远程 计算机名称。

    • 由工作流返回的对象是一个序列化的对象,而不是对象在将InlineScript活动或工作流过程(这意味着创建,简单来说,就可以没有对象的方法,但唯一的属性对象)
+0

谢谢,将块放入inlinecript修复它。它只是失败时,PSComputerName是我运行该脚本的计算机。获取“Receive-Job:连接到远程服务器GB1IVJUMPBOX02失败,并显示以下错误消息:访问被拒绝”,但这是另一个问题。 –

+0

最后一个问题解决了,我不得不以管理员身份运行PowerShell,以免在自我身上发生错误。感谢您的帮助CB! –

+0

@Oivivier很高兴帮助! –

1

请记住,工作流,你需要使用命名参数。 当您运行是这样的:

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

你真的运行以下命令:在工作流程

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

后工作正常,而不使用那些讨厌的inlinescripts。

相关问题