Powershell:将参数传递给存储在变量中的函数 [英] Powershell: passing parameters to functions stored in variables

查看:64
本文介绍了Powershell:将参数传递给存储在变量中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获得一个在作业内部使用函数的简单工作示例.我已设法将我的函数传递到用于我的工作的脚本块中,但我似乎无法获取该函数的参数.

#并发$Logx ={参数([参数(ValueFromPipeline=$true)]$味精)写主机 ("OUT:"+$msg)}# 执行从这里开始类$colors = @("红色","蓝色","绿色")$colors |%{$scriptBlock ={调用表达式 -Command $args[1]开始睡眠 3}写主机处理:" $_开始作业 -scriptblock $scriptBlock -args $_, $Logx}找工作while(Get-Job -State "Running"){写主机正在运行..."开始睡眠 2}# 输出找工作|接工作# 清理工作删除作业 *

输出如下:

<块引用>

处理:红色

Id Name State HasMoreData Location 命令-- -- ----- ----------- -------- -------175 Job175 运行真正的本地主机......加工:蓝色177 Job177 运行真正的本地主机......加工:绿色179 Job179 运行真正的本地主机......179 Job179 运行真正的本地主机......177 Job177 运行真正的本地主机......175 Job175 运行真正的本地主机......跑步...跑步...出去:出去:出去:

因此,正如输出中的 OUT: x3 所证明的那样,我的函数被调用了,但我没有找到任何允许我获取函数参数的语法.想法?

注意以下 Shawn 的观察和我的回答,我尝试使用函数作为变量,因为使用传统函数似乎不起作用.如果有办法让它工作,我会很高兴不必将我的函数作为变量传递.

解决方案

答案是使用Start-Job的initializationscript参数.如果您在一个块中定义所有函数并传递该块,则它们变得可用.

在这篇文章中找到了解决方案:

我如何开始一个我刚刚定义的函数的工作?

这是我之前的示例,现在可以使用了:

#并发$func = {函数日志{参数([参数(ValueFromPipeline=$true)]$味精)写主机 ("OUT:"+$msg)}}# 执行从这里开始类$colors = @("红色","蓝色","绿色")$colors |%{$scriptBlock ={Logx $args[0]开始睡眠 9}写主机处理:" $_开始作业 -InitializationScript $func -scriptblock $scriptBlock -args $_}找工作while(Get-Job -State "Running"){写主机正在运行..."开始睡眠 2}# 输出找工作|接工作# 清理工作删除作业 *

I'm trying to get a simple working example of using functions inside of jobs. I've managed to pass my function into the scriptblock used for my job, but I can't seem to get parameters to the function.

# concurrency
$Logx = 
{
    param(
    [parameter(ValueFromPipeline=$true)]
    $msg
    )    
    Write-Host ("OUT:"+$msg)
}

# Execution starts here
cls
$colors = @("red","blue","green")
$colors | %{
    $scriptBlock = 
    {   
        Invoke-Expression -Command $args[1]
        Start-Sleep 3        
    } 

    Write-Host "Processing: " $_
    Start-Job -scriptblock $scriptBlock -args $_, $Logx
}

Get-Job

while(Get-Job -State "Running")
{
    write-host "Running..."
    Start-Sleep 2
}

# Output
Get-Job | Receive-Job

# Cleanup jobs
Remove-Job * 

Here's the output:

Processing:  red

Id              Name            State      HasMoreData     Location             Command  
--              ----            -----      -----------     --------             -------  
175             Job175          Running    True            localhost               ...   
Processing:  blue
177             Job177          Running    True            localhost               ...   
Processing:  green
179             Job179          Running    True            localhost               ...   
179             Job179          Running    True            localhost               ...   
177             Job177          Running    True            localhost               ...   
175             Job175          Running    True            localhost               ...   
Running...
Running...
OUT:
OUT:
OUT:

So as evidenced by the OUT: x3 in the output my function is getting called, but I haven't found any syntax that allows me to get the parameter to the function. Thoughts?

EDIT:

Note in Shawn's observation below and my response I tried using functions as variables because using a traditional function does not seem to work. If there is a way to get that working I'd be more than happy to not have to pass my functions around as variables.

解决方案

The answer is to use the initializationscript parameter of Start-Job. If you define all your functions in a block and pass the block they become available.

Solution was found in this post:

How do I Start a job of a function i just defined?

Here is my example from before, now working:

# concurrency
$func = {
    function Logx 
    {
        param(
        [parameter(ValueFromPipeline=$true)]
        $msg
        )    
        Write-Host ("OUT:"+$msg)
    }
}

# Execution starts here
cls
$colors = @("red","blue","green")
$colors | %{
    $scriptBlock = 
    {   
        Logx $args[0]
        Start-Sleep 9        
    } 

    Write-Host "Processing: " $_
    Start-Job -InitializationScript $func -scriptblock $scriptBlock -args $_
}

Get-Job

while(Get-Job -State "Running")
{
    write-host "Running..."
    Start-Sleep 2
}

# Output
Get-Job | Receive-Job

# Cleanup jobs
Remove-Job * 

这篇关于Powershell:将参数传递给存储在变量中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆