加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 00:27:44 所属栏目:Windows 来源:网络整理
导读:我找到了这个很棒的脚本,它将当前DFS backlog的状态输出到power shell控制台.这很好用,但我需要脚本给我发电子邮件,所以我可以安排它每晚运行.我已尝试使用Send-MailMessage命令,但无法使其工作.主要是因为我的powershell技能非常弱.我相信大多数问题都围绕
我找到了这个很棒的脚本,它将当前DFS backlog的状态输出到power shell控制台.这很好用,但我需要脚本给我发电子邮件,所以我可以安排它每晚运行.我已尝试使用Send-MailMessage命令,但无法使其工作.主要是因为我的powershell技能非常弱.我相信大多数问题都围绕着使用Write-Host命令的脚本.虽然着色很好,但我宁愿让它给我发送结果.我还需要能够指定邮件服务器的解决方案,因为dfs服务器没有电子邮件功能.

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

这是代码.

$RGroups = Get-WmiObject -Namespace "rootMicrosoftDFS" -Query "SELECT * FROM DfsrReplicationGroupConfig"
$ComputerName=$env:ComputerName
$Succ=0
$Warn=0
$Err=0

foreach ($Group in $RGroups)
{
$RGFoldersWMIQ = "SELECT * FROM DfsrReplicatedFolderConfig WHERE     ReplicationGroupGUID='" + $Group.ReplicationGroupGUID + "'"
$RGFolders = Get-WmiObject -Namespace "rootMicrosoftDFS" -Query  $RGFoldersWMIQ
$RGConnectionsWMIQ = "SELECT * FROM DfsrConnectionConfig WHERE ReplicationGroupGUID='"+     $Group.ReplicationGroupGUID + "'"
$RGConnections = Get-WmiObject -Namespace "rootMicrosoftDFS" -Query  $RGConnectionsWMIQ
foreach ($Connection in $RGConnections)
{
$ConnectionName = $Connection.PartnerName.Trim()
if ($Connection.Enabled -eq $True)
{
if (((New-Object System.Net.NetworkInformation.ping).send("$ConnectionName")).Status -eq "Success")
{
foreach ($Folder in $RGFolders)
{
$RGName = $Group.ReplicationGroupName
$RFName = $Folder.ReplicatedFolderName

if ($Connection.Inbound -eq $True)
{
$SendingMember = $ConnectionName
$ReceivingMember = $ComputerName
$Direction="inbound"
}
else
{
$SendingMember = $ComputerName
$ReceivingMember = $ConnectionName
$Direction="outbound"
}

$BLCommand = "dfsrdiag Backlog /RGName:'" + $RGName + "' /RFName:'" + $RFName + "' /SendingMember:" + $SendingMember + " /ReceivingMember:" + $ReceivingMember
$Backlog = Invoke-Expression -Command $BLCommand

$BackLogFilecount = 0
foreach ($item in $Backlog)
{
if ($item -ilike "*Backlog File count*")
{
$BacklogFileCount = [int]$Item.Split(":")[1].Trim()
}
}

$Emailbody += "$BacklogFileCount files in backlog $SendingMember->$ReceivingMember for $RGName
"

} # Closing iterate through all folders
} # Closing  If replies to ping
} # Closing  If Connection enabled
} # Closing iteration through all connections
} # Closing iteration through all groups

$emailFrom = "sender@foobar.com"
$emailTo = "recipient@foobar.com"
$subject = "DFS Backlog Report"
$smtpServer = "MailServer"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom,$emailTo,$subject,$Emailbody)
如果你想分支出PowerShell,你可以将脚本的输出重定向到文本文件并使用第三方命令行邮件程序如blat将文本文件作为电子邮件的正文(或附件)发送并指定smtp服务器反弹.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读