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

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

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

这是代码.

$RGroups = Get-WmiObject -Namespace "root\MicrosoftDFS" -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 "root\MicrosoftDFS" -Query  $RGFoldersWMIQ
$RGConnectionsWMIQ = "SELECT * FROM DfsrConnectionConfig WHERE ReplicationGroupGUID='"+     $Group.ReplicationGroupGUID + "'"
$RGConnections = Get-WmiObject -Namespace "root\MicrosoftDFS" -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服务器反弹.
原文链接:https://www.f2er.com/windows/368786.html

猜你在找的Windows相关文章