在字符串输出后输出if subject语句到$ subject和$ body的结果

时间:2013-07-15 20:11:24

标签: email powershell smtp if-statement

您好我试图输出if / else语句的结果,该结果将从另一个文本文件输出结果到主题行和电子邮件正文中。我可以将它输出到powershell窗口,但我无法弄清楚如何将它放在主题行和电子邮件正文中。我对powershell仍然很新,所以任何帮助都会很棒。

谢谢,

-Nick -

# Check if the migrater is running
$processToCheck = "calc"
$process = Get-Process $processToCheck
If (!($process))


# Check what step in the process its in 
{
$Service = get-content c:\AmicasToolService.properties | select-string           "#AmicasService.processes=ArchiveRetrievalTool"
If ($Service) {"is done migrating"}
Else {"is done retrieving"}

# Send Email With Results
$From = "Olorin Migration Status <olorinmigration@gmail.com>"
$To = "nwarner@rchsd.org"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "email@gmail.com"
$Password = "password" 
$subject = "Tape"+" "+(select-string "000..."      c:\amicas2\config\ArchiveMigration.properties | select -Expand Matches | Select -Expand     Value) + ($step) 
$body = "Tape"+" "+(select-string "000..."   c:\amicas2\config\ArchiveMigration.properties | select -Expand Matches | Select -Expand Value) + " " + "has finished retrieval or migration to Centera. Please check the log in the   COA PACS folder for information on which step it is on in the process. \\naspfs01\Depts\ISD\COA\PACS\PACS_Library.xlsx"
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.Send($From, $To, $subject, $body);
}

1 个答案:

答案 0 :(得分:2)

我想你想要这个:

$msg = "$Service is done "
if ($Service) {
    $msg += "migrating"
}
else {
    $msg += "retrieving"
}

$subject = ... " $msg " ...

我希望PowerShell有一个三元运算符,例如:

$msg += $service ? "migrating" : "retrieving"

如果您同意,请对其进行投票here