我发现这个美妙的脚本,输出当前DFS backlog的状态到powershell控制台。 这很好,但我需要脚本给我发电子邮件,所以我可以安排它每晚运行。 我曾尝试使用Send-MailMessage命令,但无法使其正常工作。 主要是因为我的powershell技能非常薄弱。 我相信大部分的问题都是围绕使用Write-Host命令的脚本进行的。 虽然着色很好,我宁愿它给我发电子邮件的结果。 由于dfs服务器没有电子邮件function,我还需要能够指定邮件服务器的解决scheme。
任何帮助或提示,欢迎和赞赏。
这是代码。
$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 = "[email protected]" $emailTo = "[email protected]" $subject = "DFS Backlog Report" $smtpServer = "MailServer" $smtp = new-object Net.Mail.SmtpClient($smtpServer) $smtp.Send($emailFrom, $emailTo, $subject, $Emailbody)
如果你想从powershell分支出来,你可以将脚本的输出redirect到一个文本文件,并使用像blat这样的第三方命令行邮件发送文本文件作为邮件的正文(或附件),并指定smtp服务器反弹。
这只是一个可能的解决办法; 有许多。
[string]$emailBody = "" $emailBody += "This is line 1<br />" $emailBody += "This is line 2<br />" $emailBody += "This is line 3<br />" Send-MailMessage -From "$senderName <$senderAddr>" -To "$recptName <$recptAddr>" -Subject "$emailSubject" -Body $emailBody -SMTPServer $smtpServer -BodyAsHTML
那有意义吗?
我的解决scheme不是很好,不使用PowerShell,但我已经使用它多年来完成您正在尝试实现。
@echo off set s1=dfsrsrvr1 set s2=dfsrsrv2 set output=%TEMP%\dfsr.txt echo DFS Replication Backlog Report>%OUTPUT% echo.>>%OUTPUT% echo For each DFS replicated share, any backlog is displayed below.>>%OUTPUT% echo The first value is the backlog from %S2% to %S1%, the second value is the reverse>>%OUTPUT% echo.>>%OUTPUT% echo Accounts>>%OUTPUT% echo ========>>%OUTPUT% dfsrdiag backlog /rgname:Accounts /rfname:Accounts /sendingmember:%S2% /receivingmember:%S1% | head -n 2 | tail -n 1 | cut -d: -f2>>%OUTPUT% dfsrdiag backlog /rgname:Accounts /rfname:Accounts /sendingmember:%S1% /receivingmember:%S2% | head -n 2 | tail -n 1 | cut -d: -f2>>%OUTPUT% echo.>>%OUTPUT% blat "%OUTPUT%" -to [email protected] -server mta.example.com -f [email protected] -subject "DFS Replication Report %DATE% %TIME:~0,5%"
它依靠GNU Unix Utils的 head , cut和tail ,以及blat 命令行邮件程序 。
它还使用dfsrdiag实用程序(应该在您的服务器上)从DFSR服务获取所需的统计信息。 在该示例中,复制组名称为“帐户”,根据需要调整/添加更多复制组。
使用Send-MailMessage可能是最简单和最好的方法。
它在早期版本的PowerShell中不可用,所以作为替代,您可以直接使用.NET邮件对象(请参阅http://vblog.strutt.org.uk/2011/09/sending-email-from-powershell/更多信息)。
通过电子邮件发送这种方法的一种方法是将控制台输出redirect到一个文件:
$timeStamp = [DateTime]::Now.ToString("yyyyMMddHHmmss") #configure a timestamp variable $attachFile = c:\temp\attach$timestamp.txt #configure txt file to which we'll redirect the console output. Using the timestamp variable to generate a unique file name Write-Host "Some output here" >> $attachFile
然后使用send-mailmessage使用-Attachments参数…文档在这里:
http://technet.microsoft.com/en-us/library/dd347693.aspx
常见的疑难杂症
我能够与提供的解决scheme提供的几个答案。 关键是你不能将一个写主机写到一个文件中。 我添加了以下代码将输出直接写入电子邮件的正文。 奇迹般有效。
$Emailbody += "$BacklogFileCount files in backlog $SendingMember->$ReceivingMember for $RGName " $emailFrom = "[email protected]" $emailTo = "[email protected]" $subject = "DFS Backlog Report" $smtpServer = "MailServer" $smtp = new-object Net.Mail.SmtpClient($smtpServer) $smtp.Send($emailFrom, $emailTo, $subject, $Emailbody)