简体   繁体   English

如何使用Powershell将参数从另一个函数的管道输出传递给函数

[英]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.

相关问题 如何将函数输出传递给powershell中的另一个函数? - How to pass the function output to another function in powershell? 如何将一个 PowerShell 函数的“参数行”传递给另一个函数? - How to pass the 'argument-line' of one PowerShell function to another? 如何从PERL将参数传递给Powershell并保留输出? - How to pass argument to Powershell from PERL and retain the output? 如何从 foreach 到 powershell - How to pipe output from foreach, in powershell Powershell函数中的参数传递 - argument pass in powershell function 如何使用PowerShell将一个函数变量数据传递给另一函数? - How to pass one function variable data into another function using PowerShell? 将参数从另一个PowerShell脚本传递给一个PowerShell脚本 - To pass argument to one powershell script from another powershell script Win10 - 如何从一个命令中将 pipe output 作为另一个命令的参数来检查文件 hash? - Win10 - How to pipe the output from one command as an argument of another to check file hash? 如何基于 function 在 Powershell 中声明一个变量,以便将其作为参数传递给另一个 function? - How to declare a variable in Powershell based on a function in order to pass it as an argument in another function? 使用 PowerShell 将输出通过管道传输到剪贴板 - Pipe output to the clipboard using PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM