Visual Studio 构建后事件中的 Powershell 脚本失败

发布于 2024-12-13 07:47:44 字数 75 浏览 1 评论 0原文

在 Visual Studio 中将 powershell 脚本作为生成后事件运行会失败,尽管该脚本可以从命令行正常运行。这是为什么呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

新雨望断虹 2024-12-20 07:47:44

很抱歉重新动画了一个古老的问题,但由于我的搜索将我带到了这篇文章,并且我的分辨率略有不同,所以我想添加它。

就我而言,问题出在 Windows 10 1903 上的 Visual Studio 2017 CE,症状是无法将 powershell 脚本作为构建后事件运行,即使相同的 powershell 以相同的权限从命令行运行且没有错误。

我退出并出现错误 1,但可以看到脚本实际上并未运行。

VS 使用的是 32 位版本(不是按照问题的可接受答案使用的 64 位版本),而且我没有设置执行策略的 powershell 版本。

我运行了 PowerShell (x86) 和 Set-ExecutionPolicy Unrestricted,然后我的构建后任务运行良好。

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.

痴情 2024-12-20 07:47:44

(这个线程并不新鲜,但我从 Google 来到这里,所以我认为分享我发现的解决方案对其他人来说会很有趣)

我尝试将 powershell.exe 的路径更改为“%WINDIR%\SysNative\WindowsPowerShell\v1.0 ” \powershell.exe”并且运行完美。从构建后事件调用 64 位版本,并成功添加 SharePoint 管理单元。

本文来源:https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ff798298(v=office.14),“使用 Windows PowerShell 脚本自动执行任务在 Visual Studio 中”。

(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".

南街女流氓 2024-12-20 07:47:44

回答我自己的问题只是为了避免其他人研究这个问题的痛苦。

那么http://www.vistax64。 com/powershell/205436-running-powershell-post-build-event-ignoring-executionpolicy.html 表明,尽管 Visual Studio 是 32 位,但它运行独立的 64 位版本的 powershell。

事实上,Visual Studio 运行的是 Windows\syswow64 版本的 Powershell(32 位!?),而普通的 Powershell 命令提示符将默认为 Windows\system32 版本(64 位?!)。您需要为此单独设置执行策略等。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文