本文介绍了Visual Studio预构建Powershell脚本以代码9009退出并且无法构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我设置了预构建事件,该事件会更新* .cs和* .xaml文件(正在构建的项目中包含)的内容.

In my project I have set pre-build event that updates content of *.cs and *.xaml files (which are included in project being built).

我使用Set-Content Powershell命令.

I use Set-Content Powershell command for it.

当我在Powershell控制台中对其进行测试时,一切运行正常.

When I test it in Powershell console everything is working perfectly.

但是当我在预构建事件中粘贴时,它将生成代码9009的错误,并阻止VS成功构建我的项目.

But when I paste as in pre-build event it generates error with code 9009 and prevents VS from successfully building my project.

我将build输出的详细程度设置为Diagnostic,但是没有更多信息.

I have set build output verbosity to Diagnostic, but there's no more information on it.

可能是什么问题?

我目前的猜测是VS锁定了用于构建的文件,因此Powershell命令无法修改它们.

My current guess is that VS locks files for building, thus Powershell commands can't modify them.

推荐答案

问题原因:

1.VS将调用类似cmd.exe的命令来执行您在Build Events中键入的命令.

1.VS will call something like cmd.exe to execute the command you type in Build Events.

这意味着您正在尝试在cmd.exe中运行Set-content命令.

That means you're trying to run Set-content command in cmd.exe.

2.如 boxdog 所说,VS中的code9009确实表示the command is not recognized.

2.Like boxdog said, code9009 in VS do indicate the command is not recognized.

更具体地说,Set-content是PowerShell命令,不是 cmd.exe 可以识别的命令.(请参阅#1,VS调用cmd.exe来运行该命令).

More specifically, the Set-content is PowerShell command, not command recognized by cmd.exe.(See #1, VS calls cmd.exe to run the command).

因此发生了问题:Type a PowerShell command in build events => VS calls cmd.exe to execute it => cmd.exe can't recognize a PS command. and throw error=> VS output log displays error code9009.

建议:

您不能直接在构建事件中键入Set-content来运行(直接在CMD中运行Set-content的设备会导致错误).

You can't directly type Set-content in build events to run(equipment to run Set-content directly in CMD and it will cause error).

相反,我们可以调用powershell来运行cmd.exe中的PS脚本或PS命令,您可以找到许多有关如何从CMD在线运行PS脚本的相关主题.请参见在cmd中运行PS命令,,.但这更像是另一个问题,所以我在这里不多说:)

Instead we can call powershell to run PS script or PS command in cmd.exe, you can find many related topics about how to run PS script from CMD online. See run PS command in cmd, run PS from CMD, How to run PS in CMD. But it's more like another issue, so I don't talk too much here:)

摘要:您可以选择创建一个新的PS脚本并将Set-content命令移入其中,然后在构建事件中键入powershell -file path\xx.ps1或简单的powershell - command之类的命令.然后Error9009将消失. (我相信Error9009将会消失,但是我已经看到了很多其他错误代码的问题,其中大多数与路径问题有关,因此请注意脚本中定义的路径.)

In summary: You can choose to create a new PS script and move your Set-content command into it, and type command like powershell -file path\xx.ps1 or simple powershell - command in build events. Then the Error9009 would go away. (I believe the Error9009 would go away, but I've seen too many issues about other Error Codes, most of them are related to path issues, so take care about the Path defined in your script.)

这篇关于Visual Studio预构建Powershell脚本以代码9009退出并且无法构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:29