我正在尝试在远程计算机上运行跟踪,并期望得到一个错误,因为未创建文件而找不到文件,但这是我想要的,然后代码应到达catch
块。我将它与Jenkins管道一起使用,它应该返回1并退出错误,但返回的是绿色。
我尝试将$ErrorActionPreference
设置为“停止”,并在命令末尾添加了-ErrorAction Stop
,但仍然可以通过。
$Server = $args[2]
$username = $args[3]
$password = $args[4]
$pass = "$password" | ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PsCredential("ad\$username", $pass)
try {
$session = New-PSSession -ComputerName $Server -Credential $Cred
Invoke-Command -Session $session -ScriptBlock {
netsh trace start capture=yes tracefile=C:\Traces1\netsh.etl
} -ErrorAction Stop
Start-Sleep -Seconds $Time
Invoke-Command -Session $session -ScriptBlock {
netsh trace stop
}
} catch {
$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage
exit 1
}
exit 0