首页 文章

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

提问于
浏览
3

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

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

[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

Does anyone know how TeamCity actually runs Powershell?

Update

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

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

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

根据TaskManager,再次在同一台机器上作为同一用户运行

3 回答

  • 0

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

    #requires -Modules Pscx
    
  • 1

    基思的回答让我走上了正确的道路 .

    我能够通过 C:\Program Files (x86)...C:\Program Files (x86)...C:\Windows\System32\WindowPowerShell\v1.0\Modules 解决问题

    不确定为什么社区扩展默认情况下不会安装在Modules目录中,包含所有其他模块 . 但出于某种原因 TeamCity couldn't load the module when it was in the Program Files (x86) directory .

    我从TeamCity运行了64位版本的PowerShell,在这种情况下,它将无法加载32位(x86)模块,但我还运行了64位版本的powershell它手动,然后它工作 .

    所以我仍然有点困惑为什么TeamCity看不到该模块,但是在正确的powershell安装中将它移动到Modules目录修复了这个问题 .

  • 0

    不知道这对任何人都有帮助,但是我打开了cmder并且团队城市的代理在同一个用户下运行 . 出于某种原因,通过打开cmder,它以某种方式锁定了powershell . 我无法解释但关闭cmder解决了这个问题 .

相关问题