简体   繁体   中英

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?

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.

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.

(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: http://msdn.microsoft.com/en-us/library/ff798298.aspx , "Using Windows PowerShell Scripts to Automate Tasks in Visual Studio".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM