Powershell 命令在 Visual Studio 的构建事件中不起作用

标签 powershell visual-studio-2013

当我通过 Visual Studio 中的构建事件 运行文件时,我尝试的每个内置 powershell 命令都会出现相同的错误。

Visual Studio 中的构建事件窗口

enter image description here

错误

------ Build started: Project: XAzure, Configuration: Debug Any CPU ------ XAzure-> C:\visual studio 2013\Projects\test\bin\XAzure.dll

Get-Date : The term 'Get-Date' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At C:\visual studio 2013\Projects\XAzure\WaitForFiles.ps1:3 char:15 + $dateTime = $(Get-Date) + ~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-Date:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

该文件仅包含这一行 $dateTime = $(Get-Date) 并且失败。

没关系我运行什么命令

  • 开始- sleep -秒 2
  • 写输出
  • 约会
  • 等等

但是如果我关闭 Get-Date,例如

$start =[System.DateTime]::Now.ToString("yyyy-MM-dd-HH-mm-ss.fff") 

它工作得很好。但这不是解决方案。

我在 Build Events 中用

启动了它
Powershell -ExecutionPolicy bypass -File "$(ProjectDir)WaitForFiles.ps1" "$(ProjectDir)\" 

但我也尝试过许多其他变体,例如(无法全部记住它们)

Powershell -ExecutionPolicy Unrestricted -File "$(ProjectDir)WaitForFiles.ps1" "$(ProjectDir)\"

在 powershell 窗口中运行此文件效果很好所以我必须有命令但不是在构建事件中。

我在 win32 Windows 8.1 i7 戴尔笔记本电脑上以管理模式运行 Visual Studio 2013 update 4。

运行 $psversiontable 给我

Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.34014
BuildVersion                   6.3.9600.17090
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2

我曾尝试为 powershell 提供更多访问权限和更高权限,但没有任何效果。

我想不出还有什么可以添加到这个问题或尝试。这个周末我在谷歌上搜索了 3 个小时,并尝试了我能找到的关于这个错误的所有内容,但都没有成功。

奇怪的是,有很多这样的“术语‘x’无法识别..”的错误似乎完全不同的原因。

更新 1

我已尝试使用此命令显式“安装”Microsoft.PowerShell.Utiltiy(在管理员 Powershell 窗口中) 导入模块 Microsoft.PowerShell.Utility

但是我得到了这个错误

Import-Module : The specified module 'Microsoft.PowerShell.Utility' was not loaded because no valid module file was found in any module directory.

如果我运行 $env:PSModulePath 我得到

c:\Program Files\AppFabric 1.1 for Windows Server\PowershellModules;

C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement

但是 如果我运行 %windir%\system32\WindowsPowerShell\v1.0\Modules 我找到 C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules

这是否意味着这组模块没有以某种方式连接?我在那里看到了 Microsoft.PowerShell.Utiltiy 文件夹。

最佳答案

试试这个:

cmd /c powershell -executionpolicy bypass -file "$(ProjectDir)WaitForFiles.ps1"

Get-Date cmdlet 位于 Microsoft.Powershell.Utility 模块中。也许您可以尝试显式加载它:

Import-Module Microsoft.PowerShell.Utility

关于Powershell 命令在 Visual Studio 的构建事件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28810631/

相关文章:

c# - Powershell正则表达式从日志文件中的行中提取圆括号之间的字符串

arrays - 在 Powershell 中从数组中提取数据

node.js - 如何解决 npm MS 构建问题

visual-c++ - LNK2019 - Visual Studio 2013 - 尽管定义了所有符号仍出错

.net - 如何根据 Visual Studio 版本将 sql server 单元测试项目指向 localdb?

C++ unique_ptr 移动构造函数

powershell - 在 bat 错误时自动停止 powershell 脚本

powershell - powershell卸载多个应用程序?

string - 如何在 Powershell 控制台中分配多行字符串

c++ - while(fin>>a) 或 while(fin.eof()) 将不起作用。在第一种情况下,fin 流变量一直将文件中的最后一个字符作为输入