windows – powershell脚本的电子邮件输出

前端之家收集整理的这篇文章主要介绍了windows – powershell脚本的电子邮件输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我找到了这个很棒的脚本,它将当前DFS backlog的状态输出到power shell控制台.这很好用,但我需要脚本给我发电子邮件,所以我可以安排它每晚运行.我已尝试使用Send-MailMessage命令,但无法使其工作.主要是因为我的powershell技能非常弱.我相信大多数问题都围绕着使用Write-Host命令的脚本.虽然着色很好,但我宁愿让它给我发送结果.我还需要能够指定邮件服务器的解决方案,因为dfs服务器没有电子邮件功能.

欢迎和赞赏任何帮助或提示.

这是代码.

  1. $RGroups = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query "SELECT * FROM DfsrReplicationGroupConfig"
  2. $ComputerName=$env:ComputerName
  3. $Succ=0
  4. $Warn=0
  5. $Err=0
  6.  
  7. foreach ($Group in $RGroups)
  8. {
  9. $RGFoldersWMIQ = "SELECT * FROM DfsrReplicatedFolderConfig WHERE ReplicationGroupGUID='" + $Group.ReplicationGroupGUID + "'"
  10. $RGFolders = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query $RGFoldersWMIQ
  11. $RGConnectionsWMIQ = "SELECT * FROM DfsrConnectionConfig WHERE ReplicationGroupGUID='"+ $Group.ReplicationGroupGUID + "'"
  12. $RGConnections = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query $RGConnectionsWMIQ
  13. foreach ($Connection in $RGConnections)
  14. {
  15. $ConnectionName = $Connection.PartnerName.Trim()
  16. if ($Connection.Enabled -eq $True)
  17. {
  18. if (((New-Object System.Net.NetworkInformation.ping).send("$ConnectionName")).Status -eq "Success")
  19. {
  20. foreach ($Folder in $RGFolders)
  21. {
  22. $RGName = $Group.ReplicationGroupName
  23. $RFName = $Folder.ReplicatedFolderName
  24.  
  25. if ($Connection.Inbound -eq $True)
  26. {
  27. $SendingMember = $ConnectionName
  28. $ReceivingMember = $ComputerName
  29. $Direction="inbound"
  30. }
  31. else
  32. {
  33. $SendingMember = $ComputerName
  34. $ReceivingMember = $ConnectionName
  35. $Direction="outbound"
  36. }
  37.  
  38. $BLCommand = "dfsrdiag Backlog /RGName:'" + $RGName + "' /RFName:'" + $RFName + "' /SendingMember:" + $SendingMember + " /ReceivingMember:" + $ReceivingMember
  39. $Backlog = Invoke-Expression -Command $BLCommand
  40.  
  41. $BackLogFilecount = 0
  42. foreach ($item in $Backlog)
  43. {
  44. if ($item -ilike "*Backlog File count*")
  45. {
  46. $BacklogFileCount = [int]$Item.Split(":")[1].Trim()
  47. }
  48. }
  49.  
  50. $Emailbody += "$BacklogFileCount files in backlog $SendingMember->$ReceivingMember for $RGName
  51. "
  52.  
  53. } # Closing iterate through all folders
  54. } # Closing If replies to ping
  55. } # Closing If Connection enabled
  56. } # Closing iteration through all connections
  57. } # Closing iteration through all groups
  58.  
  59. $emailFrom = "sender@foobar.com"
  60. $emailTo = "recipient@foobar.com"
  61. $subject = "DFS Backlog Report"
  62. $smtpServer = "MailServer"
  63. $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  64. $smtp.Send($emailFrom,$emailTo,$subject,$Emailbody)
如果你想分支出PowerShell,你可以将脚本的输出重定向到文本文件并使用第三方命令行邮件程序如blat将文本文件作为电子邮件的正文(或附件)发送并指定smtp服务器反弹.

猜你在找的Windows相关文章