Version control 捕获用于流控制的非标准Powershell CmdLet输出 目前尝试使用MS发布的“Team Foundation Server Power工具”包中的CMDLew构建脚本。

Version control 捕获用于流控制的非标准Powershell CmdLet输出 目前尝试使用MS发布的“Team Foundation Server Power工具”包中的CMDLew构建脚本。,version-control,tfs,powershell,Version Control,Tfs,Powershell,我试图通过“Update-TfsWorkspace”cmdlet的成功或失败来传递命令逻辑,但我似乎无法从调用中获取返回代码,也无法使用out字符串捕获输出。我正在使用Powershell v1 update-tfsworkspace "C:\doesnotexist\" -recurse -version T 生成一条“无法确定工作区”的消息。这是我试图捕获的错误 $ret = update-tfsworkspace "C:\doesnotexist\" -recurse -version

我试图通过“Update-TfsWorkspace”cmdlet的成功或失败来传递命令逻辑,但我似乎无法从调用中获取返回代码,也无法使用out字符串捕获输出。我正在使用Powershell v1

update-tfsworkspace "C:\doesnotexist\" -recurse -version T
生成一条“无法确定工作区”的消息。这是我试图捕获的错误

$ret = update-tfsworkspace "C:\doesnotexist\" -recurse -version T
希望给我一个$true/$false,表示成功/失败,但不起作用

update-tfsworkspace "C:\doesnotexist\" -recurse -version T | Out-Null
trap{echo "fail"}
update-tfsworkspace $workspace_path -recurse -version T
$msg = update-tfsworkspace $workspace_path -recurse -version T | Out-String
预期将阻止cmdlet写入消息,但不起作用

update-tfsworkspace "C:\doesnotexist\" -recurse -version T | Out-Null
trap{echo "fail"}
update-tfsworkspace $workspace_path -recurse -version T
$msg = update-tfsworkspace $workspace_path -recurse -version T | Out-String
预计将捕获错误并写入“fail”,但不起作用

update-tfsworkspace "C:\doesnotexist\" -recurse -version T | Out-Null
trap{echo "fail"}
update-tfsworkspace $workspace_path -recurse -version T
$msg = update-tfsworkspace $workspace_path -recurse -version T | Out-String
应使用主机输出填充$msg变量,但不起作用

update-tfsworkspace "C:\doesnotexist\" -recurse -version T | Out-Null
trap{echo "fail"}
update-tfsworkspace $workspace_path -recurse -version T
$msg = update-tfsworkspace $workspace_path -recurse -version T | Out-String

我完全没有主意了。谢谢你的帮助

有点麻烦,但由于我没有TFS来尝试找出其他问题,请查看是否有帮助。

我认为此cmdlet编写不正确。首先,由于它没有成功,它应该发出一个错误对象,这将导致$?返回可以检查或捕获的false。其次,不能使用-ea0来抑制错误消息。看起来此管理单元正在使用主机api将错误字符串写入主机控制台。那是喷出来的!!现在,你可以按照艾伯格林的建议去做:

$msg=powershell.exe-nologo更新tfsworkspace“C:\doesnotexist\”-recurse-version t2>&1


只需注意当新的PowerShell实例启动时配置文件脚本发出的所有文本。

这里的问题是cmdlet正在写入错误(非终止错误),但没有引发异常(终止错误)。您可以通过添加ErrorAction参数使其引发异常:

trap{echo "fail"}
update-tfsworkspace $workspace_path -recurse -version T -ErrorAction "Stop"

这将导致cmdlet终止所有错误(如果它写入错误流,将引发异常)。

我没有TFS,因此无法测试,因此不会将其作为答案发布。该commandlet是否支持-ErrorAction和/或-ErrorVariable参数?这可能是一个有帮助的方向。如前所述,以下人员可以访问cmdlet输出:PS>powershell.exe-noprofile-file test.ps1>test.log PS>Get Content test.log这太棒了。这正是我今天想要的。非常感谢。