尝试获取在 Artifacts 运行 Powershell 脚本中使用的开发测试实验室 VM 用户名

Trying to get dev test lab VM username used in Artifacts Run Powershell script

我们创建了一个工件 "Clone a Git Repo" - 从 git hub 克隆 PS1 脚本 “运行 Powershell”运行 PS1 脚本。 这些神器链接到 Formulae。

在创建新 VM 时,公式将应用于 运行,其中 运行 是 PS1 脚本。 一旦公式为 运行.

,尝试使用下面的 powershell ps1 脚本发送邮件

PS1 脚本

...
...
##Send mail to the User email
$ipV4 = Test-Connection -ComputerName (hostname) -Count 1  | Select -ExpandProperty IPV4Address
$emailId = $env:UserName+"@gmail.com"    
$body = "<html>
            <body>
                Hi $emailId, </br>
                </br> 
                Your VM $ipV4 is ready to access. <br/>
                Thanks,</br>
                Digital Engagement Team
            </body>
        </html>"
Send-MailMessage -To $emailId -from *** -Subject "Azure VM $ipV4 is Ready" -SmtpServer ****** -port 25 -Body $body -BodyAsHtml

我厌倦了这样做,但脚本无法发送消息可能是因为 $env:UserName 无效。如何获取 VM 用户名作为 PS1 的参数,以便我可以附加 gmail.com 并发送?

您可能需要使其 uppper-cased 才能找到变量。查看 accessing variables through the environment:

的文档

Notice that variables are also made available to scripts through environment variables. The syntax for using these environment variables depends on the scripting language.

The name is upper-cased, and the . is replaced with the _. This is automatically inserted into the process environment. Here are some examples:

  • Batch script: %VARIABLE_NAME%
  • PowerShell script: $env:VARIABLE_NAME
  • Bash script: $VARIABLE_NAME