简体   繁体   English

Visual Studio生成事件后的Powershell脚本失败

[英]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. 尽管在命令行中可以正常运行相同脚本,但在Visual Studio中将Powershell脚本作为生成后事件运行失败。 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. 以我为例,问题是Windows 10 1903上的Visual Studio 2017 CE,并且症状是无法将Powershell脚本作为生成后事件运行,即使从命令行以相同的特权运行相同的Powershell时也没有错误。

I was getting an exited with error 1 but could see the script was not actually being run. 我退出并出现错误1,但可以看到脚本实际上并未在运行。

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. VS使用的是32位版本(不是按照问题的公认答案使用64位版本),而Powershell的构建并没有设置执行策略。

I ran PowerShell (x86) and Set-ExecutionPolicy Unrestricted, then my post-build tasks ran fine. 我运行了Powershell(x86)和Set-ExecutionPolicy Unrestricted,然后我的生成后任务运行正常。

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. 好吧, http://www.vistax64.com/powershell/205436-running-powershell-post-build-event-ignoring-executionpolicy.html建议,尽管Visual Studio是32位的,但它运行的64位版本的Powershell是独立的。

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?!). 现实情况是Visual Studio运行Windows \\ syswow64版本的Powershell(32位!?),而普通的Powershell命令提示符将默认为Windows \\ system32版本(64位?!)。 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) (该线程不是新话题,但我来自Google,因此我认为与他人分享我发现的解决方案会很有趣)

I tried changing the path to powershell.exe to "%WINDIR%\\SysNative\\WindowsPowerShell\\v1.0\\powershell.exe" and it worked perfect. 我尝试将powershell.exe的路径更改为“%WINDIR%\\ SysNative \\ WindowsPowerShell \\ v1.0 \\ powershell.exe”,它可以完美运行。 The 64 bits version is called from the Post Build event and it successfully adds the SharePoint snapin. 从Post Build事件中调用64位版本,它成功添加了SharePoint管理单元。

Credits to this article: http://msdn.microsoft.com/en-us/library/ff798298.aspx , "Using Windows PowerShell Scripts to Automate Tasks in Visual Studio". 对本文的贡献http : //msdn.microsoft.com/zh-cn/library/ff798298.aspx ,“使用Windows PowerShell脚本在Visual Studio中自动执行任务”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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