none
Can Powershell do this? RRS feed

  • Question

  • I have a powershell script that checks outlook results, now it reports back the following line of code

    Customer=Backup Exec Alert: Job Cancellation (Server: "SERVER A") (Job: "Daily Backup")

    Is it possible to grab the results of anything that is within the quotes of (Server: "SERVER A") and put it in a variable?, so as it goes through multiple lines you might have SERVER B, SERVER C but i just want to put those results in a line.

    Let me know if you want me to explain it anymore.

    Wednesday, February 19, 2014 1:34 PM

Answers

  • It reads like you're taking the $sucsess and $failure messages and putting them into an array of strings at the moment.

    You then feed them into the Write-Host commandlet which will print the list.

    In your table you only have a single line for the results, which I think will be printing out all those values in a single table row. If you want one row per result then you'd need to use a foreach loop or something similar.

    foreach($itemToPrint in $Success)
    {
    Add-Content $IndexPage "<tr><td>$Customer</td><td>$itemToPrint </td><td>$Failed</td><td></td></tr>"
    }

    Of course that doesn't seem to be what you're after, if you want to have a customer row with a list of servers that failed or sucseeded you'd need to re-structure your script a lot. You'd have to create a customer object with a name and two lists/arrays (one for sucseeded and one for failed).

    I'd start by adjusting your script so that it just prints out two tables, one for failed and one for sucseeded servers. That's a small change and gives you something that works.

    • Marked as answer by Alex Young Sys Wednesday, February 19, 2014 3:31 PM
    Wednesday, February 19, 2014 2:45 PM

All replies

  • Function Get-OutlookInBox   {
     Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
    
     $olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
    
     $outlook = new-object -comobject outlook.application
    
     $namespace = $outlook.GetNameSpace("MAPI")
    
     $folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)
    
     $folder.items |
    
     Select-Object -Property Subject, ReceivedTime, SenderName, SenderEmailAddress
    
    } #end function Get-OutlookInbox
    Function writeHtmlHeader    {
    param($fileName)
    add-Content $fileName "<html>"
    add-Content $fileName "<head>"
    add-content $fileName "<title>Backup Reports</title>"
    add-Content $fileName "</head>"
    add-Content $fileName "<body>"
    } # HTML Header
    Function writeHtmlFooter    {
    param($fileName)
    add-Content $fileName "</body>"
    add-Content $fileName "</html>"
    } # HTML Footer
    cls # Clear The Screen
    
    # Job Success
    if (Get-OutlookInBox -match { $_.subject -match 'Job Success'})
    {
    $Success = Get-OutlookInBox $inbox | Where-Object { $_.subject -match 'Job Success'} | select @{Name="Customer"; Expression={$_.subject}}
    Write-Host -ForegroundColor Green $Success
    
    }
    # Job Failed
    if (Get-OutlookInBox -match { $_.subject -match 'Job Cancellation'})
    {
    $Failed = Get-OutlookInBox $inbox | Where-Object { $_.subject -match 'Job Cancellation'} | Select @{Name="Customer"; Expression={$_.subject}}
    Write-Host -ForegroundColor Red $Failed
    
    }
    # All Else
    else {
    
    Write-Host "Unknown - Backup Job Did Not Report"
    
    }
    
    $IndexPage = "C:\BackupReport\Report.html" # This Creates The Index Page
    New-Item -ItemType file $IndexPage -Force # Creates The Page
    
    writeHtmlHeader($IndexPage) # Add Header to Index Page
    
    
    # variables
    
    $Date = Get-Date -Format "dddd dd MMMM yyyy" # Display Date
    $Time = Get-Date -Format "hh:mm:ss" # Display Time
    
    Add-Content $IndexPage "<table width='auto' align='center' border-collapse='collapse'>"
    Add-Content $IndexPage "<tr bgcolor='#D0E6FF'>"
    Add-Content $IndexPage "<td colspan='4' height='25' align='center'>"
    Add-Content $IndexPage "<font face='tahoma' color='#000000' size='2'><b>Backup Reports</b></font><br/>"
    Add-Content $IndexPage "<font face='Arial' color='#000000' size='2'><b>Date: $Date</b></font><br/>"
    Add-Content $IndexPage "<font face='Arial' color='#00000' size='2'><b>Time: $Time</b></font>"
    Add-Content $IndexPage "</td>"
    Add-Content $IndexPage "</tr>"
    Add-Content $IndexPage "</table>"
    
    Add-Content $IndexPage '<div class="BackupReport">'
    Add-Content $IndexPage '<table id="main" width="auto" align="center">'
    Add-Content $IndexPage "<tr bgcolor='#d0e6ff'>"
    Add-Content $IndexPage "<td width='auto' align='center'>Customer</td>"
    Add-Content $IndexPage "<td width='auto' align='center'>Succesful</td>"
    Add-Content $IndexPage "<td width='auto' align='center'>Failed</td>"
    Add-Content $IndexPage "<td width='auto' align='center'>Unknown</td>"
    Add-Content $IndexPage "</tr>"
    Add-Content $IndexPage "</div>"
    
    Add-Content $IndexPage "<tr><td>$Customer</td><td>$Success</td><td>$Failed</td><td></td></tr>"
    
    Add-Content $IndexPage "</table>"
    writeHtmlFooter($IndexPage)

    Wednesday, February 19, 2014 1:42 PM
  • $resultString = 'Customer=Backup Exec Alert: Job Cancellation (Server: "SERVER A") (Job: "Daily Backup")'
    $splitResults = $resultString.split('"')
    $splitResults[1]

    You can use the .split('"') method to break your string into an array with the double quotation mark as the split term. You can then index into that array to get the second result.

    There's a few things you should look though. Firstly there's the ConverTo-HTML commandlet. that can help you make much slicker html reports in a much more sustainable manner. Don Jones has an excellent free ebook on how to take it even further somewhere.

    The reason i say this is it helps eliminate manual, error prone, html mistakes, such as your <div> that doesn't enclose the full backupreport table....

    Wednesday, February 19, 2014 2:21 PM
  • Yeah i think i'm going to have to, the code i've got (bare in mind i'm still learning) it reports in the console the emails but does not output each email line, is there something i'm missing or should look at?, so for each email it creates a new row.
    Wednesday, February 19, 2014 2:30 PM
  • It reads like you're taking the $sucsess and $failure messages and putting them into an array of strings at the moment.

    You then feed them into the Write-Host commandlet which will print the list.

    In your table you only have a single line for the results, which I think will be printing out all those values in a single table row. If you want one row per result then you'd need to use a foreach loop or something similar.

    foreach($itemToPrint in $Success)
    {
    Add-Content $IndexPage "<tr><td>$Customer</td><td>$itemToPrint </td><td>$Failed</td><td></td></tr>"
    }

    Of course that doesn't seem to be what you're after, if you want to have a customer row with a list of servers that failed or sucseeded you'd need to re-structure your script a lot. You'd have to create a customer object with a name and two lists/arrays (one for sucseeded and one for failed).

    I'd start by adjusting your script so that it just prints out two tables, one for failed and one for sucseeded servers. That's a small change and gives you something that works.

    • Marked as answer by Alex Young Sys Wednesday, February 19, 2014 3:31 PM
    Wednesday, February 19, 2014 2:45 PM
  • Yeah i'll give it a go i'm just reading that Don Jones stuff and it's quite good to follow so might just start playing around with that - thanks for your help though much appreciated.
    Wednesday, February 19, 2014 3:31 PM