我发现了一个很棒的脚本,它可以将当前 DFS 积压的状态输出到 powershell 控制台。这个脚本很好用,但我需要这个脚本给我发电子邮件,这样我就可以安排它在夜间运行。我试过使用 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 = "[email protected]"
$emailTo = "[email protected]"
$subject = "DFS Backlog Report"
$smtpServer = "MailServer"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $Emailbody)
答案1
如果您想要脱离 powershell,您可以将脚本的输出重定向到文本文件,并使用第三方命令行邮件程序(如 blat)将文本文件作为电子邮件的正文(或附件)发送,并指定要反弹的 smtp 服务器。
答案2
这只是其中一种可能的解决方案;还有很多。
[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
那有意义吗?
答案3
我的解决方案并不好,并且没有使用 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%"
它依赖于head
,cut
并且tail
来自GNU Unix 实用程序, 和blat
命令行邮件程序。
它还使用dfsrdiag
实用程序(应该在您的服务器上)从 DFSR 服务获取所需的统计信息。在示例中,复制组名称为 Accounts,请根据需要进行调整/添加更多复制组。
答案4
通过电子邮件发送此信息的一种方法是将控制台输出重定向到文件:
$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
然后使用采用 -Attachments 参数的 send-mailmessage...文档在这里:
http://technet.microsoft.com/en-us/library/dd347693.aspx
常见问题:
- Exchange 中继未配置为允许从运行脚本的框发送邮件
- 反病毒群发邮件蠕虫防护可以阻止脚本电子邮件