여러 시스템에서 시스템 가동 시간 정보를 수집 한 다음 전자 메일로 보내는 powershell 스크립트를 작성하려고합니다. 전자 메일을 형식화하여 합리적인 방법으로 구조화하고 나중에 시스템에 대한 자세한 정보를 추가 할 수 있습니다.배열을 배열하고 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
I 출력 또는 오히려 전자 메일 텍스트가 더 좋아하고 싶은 :
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
I 전자 메일의 텍스트를 포맷하거나 내가 전에 그렇게해야 할 수있는 방법을 배열을 전자 메일 기능에 전달합니다.
스크립트 자체는 :
`
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)
`