1

I `ve created a powershell which works fine if executed directly via SharePoint 2013 Shell in Admin mode.

However, if running via Windows Tasks manager on daily basis, then the powershell doesnt runs.

It seems by default windows is using the Windows Powershell and not SharePoint Powershell.

Please suggest how to instruct the Windows Task Manager to use SharePoint 2013 Powershell to run the .ps1 file and not windows Powershell.

Else, kindly suggest an alternate that will allow Windows powershell to execute the command.

Any pointer will be deeply predicated.

4

Just add this before your code in the .ps1 file:

#Load SharePoint PowerShell SnapIn
if ( (Get-PSSnapin -Name Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PsSnapin Microsoft.Sharepoint.Powershell
}

This will load the required Snapin. Then all the SharePoint commands should work.

If you want to stop a running workflow via Powershell you can try this:

#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();

If you get the error: The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered, make sure the logged in user who is trying to run SharePoint PowerShell commands is having the right "SharePoint_Shell_Access" to the SharePoint_Configuration database to do so:

  1. Open SQL Management Studio
  2. Browse Security folder — Logins , select the user name that is running power shell , right click properties
  3. Click on User Mapping , check SharePoint_config , select SharePoint_Shell_Access

https://stackoverflow.com/questions/7595256/cancel-multiple-sharepoint-workflows-using-powershell

  • it gives me an error "StartWorkflow" with "4" argument(s):", even though i have powershell rights on the associated SQL db. – Jason Jul 19 '16 at 6:13
  • Well, that sounds that the script is now running. So that's a step into the right direction. Maybe you could post your .ps1 into a new question so everybody could take a look into that. But the question above is now fixed, right? – Patrick Jul 19 '16 at 6:18
  • Not yet, as I tried running the command directly and not via Windows Task. if i run the command using SharePoint 2013 Powershell (admin mode) I get error: Exception calling "StartWorkflow" with "4" argument(s): "New instances of this workflow template are currently disallowed." If i use windows Powershell (admin mode), I get error: Exception calling "StartWorkflow" with "4" argument(s): "New instances of this workflow template are currently disallowed." – Jason Jul 19 '16 at 6:47
  • Is there an powershell command, that allows to stop or terminate any existing workflow running on the server? – Jason Jul 19 '16 at 6:47
  • You can terminate them if you go into your list or library and check there the workflowsettings. You can also delete them there and publish it again, but this has nothing to do with the start option or the script. It is because there a workflows still running – Patrick Jul 19 '16 at 6:57

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.