1

I have a Powershell that runs 2 workflows. The first workflow is on every list item and the second simply sends an email to let people know that the Powershell ran successfully.

I'm getting errors when I try to run the single workflow

PowerShell

  $web = Get-SPWeb -Identity "http://portal.com/sites/it" 
$manager = $web.Site.WorkFlowManager   
$list = $web.Lists["Certificate Tracking"]

$assoc = $list.WorkflowAssociations.GetAssociationByName("Certificate Notification","en-US")
$assoc.AllowAsyncManualStart = $true
$assoc.AllowManual = $true

$emails = $list.WorkflowAssociations.GetAssociationByName("Status Update","en-US")
$emails.AllowAsyncManualStart = $true
$emails.AllowManual = $true

$view = $list.Views["All Items"] #All Items
$items = $list.GetItems($view)  

$data = $assoc.AssociationData
$emData = $emails.AssociationData

$count = 0

foreach ($item in $items) {
 $wf = $manager.StartWorkFlow($item,$assoc,$data) 
}
$em = $manager.StartWorkFlow($item,$emails,$emData)

$web.Dispose()

Error

 Exception calling "StartWorkflow" with "3" argument(s): ""
    At C:\cert.ps1:26 char: 29
    + $em = $manager.StartWorkFlow <<<< ($item,$emails,$data)
        + CategoryInfo           : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId  :  DotNetMethodException
0

Try using this code

$web = Get-SPWeb -Identity "http://portal.com/sites/it" 
$manager = $web.Site.WorkFlowManager   
$list = $web.Lists["Certificate Tracking"]

$assoc = $list.WorkflowAssociations.GetAssociationByName("Certificate Notification","en-US")
$assoc.AllowAsyncManualStart = $true
$assoc.AllowManual = $true

$emails = $list.WorkflowAssociations.GetAssociationByName("Status Update","en-US")
$emails.AllowAsyncManualStart = $true
$emails.AllowManual = $true

$view = $list.Views["All Items"] #All Items
$items = $list.GetItems($view)  

$data = $assoc.AssociationData
$emData = $emails.AssociationData

$count = 0

foreach ($item in $items) {

    $wf = $manager.StartWorkFlow($item,$assoc,$data) 
    $em = $manager.StartWorkFlow($item,$emails,$emData)

}


$web.Dispose()
  • I don't want the email to kick off for every list item. Only once. – PT_C Oct 27 '15 at 12:57
  • Then you need to have one more look on email list. – Kaushal Khamar Oct 28 '15 at 4:31
  • What does that mean? – PT_C Oct 28 '15 at 13:25
0

You are getting error for this line $em = $manager.StartWorkFlow($item,$emails,$emData)

This is because scope for parameter $item is inside for loop only. And you are passing it outside for loop where it is undefined.

Either you can call workflow inside for loop

foreach ($item in $items) {
    $wf = $manager.StartWorkFlow($item,$assoc,$data) 
    $em = $manager.StartWorkFlow($item,$emails,$emData)
}

Or you can directly send mail using Powershell.

You can get some examples on google to send mail using Powershell.

Here is one reference: Send mail using Powershell

  • can i just use $items[0] to get one item from the list? – PT_C Oct 27 '15 at 12:58
  • yes it should work.. – P S Oct 28 '15 at 4:51
  • It doesn't i get the same error – PT_C Oct 28 '15 at 13:26
  • Sorry my bad, I just googled and found that we can not use index there. – P S Oct 29 '15 at 4:47
  • Now to get single item you can either use $spListItem = $list.GetItemById(99) or write a caml query with row limit 1 this will return only single item and you can start workflow on that item. – P S Oct 29 '15 at 4:50
0
$web = Get-SPWeb -Identity "http://portal.com/sites/it" 
$manager = $web.Site.WorkFlowManager   
$list = $web.Lists["Certificate Tracking"]

$assoc = $list.WorkflowAssociations.GetAssociationByName("Certificate Notification","en-US")
$assoc.AllowAsyncManualStart = $true
$assoc.AllowManual = $true

$emails = $list.WorkflowAssociations.GetAssociationByName("Status Update","en-US")
$emails.AllowAsyncManualStart = $true
$emails.AllowManual = $true

$view = $list.Views["All Items"] #All Items
$items = $list.GetItems($view)  

$data = $assoc.AssociationData
$emData = $emails.AssociationData

$errorCount = 0

Try
{
    foreach ($item in $items) {
    $wf = $manager.StartWorkFlow($item,$assoc,$data) 
    }
}
Catch [system.exception]
{
   #send email or log to file system
   $errorCount = 1
}
Finally
{
   if ($errorCount -eq 0)
   {
      $em = $manager.StartWorkFlow($item,$emails,$emData)
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.