【问题标题】:How to pipe the result of a foreach loop into a csv file with PowerShell如何使用 PowerShell 将 foreach 循环的结果通过管道传输到 csv 文件中
【发布时间】:2014-02-05 10:53:40
【问题描述】:

我有以下语法问题,我仍然不确定这是否可能。

An empty pipe element is not allowed.

脚本是

$web = get-spweb https://xx.com/sites/billing
$list = $web.Lists["Bill Cycles"]
foreach($wf in $list.WorkflowAssociations)
{
   if ($wf.Name -like "*2013*")
   {  
      foreach($listitem in $list.Items)
      {
          foreach($Workflow in $listitem.Workflows)
          {
             if($wf.InternalStatus -ne "Completed")
             {
                if($Workflow.AssociationId -eq $wf.Id)
                {
                    New-Object psobject -Property @{
                            "InternalStatus" = $wf.InternalStatus
                            "WFName" = $wf.Name
                            "ListItemName" = $listitem.Name
                            "Url" = $listitem.Url
                            "Days" = ((Get-Date) - $Workflow.Created).Days
                    }
                }
             }
          }
      }
    }
} | Select-Object InternalStatus, WFName, ListItemName, Url, Days | Export-CSV $output -Delimiter ',' -NoTypeInformation

【问题讨论】:

    标签: sharepoint powershell


    【解决方案1】:

    您可以使用子表达式将 foreach 循环输出到管道:

    $web = get-spweb https://xx.com/sites/billing
    $list = $web.Lists["Bill Cycles"]
    $(foreach($wf in $list.WorkflowAssociations)
    {
       if ($wf.Name -like "*2013*")
       {  
          foreach($listitem in $list.Items)
          {
              foreach($Workflow in $listitem.Workflows)
              {
                 if($wf.InternalStatus -ne "Completed")
                 {
                    if($Workflow.AssociationId -eq $wf.Id)
                    {
                        New-Object psobject -Property @{
                            "InternalStatus" = $wf.InternalStatus
                            "WFName" = $wf.Name
                            "ListItemName" = $listitem.Name
                            "Url" = $listitem.Url
                            "Days" = ((Get-Date) - $Workflow.Created).Days
                        }
                    }
                 }
              }
          }
        }
    }) | Select-Object InternalStatus, WFName, ListItemName, Url, Days | Export-CSV $output -Delimiter ',' -NoTypeInformation
    

    您还可以使用脚本块调用(将循环包装在 &{} 而不是 $() 中)

    【讨论】:

      【解决方案2】:

      foreach 循环不会输出到管道,因此Export-CSV 可能没有任何输入。

      尝试括号中的 wrapping the iteration 的 WorkflowAssociations。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-06
        • 2011-01-04
        • 1970-01-01
        • 2014-07-08
        • 1970-01-01
        • 2019-01-24
        相关资源
        最近更新 更多