使用 PowerShell 取消多个 SharePoint 工作流

发布于 2024-12-07 04:25:47 字数 1133 浏览 1 评论 0原文

如何取消 SharePoint (2010) 列表中所有正在运行的工作流?

我通过 technet 找到了这个脚本。

http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/d3913265-9712-4e61-9e38-1f9b78c8f718/

代码:

using (SPSite oSite = new SPSite("<your url>"))
{
    foreach (SPWeb oWeb in oSite.AllWebs)
    {
        oWeb.AllowUnsafeUpdates = true;

        // stop list workflows
        foreach (SPList list in oWeb.Lists)
        {
            foreach (SPListItem oItem in list.Items)
            {
                foreach (SPWorkflow workflow in oItem.Workflows)
                {
                    SPWorkflowManager.CancelWorkflow(workflow);
                }
            }
        }

        // stop site workflows
        foreach (SPWorkflow workflow in oWeb.Workflows)
        {
            SPWorkflowManager.CancelWorkflow(workflow);
        }

        oWeb.AllowUnsafeUpdates = false;
        oWeb.Dispose();
    }
}

非常感谢您的帮助。

How is it possible to cancel all running workflows in a SharePoint (2010) List?

I found this script via technet.

http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/d3913265-9712-4e61-9e38-1f9b78c8f718/

CODE:

using (SPSite oSite = new SPSite("<your url>"))
{
    foreach (SPWeb oWeb in oSite.AllWebs)
    {
        oWeb.AllowUnsafeUpdates = true;

        // stop list workflows
        foreach (SPList list in oWeb.Lists)
        {
            foreach (SPListItem oItem in list.Items)
            {
                foreach (SPWorkflow workflow in oItem.Workflows)
                {
                    SPWorkflowManager.CancelWorkflow(workflow);
                }
            }
        }

        // stop site workflows
        foreach (SPWorkflow workflow in oWeb.Workflows)
        {
            SPWorkflowManager.CancelWorkflow(workflow);
        }

        oWeb.AllowUnsafeUpdates = false;
        oWeb.Dispose();
    }
}

Many thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

靑春怀旧 2024-12-14 04:25:47

试一试这个。

我对其进行了修改,以便在特定网络上的特定列表上完成。

#Site URL
$web = Get-SPWeb "http://urlforsite.com";
$web.AllowUnsafeUpdates = $true;    

#List Name
$list = $web.Lists["ListName"];

# Iterate through all Items in List and all Workflows on Items.         
foreach ($item in $list.Items) {
foreach ($wf in $item.Workflows) {

#Cancel Workflows        
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);      
}
}
$web.Dispose();

对我来说效果很好。让我知道它是否适合您。

Give this one a shot.

I modified it so that it is done on a specific list on a specific web.

#Site URL
$web = Get-SPWeb "http://urlforsite.com";
$web.AllowUnsafeUpdates = $true;    

#List Name
$list = $web.Lists["ListName"];

# Iterate through all Items in List and all Workflows on Items.         
foreach ($item in $list.Items) {
foreach ($wf in $item.Workflows) {

#Cancel Workflows        
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);      
}
}
$web.Dispose();

Worked just fine for me. Let me know if it works for you.

北凤男飞 2024-12-14 04:25:47

这个脚本对我来说非常有用。我进行了修改,允许我取消一个列表中的所有工作流程或仅取消一个工作流程,并认为我也将其发布在这里:

    #Parameters
    param($listToCancel,$WfToCancel)

    #Site URL 
    $web = Get-SPWeb "http://mydomain.com"; 
    $web.AllowUnsafeUpdates = $true;     

    #List Name 
    $list = $web.Lists[$listToCancel]; 

    #Add wildcards to Wf variable
    $WildcardWfToCancel = "*"+$WfToCancel+"*";

    # Iterate through all Items in List and all Workflows on Items.          
    foreach ($item in $list.Items) { 
    foreach ($wf in $item.Workflows) { 

    #Test for workflow complete and match criteria
    if (($wf.ParentAssociation.InternalName -like $WildcardWfToCancel) -and ($wf.IsCompleted  -ne $true))        {

    #Show status and cancel Workflows
    write-Host $wf.ItemName -nonewline;
    write-host "     " -nonewline;
    write-host $wf.ParentAssociation.InternalName;
    Write-Host " Status " -nonewline;
    Write-host $wf.InternalState;     

    [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);       
    }
    } 
    } 
    $web.Dispose(); 

This script has been wickedly useful to me. I make a modification to allow me to cancel all the workflows in one list or just one workflow and thought I would post it here too:

    #Parameters
    param($listToCancel,$WfToCancel)

    #Site URL 
    $web = Get-SPWeb "http://mydomain.com"; 
    $web.AllowUnsafeUpdates = $true;     

    #List Name 
    $list = $web.Lists[$listToCancel]; 

    #Add wildcards to Wf variable
    $WildcardWfToCancel = "*"+$WfToCancel+"*";

    # Iterate through all Items in List and all Workflows on Items.          
    foreach ($item in $list.Items) { 
    foreach ($wf in $item.Workflows) { 

    #Test for workflow complete and match criteria
    if (($wf.ParentAssociation.InternalName -like $WildcardWfToCancel) -and ($wf.IsCompleted  -ne $true))        {

    #Show status and cancel Workflows
    write-Host $wf.ItemName -nonewline;
    write-host "     " -nonewline;
    write-host $wf.ParentAssociation.InternalName;
    Write-Host " Status " -nonewline;
    Write-host $wf.InternalState;     

    [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);       
    }
    } 
    } 
    $web.Dispose(); 
三生池水覆流年 2024-12-14 04:25:47

试试这个(C# 到 powershell 的简单“翻译”):

$site = Get-SPSite "<your url>";

$site.AllWebs | foreach {
  $web = $_;
  $web.AllowUnsafeUpdates = $true;

  # stop list workflows
  $web.Lists | foreach {
    $list = $_;
    $list.Items | foreach {
      $item = $_;
      $item.Workflows | foreach {
         $wf = $_;
         [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
      }
    }
  }

  # stop site workflows
  $web.Workflows | foreach {
    $wf = $_;
    [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
  }    

  $web.AllowUnsafeUpdates = $false;
  $web.Dispose();
}

$site.Dispose();

Try this (simple "translation" of C# to powershell):

$site = Get-SPSite "<your url>";

$site.AllWebs | foreach {
  $web = $_;
  $web.AllowUnsafeUpdates = $true;

  # stop list workflows
  $web.Lists | foreach {
    $list = $_;
    $list.Items | foreach {
      $item = $_;
      $item.Workflows | foreach {
         $wf = $_;
         [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
      }
    }
  }

  # stop site workflows
  $web.Workflows | foreach {
    $wf = $_;
    [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
  }    

  $web.AllowUnsafeUpdates = $false;
  $web.Dispose();
}

$site.Dispose();
安稳善良 2024-12-14 04:25:47

这是我修改后的脚本:

$web = Get-SPWeb "http://site/subsite";

  $web.allowUnsafeUpdates = 'true';

  # stop list workflows
  $web.Lists | foreach {
    $list = $_;
    $list.Items | foreach {
      $item = $_;
      $item.Workflows | foreach {
         $wf = $_;
         [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
      }
    }
  }

  $web.allowUnsafeUpdates = 'false';
  $web.Dispose();

这是我得到的错误(我使用的是德国本地化版本的服务器和共享点):

Fehler bei der Enumeration einer Auflistung: Collection was modified; enumeration operation may not execute..
Bei C:\stopwf.ps1:7 Zeichen:3
+    <<<< $web.Lists | foreach {
    + CategoryInfo          : InvalidOperation: (Microsoft.Share...on+SPEnumerator:SPEnumerator) [], RuntimeException
    + FullyQualifiedErrorId : BadEnumeration

运行脚本的用户是机器本身的管理员,站点收集管理员和sql管理员。我还用这个帐户创建了子网站,所以我不认为用户访问会成为问题。

我找到了这个答案(枚举集合时 PowerShell 和 C# 之间的差异)但我不知道如何为我使用这些信息?!

here is my modified script:

$web = Get-SPWeb "http://site/subsite";

  $web.allowUnsafeUpdates = 'true';

  # stop list workflows
  $web.Lists | foreach {
    $list = $_;
    $list.Items | foreach {
      $item = $_;
      $item.Workflows | foreach {
         $wf = $_;
         [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
      }
    }
  }

  $web.allowUnsafeUpdates = 'false';
  $web.Dispose();

an this is the error i get (i am using a german localized version of the server and sharepoint):

Fehler bei der Enumeration einer Auflistung: Collection was modified; enumeration operation may not execute..
Bei C:\stopwf.ps1:7 Zeichen:3
+    <<<< $web.Lists | foreach {
    + CategoryInfo          : InvalidOperation: (Microsoft.Share...on+SPEnumerator:SPEnumerator) [], RuntimeException
    + FullyQualifiedErrorId : BadEnumeration

the user runnig the script is admin on the machine it self, sitecollection admin and sql-admin. i also created the subsite with this account, so i don't think that useracces would be the problem.

I found this answer (Differences Between PowerShell and C# when Enumerating a Collection) but i don't get a clue how to use this information for me?!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文