2013-03-14 44 views
-1

我想建立一個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) 

`

回答

1

首先,它表明,當你的樣品甚至沒有工作示例舉手之勞。可變錯字等

作爲一種解決方案,爲什麼不創建包含每個服務器並將其輸出作爲一個陣列中的數據的自定義對象?喜歡的東西:

PS > function CheckUptime($listofservers){ 
    $res = @() 

    foreach($s in $listofservers){ 
     $boot = Get-WmiObject win32_operatingsystem -ComputerName $s | % { $_.ConvertToDateTime($_.LastBootUpTime) } 
     $uptime = [DateTime]::Now - $boot 
     $res += New-Object psobject -Property @{ 
      Server = (Get-Culture).TextInfo.ToTitleCase($s) 
      BootTime = $boot 
      Uptime = "{0} days and {1} hours" -f $uptime.Days, $uptime.Hours 
     } 
    } 

    #Output 
    $res 
} 

$myservers = "localhost", "localhost" 

$str = "List of servers and uptime:`r`n" 

$str += CheckUptime $myservers | Out-String 

PS > $str 
List of servers and uptime: 

Server       Uptime       BootTime 
------       ------       -------- 
Localhost       8 days and 6 hours    06.03.2013 09:33:37 
Localhost       8 days and 6 hours    06.03.2013 09:33:37 
1

使用Format-Table格式化如所期望的數據,並通過管道將進入Out-String轉換成含有格式化的數據的單個串。

然後追加,爲您的電子郵件內容。

這依賴於數據收集替換建設有構建定製對象,具有對每個不同的列的單獨字段單個字符串。

如(不完整的,但應該給你的想法)

$serverData = $serverList | Foreach-Object { GetServerData $_ } | 
    Format-Table Name, 
       @(l='Boot Time'; e={$_.BootTime.ToLocalTime()}}, 
       @(l='Uptime'; e={FormatUptime([DateTime]::UtcNow - $_.BootTime}} | 
    Out-String 

$emailBody += $serverData 

其中引用的函數已經定義,沿着線:

function GetServerData { 
    params([string]$name) 

    $os = Get-WmiObject -Class Win32_OperatingSystem -Computer $name 
    $bootTimeLocal = $os.ConvertToDateTime($os.LastBootUpTime) 

    new-object PSObject -property @{ 
    Name = $name; 
    # Keep everything in UTC to avoid confusion in calcs, 
    # TODO FIX: Use the remote machine's timezone for this.... 
    BootTime = $bootTimeLocal.ToUniversalTime() 
    } 
} 

function FormatUptime { 
    params([TimeSpan]$time) 

    ... 
} 

總結:

  • 使用Format-* cmdlet進行格式化,如果沒有立即傳遞給輸出(並且默認Out-Default追加到每個管道的末尾而沒有Out-*),然後轉換爲一個字符串Out-String
  • 儘可能長地保持數據類型的本地類型。直到必須轉換爲人類可讀形式。因此信息不會丟失。
  • 。利用自定義對象和屬性(尤其是NoteProperties)來創建自己的對象「類型」,以保持相關信息一起。
相關問題