2016-07-14 35 views
1

Ich habe ein Skript auf Powershell, dass wenn es ausgeführt wird, senden Sie eine E-Mail mit Serverstatusdetails und druckt auch die Informationen aufauf Powershell wie Skript als eine Textdatei in einem freigegebenen Ordner sowie Drucken auf dem Bildschirm speichern und E-Mail senden

Bildschirm

ich jetzt bin auf der Suche, um das Skript hinzuzufügen, das als das Senden der E-Mail-aswell und druckt die Informationen auf dem Bildschirm speichert sie auch die Ergebnisse auf Notizblock und speichert sie in einem Ordner auf dem freigegebenen Laufwerk

ich bin neu Powershell so nicht sicher, wie ich das mache. Ich kann meinen Code hinzufügen, wenn nötig

dank

os

der Code


$DirectoryPath = Split-Path $MyInvocation.MyCommand.Path 
$ConfigurationPath = $DirectoryPath + '\Config file.xml' 

Function FormatCell 
{ 
param($cellValue) 

$cell = "<td>" + $cellValue + "</td>" 
return $cell 
} 


Function Getservicechecker 
{ 

[xml]$ConfigFile = Get-Content $ConfigurationPath 

$Servers = $ConfigFile.SelectNodes('/Configs/Servers/Server') 

$ServString = "<tr><th>Server</th><th>IP Address</th><th>Process</th>  <th>Status</th></tr>" 
foreach($Server in $Servers) 
{ 
    [string]$serverName = $Server.ServerName 
    $OutputText += "Server: " + $serverName + $NL 
    $Process = $Server.ProcessesToMonitor 

    foreach($Processtomonitor in $Process.ChildNodes) 
    { 
     $ServString += "<tr>" 
     $ServString += FormatCell -cellValue $serverName 
     $ipaddress = Test-Connection $serverName -count 1 | select  Ipv4Address 
     $ipAddressValue = $ipaddress.IPV4Address.IPAddressToString     
     $servString += FormatCell -cellValue $ipAddressValue 
     [string]$processName = $Processtomonitor.InnerText 
     $servicestatus = Get-service -ComputerName $serverName -Name $processName | select status 
     $ServString += FormatCell -cellValue $processName 

     $FormatedStatus = 'status' 
     [string]$statusString = -join $servicestatus 
     $statusString = $statusString.Remove(0,"@{Status=".Length) 
     $statusString = $statusString.Remove($statusString.IndexOf("}")) 
     $FormatedServiceStatus = FormatCell -cellValue $servicestatus 
    If ($FormatedServiceStatus –eq “<td>@{Status=Running}</td>”) 
    { 
     $Formatedstatus = 'Running' 
    } 
    elseif ($FormatedServiceStatus –eq “<td>@{Status=Stopped}</td>”) 

    { 
     $Formatedstatus = "<p style='color:red'>Service stopped  investigation required</p>" 
    } 

    else 
    { 
     $FormatedStatus = "$servicestatus potential investigation" 
    } 

Write-host "server: $serverName `t ipaddress: $ipAddressValue `t process:  $processName `t status: $statusString" 


     $ServString += FormatCell -cellValue $Formatedstatus 
     $ServString += "</tr>" 
    } 

} 

     return "<table class=""gridtable"">" + $ServString + "</table>" 
} 




function SendMail 
{ 
param($bodyText, $subject) 


$SmtpServer = "164.134.84.81" 
$emailMessage = New-Object System.Net.Mail.MailMessage 
$fromaddress = "[email protected]" 
$recipients = ("[email protected]") 
$Subject = "BOXI Servers Report for " +$dateTimeOfServiceCheck 



$body = "<HTML><HEAD><META http-equiv=""Content-Type"" content=""text/html; charset=iso-8859-1"" /><TITLE></TITLE>" 
$body += "<style type=""text/css"">table.gridtable {font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border-width: 1px;border-color: #666666;border-collapse: collapse;}" 
$body += "table.gridtable th {border-width: 1px;padding: 8px;border-style: solid;border-color: #666666; background-color: #dedede;}" 
$body += "table.gridtable td {border-width: 1px;padding: 8px;border-style: solid;border-color: #666666; background-color: #ffffff;}</style></HEAD>" 
    $body += "<BODY bgcolor=""#FFFFFF"" style=""font-size: Small; font-family: TAHOMA; color: #000000"">" 
$body += $bodyText 

Send-MailMessage -to $recipients -subject $subject -bodyashtml -body $body - from $fromAddress -SmtpServer $smtpServer -Port 25 

} 

function main 
{ 
$dateTimeOfServiceCheck = Get-Date -Format F 
$outputText = "Service Checker :" + $dateTimeOfServiceCheck 

$emailBody = "<h3>"+$outputText +"</h3>" #take out if dont want datetime  above table 
$emailBody += Getservicechecker 

SendMail $emailBody "BOXI Servers Report for" +$dateTimeOfServiceCheck 


} 

main 
+1

öffnen. Bitte fügen Sie Ihren Code hinzu; es wird einfacher sein, die Frage in der Weise zu beantworten, die Ihnen dabei helfen wird. – Persistent13

Antwort

2

Es gibt viele Ansätze, um Dateien in Powershell zu schreiben. Welche Sie verwenden, liegt ganz bei Ihnen!

  • Verwenden Sie den Set-Content Befehl
  • Verwenden Sie den Out-File Befehl
  • Verwenden Sie die [System.IO.File]::WriteAllText() Methode

Als Mathias in den Kommentaren erwähnt, kann der Tee-Object Befehl verwendet werden, um eine Datei zu schreiben und gleichzeitig sende es in die Pipeline oder schreibe den Inhalt in eine Variable. Es gibt wahrscheinlich noch mehr Ansätze.

+2

Ich finde, dass "Tee-Object" in diesem Zusammenhang eine lobende Erwähnung verdient –

+1

Umleitung? '>' und '>>' :) – briantist

+0

Ja :) Das ist ein weiterer guter! –

2

Trevors Antwort umfasst das Schreiben der Datei ziemlich gut. Sobald die Datei geschrieben ist, können Sie sie in der Standardanwendung unter Verwendung von Invoke-Item:

+0

Hallo, das hat funktioniert, aber es hat die Ergebnisse von der E-Mail nicht auf das Notepad kopiert. Zeigt nur ein leeres Dokument –

+0

@OsmanFarooq Sie müssen Ihren Code in Ihre Frage bearbeiten. Wir können keinen Code debuggen, den wir nicht sehen können. – briantist

+0

Code wurde hinzugefügt –