[英]How to pass argument to a function from a pipe output of another using powershell
I have written a function called Wait-UntilJobFailOrSuccess . 我已经编写了一个名为Wait-UntilJobFailOrSuccess的函数。 This takes the output from Get-Job using pipe command line. 这使用管道命令行从Get-Job获取输出。 For example. 例如。
Get-Job|Remove-Job
The same way I want to do for my function. 我想为自己的功能做同样的事情。 For example 例如
Get-Job | Wait-UntilJobFailOrSuccess
I also attached Wait-UntilJobFailOrSuccess below. 我还在下面附加了Wait-UntilJobFailOrSuccess。 Please let us know. 请告诉我们。 Do anyone has a solution for this. 有谁对此有解决方案。
Function Wait-UntilJobFailOrSuccess
{
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[System.Object[]]$jobs
)
while ($true) {
if ('Failed' -in $jobs.State) {
$jobs | Stop-Job
Write-Host "Aborting Jobs ${result}"
break
}
Start-Sleep -Milliseconds 500
}
foreach ($job in $jobs) {
if ($job.State -eq 'Failed') {
Write-Host ($job.ChildJobs[0].JobStateInfo.Reason.Message) -ForegroundColor Red
} else {
Write-Host (Receive-Job $job) -ForegroundColor Green
}
}
$jobs|remove-Job
}
Solution for what exactly? 解决方案究竟是什么? You haven't stated a problem. 您还没有提出问题。
Anyway, in your code you name you your parameter "Jobs" then make it as [ValueFromPipelineByPropertyName]
. 无论如何,在您的代码中将您的参数“ Jobs”命名为[ValueFromPipelineByPropertyName]
。 Job objects don't have a Jobs
property so this will not work. 作业对象没有Jobs
属性,因此将无法使用。 Consider a separate parameter Id
for this. 为此考虑一个单独的参数Id
。 Also, rather than typing the parameter as [object[]]
, type it as [System.Management.Automation.Job[]]
which is the type of the job object. 另外,不要将参数键入为[object[]]
,而是将其键入为[System.Management.Automation.Job[]]
,这是作业对象的类型。
You should have logic in the process
block to accumulate all of the Job
objects then move the loops to the end
block once all of the jobs have been gathered. 您应该在process
块中具有逻辑以累积所有Job
对象,然后在收集end
所有作业后将循环移至end
块。
I've reformatted your code and cleaned it up a bit, but I still can't figure out what you're actually trying to do: 我已经重新格式化了您的代码并对其进行了一些清理,但是我仍然无法弄清您实际上要做什么:
Function Wait-UntilJobFailOrSuccess
{
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[System.Management.Automation.Job[]]
$jobs
)
begin
{
$joblist = @()
}
process
{
$joblist += $jobs
}
end
{
foreach ($job in $joblist)
{
if ($job.State -eq 'Failed')
{
Stop-Job -Job $job
Write-Host "Aborting Job $(job.name)"
break
}
}
Start-Sleep -Milliseconds 500
foreach ($job in $jobslist)
{
if ($job.State -eq 'Failed')
{
Write-Host ($job.ChildJobs[0].JobStateInfo.Reason.Message) -ForegroundColor Red
}
else
{
Write-Host (Receive-Job $job) -ForegroundColor Green
}
}
$joblist | Remove-Job -Force # Is this what you really want to do?
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.