2016-09-27 118 views
1

我正尝试读取文件夹中的文本文件并通过两个工作流程读取内容。是否可以在另一个工作流程中调用工作流程?在工作流程中调用工作流程

workflow ReadFilesFromFolder 
{ 
    $folderPath = 'C:\Users\MyName\MyFolder' 

    Get-ChildItem $folderPath -Filter *.txt | 
     Foreach-Object { 
      ReadTextFile $_.FullName 
     } 
} 

workflow ReadTextFile 
{ 
    param($path) 

    $lines = Get-Content $path 
    ForEach -Parallel ($line in $lines) 
    {   
      $line 
    } 
} 

PS:Foreach并行接受工作流程。

回答

2

您必须明确指定参数,其名称。它也像你不能Foreach-Object命令内调用工作流 - 然而foreach似乎工作:

workflow ReadFilesFromFolder 
{ 
    $folderPath = 'C:\Users\MyName\MyFolder' 

    $items = Get-ChildItem $folderPath -Filter *.txt 
    foreach($item in $items) 
    { 
     ReadTextFile -path $item.FullName 
    } 
} 

workflow ReadTextFile 
{ 
    param($path) 

    $lines = Get-Content $path 
    ForEach -Parallel ($line in $lines) 
    {   
     $line 
    } 
} 
+0

它抛出错误ReadTextFile不被识别为cmdlet。 – Kurkula

+1

你说得对。看起来你不能在'Foreac-Object' cmdlet中调用它。然而它在foreach循环中起作用。我会编辑我的答案。 –

+0

我猜想有一些问题。 Get-Item:找不到接受参数 'System.Management.Automation.PSDataCollection'1 [System.Management.Automation.PSObject]'的位置参数。 在PurgeStreamsFromFolder:3字符:3 + + CategoryInfo:InvalidArgument:(:) [获取-项目],ParameterBindingException + FullyQualifiedErrorId:PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetItemCommand + PSComputerName:[本地主机] – Kurkula