我想建立一個PowerShell腳本,收集來自多個系統,然後電子郵件IT系統的正常運行時間的信息。我想格式化電子郵件,以便以合理的方式構建電子郵件,然後添加有關係統的更多信息。格式陣列,包括在電子郵件中使用PowerShell
今天我有這個劇本,低於該收集的系統列表中的正常運行時間的信息,然後通過一個數組sendmail的功能,其中包括電子郵件數組中這些信息。然而目前的產量是這樣的。
Uptime list: Restarted 02/19/2013 04:04:52 MyServer01 Computer Uptime: 23 days and 9 hours Restarted 02/15/2013 04:17:40 MyServer02 Computer Uptime: 27 days and 9 hours
我想有輸出或相當的電子郵件文本更像是:
System | Restarted date/time | Uptime MyServer01 02/17/2013 04:04:52 25 days and 9 hours MyServer02 02/17/2013 04:04:52 25 days and 9 hours
如何設置格式在電子郵件中的文本或做我必須做的,以前我將數組傳遞給電子郵件功能。
腳本本身:
`
function SendMail([string]$arg1){
#SMTP server name
$smtpServer = "mysmtp"
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
#Email structure
$msg.From = "[email protected]"
$msg.ReplyTo = "[email protected]"
$msg.To.Add("[email protected]")
$msg.subject = "System uptime"
$msg.body = ("List og servers and uptime:" + $arg1)
#Sending email
$smtp.Send($msg)}
function CheckUptime($listofservers){
$k [email protected]()
foreach($s in $listofservers){
$Booted = Get-WmiObject -Class Win32_OperatingSystem -Computer $s
$Calc = [DateTime]::Now - $Booted.ConvertToDateTime($Booted.LastBootUpTime)
$k += " Restarted " +
$Booted.ConvertToDateTime($Booted.LastBootUpTime) +
" " + $s + " " +
" Computer Uptime: " +
$Calc.days + " " +
"days and", $Calc.hours + " hours"
}
return $k
}
#Defining list og servers
$listofservers = "MyServer01", "MyServer02"
#Calling function
$message = CheckUptime($serverlist)
SendMail($message)
`