我有一个脚本,用于检查用户是否在两个报告之间匹配。如果有任何用户匹配,则应发送一条消息。如果用户不匹配,我希望它发送一条消息,上面写着"没有用户移动。"我的问题是,无论我做什么,它总是发送相同的信息。
$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
,其值为"No users to move."
,但它仍会在电子邮件中运行另一行。有人可以帮忙吗? 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 += "No user to move for $Date."
这也行不通。
我将最后一个从-like
更改为-notlike
,这似乎修复了它:
If($Job -notlike ""){
$msg.Body += "$Job"
}
Else{
$msg.Body += "No users to move for $Date."
}
$smtp.Send($msg)