调用 TF.exe 的 powershell 脚本中的表达式或语句中出现意外标记 'workspace'

Unexpected token 'workspace' in expression or statment in powershell script calling TF.exe

我正在编写 PowerShell(5.1 版)脚本以从 TFS 获取特定项目的最新源代码,但出现错误:

"Unexpected token 'workspace' in expression or statement."

我在这里搜索并用谷歌搜索了错误,但找不到对此错误的具体参考。我尝试了多种语法组合,使用单引号与双引号等,但没有取得任何成功。我不明白这是什么问题...

这是代码:

$TFdir="C:\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer"
$TF="'$TFdir\tf.exe'"
$WorkSpaceName="C:\temp\NewTestBuildProject"
$expr="$TF workspace /new $WorkSpaceName /noprompt /collection:http://(servername):8080/tfs/defaultcollection"

$expr

这是上面一行的计算结果:

'C:\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\tf.exe' workspace /new C:\temp\NewTestBuildProject /noprompt /collection:http://(servername):8080/tfs/defaultcollection

这是执行上述表达式的行,也是产生错误的地方:

Invoke-Expression $expr

这是它生成的错误:

Invoke-Expression : At line:1 char:143
+ ... ions\Microsoft\TeamFoundation\Team Explorer\tf.exe' workspace /new 
C: ...
+                                                         ~~~~~~~~~
Unexpected token 'workspace' in expression or statement.
At C:\Users\PF12764\Desktop\PS\PublishWithPowerShell.ps1:42 char:1
+ Invoke-Expression $expr
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ParserError: (:) [Invoke-Expression], 
ParseException
+ FullyQualifiedErrorId : 
UnexpectedToken,Microsoft.PowerShell.Commands.InvokeExpressionCommand

Lee_Dailey,你说的对!此命令有效:

& "C:\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\tf.exe" workspace /new mytemp /noprompt /collection:http://(server):8080/tfs/defaultcollection