Alan Christensen
Alan Christensen

Reputation: 847

Powershell script from Visual Studio Post-build-event failing

Running a powershell script as a post-build event in Visual Studio fails despite the fact that that same script runs fine from the commandline. Why is this?

Upvotes: 3

Views: 2654

Answers (3)

Alex Ferreira
Alex Ferreira

Reputation: 191

(This thread is not new, but I got here from Google, so I thought sharing the solution I found would be interesting to others)

I tried changing the path to powershell.exe to "%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe" and it worked perfect. The 64 bits version is called from the Post Build event and it successfully adds the SharePoint snapin.

Credits to this article: https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ff798298(v=office.14), "Using Windows PowerShell Scripts to Automate Tasks in Visual Studio".

Upvotes: 1

Andrew
Andrew

Reputation: 1140

Sorry for re-animating an ancient question but since my search lead me to this post and my resolution was slightly different I wanted to add it.

In my case the issue was with Visual Studio 2017 CE on Windows 10 1903 and the symptom was a failure to run a powershell script as a post-build event even though the same powershell ran without error from the command line with the same privileges.

I was getting an exited with error 1 but could see the script was not actually being run.

VS was using the 32 bit version (not the 64 as per the accepted answer to the question) and it was that build of powershell that I'd not set the exectution policy on.

I ran PowerShell (x86) and Set-ExecutionPolicy Unrestricted, then my post-build tasks ran fine.

Upvotes: 3

Alan Christensen
Alan Christensen

Reputation: 847

Answering my own question just to save others the pain of researching this.

Well http://www.vistax64.com/powershell/205436-running-powershell-post-build-event-ignoring-executionpolicy.html suggests that despite Visual Studio being 32 bit, it runs the 64 bit version of powershell which is independent.

The reality seems that Visual Studio runs the Windows\syswow64 version of Powershell (32 bit!?) while your normal Powershell command prompt will default to the Windows\system32 version (64 bit?!). You need to set execution policy etc for that separately.

Upvotes: 0

Related Questions