是否应在Prem Azure Devops代理上停止执行Powershell Exception?

时间:2019-12-11 20:38:28

标签: azure powershell release

答案通常是“是”。但是,我遇到了一个非常奇怪的场景,带来了灾难性的后果。

我有一个Powershell脚本,该脚本在发布管道的每个阶段运行,可在执行安装程序之前清除安装程序所在的特定目录。问题脚本非常简单:

$ErrorActionPreference = "Stop"

$user = whoami
$env = '$(Agent.Name)'
$artifact = '$(ArtifactName)'
Write-Host 'Deleting local folder for Artifact: ' $artifact ' on: ' $env ' as ' $user

try
{
    $inst = '$(InstallerDirectory)'
    $path = "$inst\"

    $testPath = Test-Path -Path "$(InstallerDirectory)\"


    if($testPath)
    {
        Invoke-Command  -ScriptBlock {Remove-Item "$(InstallerDirectory)\*" -Force -Recurse -Verbose} -Verbose
    }
    else
    {
        Write-Host $path ' does not exist'
    }

}
catch
{
    <#The last error record is available inside the catch block under the $_ variable.#>
    write-host "Caught an exception:" -ForegroundColor Red
    write-host "Exception Type: " + $_.Exception.GetType().FullName -ForegroundColor Red
    write-host "Exception Message: " + $_.Exception.Message -ForegroundColor Red
Write-Host '##vso[task.complete result=Failed;]DONE'
}

如果按原样在Powershell ISE中或仅在Powershell中运行此脚本,则会得到以下输出:

Deleting local folder for Artifact:  $(ArtifactName)  on:  $(Agent.Name)  as  *****
Caught an exception:
Exception Type:  + System.Management.Automation.CommandNotFoundException
Exception Message:  + The term 'InstallerDirectory' 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.
##vso[task.complete result=Failed;]DONE

这很有意义,我有一个$(InstallerDirectory)变量(以及其他变量),该变量应在发行版运行时从“过程变量”或“变量组”中注入,因此,如果不存在该变量,您会期望它会失败,因为找不到文件路径。

不幸的是,事实并非如此。相反,会发生这种情况:

2019-11-07T19:28:51.2221276Z ##[section]Starting: PowerShell Script - Delete Files
2019-11-07T19:28:51.2461872Z ==============================================================================
2019-11-07T19:28:51.2461993Z Task         : PowerShell
2019-11-07T19:28:51.2462035Z Description  : Run a PowerShell script on Windows, macOS, or Linux.
2019-11-07T19:28:51.2462073Z Version      : 2.140.2
2019-11-07T19:28:51.2462126Z Author       : Microsoft Corporation
2019-11-07T19:28:51.2462164Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=613736)
2019-11-07T19:28:51.2462202Z ==============================================================================
2019-11-07T19:28:52.3807051Z Generating script.
2019-11-07T19:28:52.4421725Z ##[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\vstsagent_work\_temp\c3f053d4-555d-4900-9c6d-702bcc858108.ps1'"
2019-11-07T19:28:52.7257381Z Deleting local folder for Artifact:  App  on:  SERVERNAME  as  domain\sa-devops-agents
2019-11-07T19:28:52.7786538Z VERBOSE: Performing the operation "Remove Directory" on target "D:\$RECYCLE.BIN".
2019-11-07T19:28:52.7796041Z VERBOSE: Performing the operation "Remove Directory" on target 
2019-11-07T19:28:52.7797016Z "D:\$RECYCLE.BIN\S-1-5-21-2758866545-473063294-3191214161-106116".
2019-11-07T19:28:52.7806688Z VERBOSE: Performing the operation "Remove File" on target 
2019-11-07T19:28:52.7807962Z "D:\$RECYCLE.BIN\S-1-5-21-2758866545-473063294-3191214161-106116\desktop.ini".
2019-11-07T19:28:52.7819386Z VERBOSE: Performing the operation "Remove Directory" on target 
2019-11-07T19:28:52.7820835Z "D:\$RECYCLE.BIN\S-1-5-21-2758866545-473063294-3191214161-192463".
2019-11-07T19:28:52.7828818Z VERBOSE: Performing the operation "Remove Directory" on target 
2019-11-07T19:28:52.7830081Z "D:\$RECYCLE.BIN\S-1-5-21-2758866545-473063294-3191214161-192463\$RCT5CRU".
2019-11-07T19:28:52.7837587Z VERBOSE: Performing the operation "Remove Directory" on target 
2019-11-07T19:28:52.7838717Z "D:\$RECYCLE.BIN\S-1-5-21-2758866545-473063294-3191214161-192463\$RCT5CRU\config".
2019-11-07T19:28:52.7846985Z VERBOSE: Performing the operation "Remove File" on target 
2019-11-07T19:28:52.7848184Z "D:\$RECYCLE.BIN\S-1-5-21-2758866545-473063294-3191214161-192463\$RCT5CRU\config\server.json".
2019-11-07T19:28:52.7857824Z VERBOSE: Performing the operation "Remove File" on target 
2019-11-07T19:28:52.7859144Z "D:\$RECYCLE.BIN\S-1-5-21-2758866545-473063294-3191214161-192463\$RCT5CRU\config\services.json".
2019-11-07T19:28:52.7870624Z VERBOSE: Performing the operation "Remove Directory" on target 
2019-11-07T19:28:52.7871823Z "D:\$RECYCLE.BIN\S-1-5-21-2758866545-473063294-3191214161-192463\$RCT5CRU\logs".
2019-11-07T19:28:52.7879749Z VERBOSE: Performing the operation "Remove File" on target 
2019-11-07T19:28:52.7880895Z "D:\$RECYCLE.BIN\S-1-5-21-2758866545-473063294-3191214161-192463\$RCT5CRU\logs\dummy-for-creating-the-logs-folder.txt".
2019-11-07T19:28:52.7891897Z VERBOSE: Performing the operation "Remove File" on target 
2019-11-07T19:28:52.7893172Z "D:\$RECYCLE.BIN\S-1-5-21-2758866545-473063294-3191214161-192463\$RCT5CRU\consul.exe".
2019-11-07T19:28:52.7911733Z VERBOSE: Performing the operation "Remove File" on target 
2019-11-07T19:28:52.7913167Z "D:\$RECYCLE.BIN\S-1-5-21-2758866545-473063294-3191214161-192463\$RCT5CRU\nssm.exe".
2019-11-07T19:28:52.7926767Z VERBOSE: Performing the operation "Remove File" on target 
2019-11-07T19:28:52.7928273Z "D:\$RECYCLE.BIN\S-1-5-21-2758866545-473063294-3191214161-192463\$I0UGHF0.txt".
2019-11-07T19:28:52.7936719Z VERBOSE: Performing the operation "Remove File" on target 
2019-11-07T19:28:52.7937955Z "D:\$RECYCLE.BIN\S-1-5-21-2758866545-473063294-3191214161-192463\$ICT5CRU".
2019-11-07T19:28:52.7946489Z VERBOSE: Performing the operation "Remove File" on target 
...

哦!

如果不选中它,将递归删除代理安装在驱动器上的所有内容。

我的问题是,为什么?在本地使用错误的文件路径运行脚本会产生错误,无论如何该错误应该执行失败。在这种情况下,我实际上正在测试可以首先找到该路径,并将所有内容粘贴在try-catch块中,但是它仍然不会抛出错误或在代理运行时未能“找到文件路径”它。

为什么当代理运行它时它会以这种方式运行,我想念什么?

0 个答案:

没有答案