[英]Where-Object clause fails when run as a workflow in PowerShell
I'm trying to extract static routes from all AD computer using the Get-NetRoute cmdlet. 我正在尝试使用Get-NetRoute cmdlet从所有AD计算机提取静态路由。 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 "?" 我可以追溯到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. 我可以在工作流之后进行过滤以解决此问题,但是我想了解我做错了什么,因为ParameterBindingException非常模糊。
Thanks, 谢谢,
Olivier. 奥利维尔。
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. 要在工作流中运行在Windows PowerShell中有效但在工作流中无效的命令或表达式,请在inlineScript活动中运行命令。 You can use also an inlineScript activity to run Windows PowerShell scripts (.ps1 files) in a workflow.
您还可以使用inlineScript活动在工作流中运行Windows PowerShell脚本(.ps1文件)。
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. 内联活动之外的
$env:computername
解析为本地计算机名。 Inside the inlinescipt activity resolve to the remote computer name. 在inlinescipt活动中,解析为远程计算机名称。
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. 后者在工作流程中可以很好地工作,而无需使用那些讨厌的内联脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.