Issue with e-mail loop

This topic contains 1 reply, has 2 voices, and was last updated by  postanote 3 months ago.

  • Author
    Posts
  • #94423

    Venkata Kalyan
    Participant

    Hi,
    I have two log files (printsmith.log, printsmith.log.1). The below script is to monitor if aggregation job is completed sucessfully or not.

    Script is working fine without any issue (it gives me the output in the console). I am facing issue, writing the output to a file and send the email.

    > Action1 must send an email, if the Aggregation did not start for the date.
    > Action2 must send an email, if Aggregation did not complete yet for the date.
    > Action3 must send an email, if Aggregation completed successfully.

    I am facing issues looping the e-mail part. Can someone help me on this. I marked bold in the script, where I am facing issues.

    #declare variables
    
    $logsourcepath = 'C:\Program Files (x86)\EFI\PrintSmith\PrintSmith Vision Server\logs\'
    $logpath = 'C:\opscripts\Kalyan\Vision_Data_Aggregation_Daily_Check\log\'
    $logfile1 = 'printsmith.log'
    $logfile2 = 'printsmith.log.1'
    $logfile1path = $logpath + $logfile1
    $logfile2path = $logpath + $logfile2
    $logfile1sourcepath = $logsourcepath + $logfile1
    $logfile2sourcepath = $logsourcepath + $logfile2
    $previous_date = (Get-Date).AddDays(-1) 
    $agg_start_date = (Get-Date $previous_date -Format 'yyyy-MM-dd')
    $agg_end_date = (Get-Date -Format 'yyyy-MM-dd')
    
    #copy latest log files and delete existing ones
    
    if ((Test-Path $logfile1path) -and (Test-Path $logfile2path)) {
        rm -Force $logfile1path
        rm -Force $logfile2path
    }
    
    cp $logfile1sourcepath $logpath
    cp $logfile2sourcepath $logpath
    
    ### MAIN FUNCTION ###
    
    #search printsmith.log to find start of aggregation
    cd C:\opscripts\Kalyan\Vision_Data_Aggregation_Daily_Check
    
    #find if aggregation started or not
    $agg_start = (.\sed.exe -n -e "/^ERROR $agg_start_date 22:30:00/p" $logfile1path)
    if ($agg_start -lt 1) {
        $agg_start = (.\sed.exe -n -e "/^ERROR $agg_start_date 22:30:00/p" $logfile2path)
        if ($agg_start -lt 1) {
            Write-Host -ForegroundColor Red "ERROR: Server01 Aggregation did not start for the date: $agg_start_date"
        }
        elseif ($agg_start | Select-String "Data Aggregation Trigger start") {
            Write-Host -ForegroundColor Green "SUCCESS: Server01 Aggregation started successfully for the date: $agg_start_date which is recorded in the log file: $logfile2"
            Write-Host "EXTRACT FROM LOG FILE:" $agg_start[0]
        }
        else {
            Write-Host -ForegroundColor Red "ERROR: Server01 Aggregation did not start for the date: $agg_start_date"    
        }
    }
    elseif ($agg_start | Select-String "Data Aggregation Trigger start") {
        Write-Host -ForegroundColor Green "SUCCESS: Server01 Aggregation started successfully for the date: $agg_start_date which is recorded in the log file: $logfile1"
        Write-Host "EXTRACT FROM LOG FILE:" $agg_start[0]
    }
    else {
        Write-Host -ForegroundColor Red "ERROR: Server01 Aggregation did not start for the date: $agg_start_date"    
    }
    
    #send mail and terminate script if aggregation did not start
    
    #find if aggregation completed
    
    if ($agg_start | Select-String "Data Aggregation Trigger start") {
        $agg_end = (./sed.exe -n -e "/End Time - $agg_end_date/p" $logfile1path)
        if ($agg_end -lt 1) {
           $agg_end = (./sed.exe -n -e "/End Time - $agg_end_date/p" $logfile2path)
           if ($agg_end -lt 1) {
               Write-Host -ForegroundColor Yellow "WARNING: Server01 Aggregation did not complete yet for the date: $agg_start_date. Kindly investigate!!!"
           }
           else {
               Write-Host -ForegroundColor Green "SUCCESS: Server01 Aggregation completed successfully and is recorded in the log file: $logfile2"
               $logline = $agg_end[0]
               Write-Host "EXTRACT FROM LOG FILE:" $logline
               Write-host "`n FINAL OUTPUT:"
               $final_output = (gc $logfile2path | Select-String $logline -Context 2,5 | Select-Object -First 1)
               Write-Host $final_output
           }
        }
        else {
           Write-Host -ForegroundColor Green "SUCCESS: Server01 Aggregation completed successfully and is recorded in the log file: $logfile1"
           $logline = $agg_end[0]
           Write-Host "EXTRACT FROM LOG FILE:" $logline
           Write-host "`n FINAL OUTPUT:"
           $final_output = (gc $logfile1path | Select-String $logline -Context 2,5 | Select-Object -First 1)
           Write-Host $final_output
        }
    }
    
    #send final mail based on result from above
    
    

    -Kalyan

  • #94453

    postanote
    Participant

    Since you are trying to create a file. You should be looking at using these two cmdlets / redirect options

            # Get parameters, examples, full and Online help for a cmdlet or function
    
            (Get-Command -Name New-Item).Parameters
            Get-help -Name New-Item -Examples
            Get-help -Name New-Item -Full
            Get-help -Name New-Item -Online
    
    
            (Get-Command -Name Write-Output).Parameters
            Get-help -Name Write-Output-Examples
            Get-help -Name Write-Output-Full
            Get-help -Name Write-Output-Online
    
            Get-Help about_*
            Get-Help about_Redirect
    
            # All Help topics locations
            explorer "$pshome\$($Host.CurrentCulture.Name)"
        

    Depending on what version of PoSH you are on. Avoid any use of Write-Host for anything other than colorized text (as you are doing at some places in your code) and some custom formatting scenarios, because it will clear the buffer, thus no pipeline data to send down the pipe. Also, if you ever fully automate this. Write-Host is moot as there is no one sitting at the screen to see it. It's better to log to a file that you can review later, or use the transcript cmdlets.

            (Get-Command -Name Start-Transcript).Parameters
            Get-help -Name Start-Transcript -Examples
            Get-help -Name Start-Transcript -Full
            Get-help -Name Start-Transcript -Online
    
            (Get-Command -Name Stop-Transcript).Parameters
            Get-help -Name Stop-Transcript-Examples
            Get-help -Name Stop-Transcript-Full
            Get-help -Name Stop-Transcript-Online
        

    You don't need Write-Host for writing to the screen. All of the following will output to the screen. No Write-* required.

        
            ($Var = 'Hello World')
            Hello World
    
            $Var
            Hello World
    
            "$Var"
            Hello World
     
        

    If you want to use built-in color output, then these two Write cmdlets can help, well, that is if you like the color they leverage.

            Write-Warning -Message ($Var = 'Hello World')
            Write-Warning -Message $Var
            Write-Warning -Message "$Var"
        

    Or

            # Store current default verbose setting
            ($DefaultVerboseSetting = $VerbosePreference )
    
            # Define the setting you want
            $VerbosePreference = "Continue"
            Write-Verbose ($Var = 'Hello World')
            Write-Verbose $Var
            Write-Verbose "$Var"
    
            # Reset default
            $VerbosePreference = $DefaultVerboseSetting 
        

    Using Write-Host has been a hotly debated topic for a long while. Even by the inventor / author of PowerShell.

    Write-Host Considered Harmful – by PowerShell founder Jeffrey Snover

    Start-Process 'http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful'

    When you are writing or reviewing PowerShell scripts, I'd like you to remember
    the following rule of thumb:

    ◾ Using Write-Host is almost always wrong.

    Write-Host is almost always the wrong thing to do because it interferes with
    automation. There are typically two reasons why people use Write-Host:

    Lot's of other articles exist on the topic. In earlier versions of PoSH,
    Write-Host could not be used in pipeline, as the moment yo use it the data is
    gone from the buffer.

    However, in PoSHv5 Jeffrey Snover now says...

    With PowerShell v5 Write-Host no longer "kills puppies". data is captured into
    info stream

    Start-Process 'https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Write-Information?view=powershell-5.1'

    Description

    The Write-Information cmdlet specifies how Windows PowerShell handles information
    stream data for a command.

    Windows PowerShell 5.0 introduces a new, structured information stream (number
    6 in Windows PowerShell streams) that you can use to transmit structured data
    between a script and its callers (or hosting environment). Write-Information
    lets you add an informational message to the stream, and specify how Windows
    PowerShell handles information stream data for a command.

    You can still use colors, without using Write host, by doing this ...

    PowerTip: Write PowerShell Output in Color Without Using Write-Host

    Start-Process 'https://blogs.technet.microsoft.com/heyscriptingguy/2012/10/11/powertip-write-powershell-output-in-color-without-using-write-host'

    Summary: Write colorized output to the Windows PowerShell console without using the Write-Host cmdlet.

You must be logged in to reply to this topic.