TeamCity PowerShell Runner 无法识别 PowerShell 社区扩展

标签 powershell teamcity

当 TeamCity Powershell 运行程序运行我的 .ps1 文件时,它无法识别作为 Powershell 社区扩展的一部分的 Write-Zip 命令。

我可以在构建服务器上手动运行相同的命令,它工作得很好。我已经验证 TeamCity 和我都在运行 x64 版本的 powershell.exe 并且都以同一用户身份运行。

[17:21:22][Step 5/5] Packaging WWW.zip
[17:21:45][Step 5/5] Write-Zip : The term 'Write-Zip' is not recognized as the name of a cmdlet, 
[17:21:45][Step 5/5] function, script file, or operable program. Check the spelling of the name, or 
[17:21:45][Step 5/5] if a path was included, verify that the path is correct and try again.
[17:21:45][Step 5/5] At 
[17:21:45][Step 5/5] C:\TeamCity\buildAgent\work\e19b1309c030c7e2\Build\PowerShell\Package.ps1:20 
[17:21:45][Step 5/5] char:1
[17:21:45][Step 5/5] + Write-Zip -InputObject $items -OutputPath .\WWW.zip
[17:21:45][Step 5/5] + ~~~~~~~~~
[17:21:45][Step 5/5]     + CategoryInfo          : ObjectNotFound: (Write-Zip:String) [], CommandNo 
[17:21:45][Step 5/5]    tFoundException
[17:21:45][Step 5/5]     + FullyQualifiedErrorId : CommandNotFoundException
[17:21:45][Step 5/5] 

我认为这可能与从不同环境运行的 powershell 有关,但我还不完全理解这一点。

我在 TeamCity 构建日志中发现脚本正在像这样运行:
 C:\Windows\sysnative\cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -ExecutionPolicy ByPass -File C:\TeamCity\buildAgent\work\e19b1309c030c7e2\Build\PowerShell\Package.ps1 -WorkingDirectory C:\TeamCity\buildAgent\work\e19b1309c030c7e

请注意,powershell 正在由 C:\Windows\sysnative\cmd.exe 运行

但是,在构建服务器上,我看不到这个目录或 cmd.exe 版本,如果我尝试从 PowerShell 或命令提示符运行相同的命令,它会说找不到 C:\Windows\sysnative\cmd.exe

有谁知道 TeamCity 是如何实际运行 Powershell 的?

更新

我尝试使用命令行运行程序运行我的 powershell 脚本,然后像 TeamCity 一样使用 -File 命令调用 powershell.exe。

我让 TaskManager 运行,看到它运行的 cmd.exe 进程是 C:\Windows\SysWOW64\cmd.exe

我得到了与上面打印的完全相同的错误,Write-Zip 无法识别。但是,如果我在 ...\SysWOW64\cmd.exe 中手动运行相同的命令,它就可以工作。

再一次,根据TaskManager,两者在同一台机器上以相同的用户身份运行

最佳答案

您是否在脚本中导入 PSCX 模块? PowerShell v3 将在您完成此操作后缓存模块信息,因此您无需再次导入它。但是,如果 TeamCity 正在运行 64 位控制台,而您通常运行 32 位控制台,则 64 位控制台的命令缓存中不会有 PSCX 命令。无论如何,让您的脚本明确需要它所依赖的模块是一个好习惯,例如

#requires -Modules Pscx

关于TeamCity PowerShell Runner 无法识别 PowerShell 社区扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17702740/

相关文章:

由于 Filesystemwatcher,Powershell 挂起

teamcity - 为什么在 TeamCity 构建中构建后步骤 (xcopy) 偶尔会退出并显示代码 2?

命令中的 Powershell 对象字段

powershell - 使用 Powershell 从 .zip 文件中删除文件

azure - Powershell - 按第一个分割字符串值返回 Azure Blob 大小

string - 如何将 cmdlet 插入到 powershell 中的字符串中

TeamCity 7.0.1 命令行构建步骤和 ERRORLEVEL

mono - 在 CentOS 上使用 TeamCity 从 GitHub 构建 C#(Mono)解决方案

.net - CruiseControl .NET - 配置哪些内容以避免多次触发 CI 构建

teamcity - 构建状态硬件