Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Email PowerShell:发送以正文为变量的电子邮件_Email_Powershell_Variables - Fatal编程技术网

Email PowerShell:发送以正文为变量的电子邮件

Email PowerShell:发送以正文为变量的电子邮件,email,powershell,variables,Email,Powershell,Variables,我有一个脚本,用来检查用户是否匹配两个报告。如果有匹配的用户,则应发送一条消息。如果用户不匹配,我希望它发送一条消息,只是说“没有用户可以移动”。我的问题是,无论我做什么,它总是发送相同的消息 $TermRep = Import-Csv TestFileTerm.csv $Donna = Import-Csv TestFileDonna.csv #Job to match users between CSVs $Job = ForEach($i in $TermRep){ $TID = $($

我有一个脚本,用来检查用户是否匹配两个报告。如果有匹配的用户,则应发送一条消息。如果用户不匹配,我希望它发送一条消息,只是说“没有用户可以移动”。我的问题是,无论我做什么,它总是发送相同的消息

$TermRep = Import-Csv TestFileTerm.csv
$Donna = Import-Csv TestFileDonna.csv

#Job to match users between CSVs
$Job = ForEach($i in $TermRep){
$TID = $($i.UserID)
ForEach($u in $Donna){
$DID = $($u.UserID)
If($TID -eq $DID){
"Move-ADObject -Identity $TID -TargetPath 'NEWPATH' `r`n"
            }
    }
}

$smtpServer = "server.stuff.com"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "PowerShell.Reports@work.com"
$msg.To.Add("me@work.com")
#$msg.To.Add("administrator2@test.com")
$msg.Priority = "High"
$msg.Subject = "Users To Move"
$msg.Body = "The following users exist in Termination Report and need to be moved.  Please run the following commands: `r`n $Job"
$smtp.Send($msg)
我已尝试将
$msg.body
转换为

If($TID -eq $DID){
$msg.Body = "The following users exist in Termination Report and need to be moved.  Please run the following commands: `r`n $Job"}
Else{$msg.Body = "No users to move."}
无论
If
是否为真,它仍然只显示第一个选项。我还尝试在
ForEach

If($TID -ne $DID){
$Job = "No users to move."
}
如果我运行
$Job
它的值是
“没有用户可以移动。”
但它仍然运行电子邮件中的另一行。有人能帮忙吗?PowerShell v2.0。我尝试将
$Job=@()
放在
If
中,也先将其清除,但没有改变

额外尝试 我也尝试过这样格式化:

$msg.Body = "The following users exist in Termination Report and need to be moved.  Please run the following commands:`r`n"
If($Job -like "*"){
$msg.Body += "$Job"
}
Else{
$msg.Body = "No user to move for $Date."
}
如果
$Job
有任何值,我仍然会得到正确的结果,但是
Else
语句在这里不起作用。我还尝试了
$msg.Body+=“没有用户移动$Date。”
,这也不起作用

解决了。 我把最后一个从
-like
改为
-notlike
,似乎已经解决了这个问题:

If($Job -notlike ""){
$msg.Body += "$Job"
}
Else{
$msg.Body += "No users to move for $Date."
}
$smtp.Send($msg)

您确定在执行if检查时,
$TID
$DID
匹配吗。在TID上找到匹配项后查看您的代码,它是否继续循环,并且
$TID
$DID
再次更改。您是否可以不使用compare object进行初始比较工作?@DaneBoulton不确定我是否理解。我正在从循环中得到我想要的结果,是的,它们确实不断循环,所以我每一行都会得到一个结果,而不是每一行,但我并不担心它。
If($Job){}
也应该足够了,只要您将它初始化为“”两次通过之间。@Matt我简历上的标题是
professional-dislike
当你进行if检查时,你确定
$TID
$DID
匹配吗。在TID上找到匹配项后查看您的代码,它是否继续循环,并且
$TID
$DID
再次更改。您是否可以不使用compare object进行初始比较工作?@DaneBoulton不确定我是否理解。我正在从循环中得到我想要的结果,是的,它们确实在不断循环,所以我每一行都会得到一个结果,而不是每一行,但我并不担心它。
If($Job){}
也应该足够了,只要你在两次循环之间将其初始化为“”。@我简历上的马特标题是
professional-disliker