Execute PowerShell Script - Syntax?

Jul 13, 2011 at 8:12 PM

Hello!

First, thanks for creating these tools....they are incredibly useful.

Second, could you post an example of the syntax for the Execute PowerShell script action?  I keep getting errors along the line of "Error while executing PowerShell Script: The term 'whatever term/command i used' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included..."     Are we supposed to paste the contents of the script directly into the string builder, or reference the file in the string builder?  Do we need to use quotes?   I have not been able to get this to work, and your feedback would be incredibly helpful.

Thanks again.

Oct 10, 2011 at 2:52 AM
vlozada wrote:

Hello!

First, thanks for creating these tools....they are incredibly useful.

Second, could you post an example of the syntax for the Execute PowerShell script action?  I keep getting errors along the line of "Error while executing PowerShell Script: The term 'whatever term/command i used' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included..."     Are we supposed to paste the contents of the script directly into the string builder, or reference the file in the string builder?  Do we need to use quotes?   I have not been able to get this to work, and your feedback would be incredibly helpful.

Thanks again.


Hi Viozada,

Did you reslove this issue? if yes. could you please post the Syntax...

THX

Oct 21, 2011 at 11:21 AM

I'm in the same boat.. I have a simple PowerShell cmdlet that I want to run as an action in an SPD Workflow.

I've tried passing it without other arguments;

Execute PowerShell: Start-SPContentDeploymentJob -identity 'Staging to live Job'

I tried putting it, and the snap-in, in a .ps1 file;

Add-PsSnapin Microsoft.SharePoint.PowerShell
Start-SPContentDeploymentJob -identity 'Staging to live Job'

I tried the above but with a simple line to check if it was running

Set-content -literalpath "C:\Downloads\DeploymentReport.txt" -value "Started"

(this didn't generate the file)

I tried signing the script as per the instructions on the documentation page, which works fine, and while the workflow runs, nothing happens, and there is no History.

I set-ExectutionPolicy unrestricted and tried the above again, no dice, runs alright, but it nothing happens.

I could have the users simply run the Powershell script when they want to update content, but I don't want them to have to log on to the server to do it, I'd much rather do it in a workflow with a Quick Step button.

It's been suggested that I "just do it in code", but I don't speak .net I'm afraid.

Help?

Could someone post an example of *working* syntax?  Do the $vars have to have values?  Can I do without Signature?  What is Secure Store AppID?

Jan 4, 2012 at 7:19 PM
Edited Jan 4, 2012 at 7:19 PM

I'm also having trouble getting the syntax correct. First, the script itself contains only 1 line, for testing purposes. The line is:

echo "This is a test." >> C:\log.txt

The script is located directly in the C:\ directory.

The workflow is a Site Workflow on a subsite of a development server. When I click to edit the workflow in Sharepoint Designer, I only have 1 step, and it says:

Execute Powershell: C:\test.ps1 (Signature Signature ; $var1 var1 ; $var2 var2 ; $var3 var3 ; $var4 var4 ; $var5 var5 ; $secure secure string ; Secure Store AppId: AppId ).

(Everything above except "C:\test.ps1" is blue and not set.)

I save the workflow, and publish it with no errors. Then, I browse to the site, under All Site Content, Workflows, select the workflow, and click Start. It completes successfully, but no log.txt is ever created. No error messages or event viewer entries appear, so I can't tell if it's actually doing anything. I've read the documentation, and it seems incomplete or unclear about a few things. Am I missing something obvious here?

Jan 5, 2012 at 12:36 PM
Edited Jan 5, 2012 at 12:49 PM

@sickaroll: I am also not able to use any standard SharePoint 2010 CmdLets. When I use the SharePoint Object Model, I am perfectly able to achieve what I want. Too bad I can't achieve to use CmdLets.

@omatsei: Regarding the writing a file to the C:\drive. I can imagine there are some permission issues. If you've created the Workflow with an Impersonation step, it seems that the PowerShell script is still being executed with the Web Application Pool account your portal is running under in IIS. Obviously this account does not have any permissions to write things to your C drive. Install ProcMon (from SysInternals) on your SharePoint 2010 server, and see if you receive any "ACCESS DENIED" errors on the FileSystem. This should lead you to a solution.

You could also try to place the PowerShells script inside a SharePoint custom list, or directly into the Workflow, instead on your C drive. Maybe the account executing the workflow does not have any permissions to read from your C drive.

If your trying to test the PowerShell workflow action, I recommend to start with a more simplified PowerShell script. Try to change the Title of the current SPWeb object or current SPListItem object or so.

Good luck!

Coordinator
Jan 5, 2012 at 12:52 PM

Hi,

To execute scripts in PowerShell you have to put a & or a . before the statement.

& c:/Test.ps1

Bye,

Christian

Jan 5, 2012 at 1:19 PM

Great Christian, thanks for getting back to us.

In terms of the script itself, does the path of the script have to be a UNC path or can it execute from a local path on the Application Server?

& \\myappserver\sharenname\script.ps1

or will

& c:\scripts\script.ps1 do?

Are single or double quotes required?  

An example would be fantastic if you had the time.

Coordinator
Jan 5, 2012 at 4:29 PM

Did you try to paste your script directly into the execute powershell action? You don't need to run an external script you, can write script inline in the action itself.

Single or double quotes doens't matter: & "c:\scripts\script.ps1"

The script will runder the identity of the current application pool (securtiy!)

Bye, Christian

Jan 9, 2012 at 3:41 PM

Is it possible to use "Add-PSSnapIn" to a script? For instance, I want to add the Sharepoint and SQL Server snap-ins to the script.

Coordinator
Jan 9, 2012 at 8:16 PM

It should be possible, but the application pool needs very high privileges on sp server and on the sql server. From the security perspective it is not recommended.

Bye,

Christian

Jan 10, 2012 at 8:02 PM

Thanks for the input Christian, it's been extremely helpful so far! I was able to get a script to run, and I'm almost where I need to be. I just can't figure out how to add a Snap-in in the script.

The first three lines of my script are adding the SQL and Sharepoint PSSnapins, which are required to query a SQL database, then process the data, and "export" it to a Sharepoint list. Specifically, the lines in question are:

Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
Add-PSSnapin Microsoft.SharePoint.PowerShell

$sqlquery = "SELECT * FROM [TableName]"
Invoke-Sqlcmd -Database "$sqldatabase" -server "$sqlserver" -username "$sqlusername" -password "$sqlpassword" -query "$sqlquery"

The script starts to run, but then Sharepoint prints the error: "Error while executing PowerShell Script: The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again..."

The Application Pool runs as NetworkService, and I've added that user to the local Administrators group (on the Sharepoint server), but it didn't help. 

Jan 24, 2012 at 8:59 PM

@omatsei - were you ever able to figure out how to get cmdlets to run in this context.  I have a similar situation but I'm not familiar enough with PowerShell to know how to work around the issue.

Thanks.

Jan 26, 2012 at 2:04 PM

@bgullick - No, I wasn't. It seems like anything built into Powershell will work, but not adding additional snapins. I'm not sure if there's a workaround, but I haven't had a chance to look in-depth yet.