【发布时间】:2013-12-13 21:01:01
【问题描述】:
我有一个与 Team Foundation Server 交互的 PowerShell 脚本。当我在 PowerShell 控制台中运行它时,它运行良好。这很适合测试它,但我想通过双击它或批处理文件或其他东西来运行它。我什至会满足于右键单击它并选择“使用 PowerShell 运行”。
但是当我这样做时,我得到一个错误。 “使用 PowerShell 运行”关闭窗口太快,无法查看错误所在。当他们设计那个时,有人真的在想,也许鲍尔默参与其中。我也可以在cmd.exe中运行,像这样:
PowerShell -File dostufftocheckouts.ps1
当我这样做时,我会看到一条错误消息,我猜它可能是同一个:
Get-PSSnapin : No Windows PowerShell snap-ins matching the pattern
'Microsoft.TeamFoundation.PowerShell' were found. Check the pattern and then
try the command again.
以下代码首先包含在脚本中:
if ((Get-PSSnapin -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue
}
当我启动交互式 PowerShell shell 的新实例并在其中运行脚本时,一切正常。
更新
我在使用以下任一 PowerShell 可执行文件时遇到了相同的错误(因为我记得 TFS 管理单元只是 32 位的):
C:\windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe
在 cmd.exe 中,以下命令产生以下输出:
c:\ powershell -Command "get-pssnapin -registered | where { $_.Name -eq 'TfsBPAPowerShellSnapIn' }"
Name : TfsBPAPowerShellSnapIn
PSVersion : 2.0
Description : This is a PowerShell snap-in that includes Team Foundation Server cmdlets.
所以,我写了一个非常简单的脚本,joke.ps1:
Add-PsSnapin TfsBPAPowerShellSnapIn
$server = Get-TfsServer tfsserver/DefaultCollection
然后我运行它:
c:\ powershell -File .\joke.ps1
The term 'Get-TfsServer' 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:\Users\jmcnamara\PowerShell\broken.ps1:3 char:24
+ $server = Get-TfsServer <<<< gearys/DefaultCollection
+ CategoryInfo : ObjectNotFound: (Get-TfsServer:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Add-PsSnapin 不会给我一个错误。但添加管理单元不会使任何管理单元的 cmdlet 对脚本的其余部分可见。
据称,Add-PsSnapin 在当前会话中添加了一个管理单元:
Add-PSSnapin cmdlet 将已注册的 Windows PowerShell 管理单元添加到 当前会话。添加管理单元后,您可以使用 管理单元当前支持的 cmdlet 和提供程序 会话。
“你”可以,是吗? “你”是谁?是的,当然可以。
但是怎么做呢?
【问题讨论】:
-
用-noexit参数启动powershell,我们可以看到错误。
-
@Knuckle-Dragger 同样的错误。
-
@Knuckle-Dragger 是的,听起来你是对的。谢谢。
标签: powershell tfs