1

I have a script on powershell that when it runs it send an email out with server status details and also prints the information on screen

I am looking now to add to the script that aswell as sending the email and prints the information on screen it also saves the results on notepad and saves it in a folder on the shared drive

I am new to powershell so not sure how i go about doing that. I can add my code if needed

thanks

os

the code is


$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 = "osman.test.farooq@gmail.com"
$recipients = ("osman.farooq@atos.net")
$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

2 Answers2

2

There are many approaches to writing files in PowerShell. Which one you use is entirely up to you!

  • Use the Set-Content command
  • Use the Out-File command
  • Use the [System.IO.File]::WriteAllText() method

As Mathias mentions in the comments, the Tee-Object command can be used to write to a file and simultaneously send it down the pipeline, or write the contents to a variable. There are probably even more approaches as well.

2

Trevor's answer covers writing the file pretty well. Once the file is written, you could open it in the default application using Invoke-Item:

$myPath = 'C:\my\file.txt'
$information | Set-Content -Path $myPath
$mypath | Invoke-Item
briantist
  • 45,546
  • 6
  • 82
  • 127
  • Hi, this worked but it didnt copy the results from the email over to the notpad. Just brings up and empty document – Osman Farooq Jul 14 '16 at 15:18
  • @OsmanFarooq You need to edit your code into your question. We can't debug code we can't see. – briantist Jul 14 '16 at 15:22
  • @OsmanFarooq thank you for adding code, but this is a bit long, and I don't see your use of `Invoke-Item` anywhere. Please see [ask] and try to provide a [mcve]. – briantist Jul 14 '16 at 16:33