We have a artifacts created "Clone a Git Repo" - Clones the PS1 script from git hub "Run Powershell" run the PS1 script. These artifact is linked to Formulae.
On creating a new VM, the Formulae is applied to run which runs the PS1 script. Tried to a use the below powershell ps1 script to send mail once the formulae is run.
PS1 script
...
...
##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
I tired to do this but the script is failing to send message may be becauseof $env:UserName
is not valid. How can I get the VM username as parameter to PS1 so that I can append gmail.com and send?
You probably need to make it uppper-cased for it to find the variable. Check the documentation for 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