Hi,
i've got a script that scans our backup reports in the public folders and outputs the results to a html file, that bit works fine. I want to expand the results to start putting results into a SQL Database the only problem it's not working lol, can someone look at the code and point me in the right direction.
Cheers
Function Get-OutlookInBox {
$OutlookApp = new-object -com outlook.application
$MAPI = $OutlookApp.getNamespace("MAPI")
$Folders = $MAPI.Session.GetDefaultFolder(18)
$Folders.folders.Item("Backup Reports").items | Select-Object -Property Subject, ReceivedTime, SenderName, SenderEmailAddress
} # Go into Mailbox & Access
cls # Clear The Screen
$DateToCompare = (Get-date).AddDays(-1) # Date To Compare
# Job Success
if (Get-OutlookInBox -match { $_.subject -match 'Job Success'})
{
$Success = Get-OutlookInBox $inbox | Where-Object { $_.subject -match 'Job Success' -or $_.subject -match 'Job Completed with Exceptions'} | Where-Object {$_.ReceivedTime -gt $DateToCompare} | select @{Name="Customer"; Expression={$_.subject}}, SenderName, ReceivedTime | Sort-Object SenderName
Write-Host -ForegroundColor Green $Success.SenderName
}
# Job Failed
if (Get-OutlookInBox -match { $_.subject -match 'Job Cancellation' -and $_.subject -match 'Job Failed'} )
{
$Failed = Get-OutlookInBox $inbox | Where-Object { $_.subject -match 'Job Cancellation' -or $_.subject -match 'Job Failed'} | Where-Object {$_.ReceivedTime -gt $DateToCompare} | Select @{Name="Customer"; Expression={$_.subject}}, SenderName, ReceivedTime | Sort-Object SenderName
Write-Host -ForegroundColor Red $Failed.SenderName
}
# Any Errors Encountered
else {
Write-Host "Error Detected - Please Try Again...."
}
Function SuccessBackups {
$Success
} # Container for Success Backups
Function FailedBackups {
$Failed
} # Container for Failed Backups
$HTMLHeader = @"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"><html><head><title>Backup Report</title><style type="text/css"><!--
body
{
font-family: Tahoma;
background: White;
}
#report
{
width: 1000px;
align:center;
}
table
{
border-collapse: collapse;
border: none;
Font: 10pt Verdana, Geneva, Arial, Helvetica, sans-serif;
margin-bottom: 5px;
}
table td
{
font-size: 10px;
padding-left: 0px;
padding-right: 0px;
text-align: center;
border-top: 0.1px solid #aabcfe;
border-bottom: 0.1px solid #aabcfe;
}
table th
{
font-size: 12px;
font-weight: bold;
font-family: Tahoma;
padding-left: 0px;
padding-right: 0px;
text-align: center;
color:blue
}
table tr
{
font-size: 11px;
background: white;
padding-left: 0px;
padding-right: 0px;
text-align: center;
}
h3
{
clear: both;
font-size: 115%;
margin-left: 20px;
margin-top: 30px;
border-bottom: 1px solid #fff;
color: red;
text-align:center
}
p {
margin-left: 20px;
font-size: 12px;
border-bottom: 1px solid #fff;
color: blue;
}
table.imagetable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
}
table.imagetable th {
background:#b5cfd2 url('cell-blue.jpg');
border-width: 1px;
border-style: solid;
border-color: #999999;
}
table.imagetable td {
background:#dcddc0 url('cell-grey.jpg');
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #999999;
}
--></style></head><body>"@
Function BackupReports {
$SucessReport = SuccessBackups | Select-Object SenderName, Customer, ReceivedTime | ConvertTo-Html -Fragment
$FailedReport = FailedBackups | Select-Object SenderName, Customer, ReceivedTime | ConvertTo-Html -Fragment
}
$HTMLMiddle = @"<div id="report"><h3>NSM - Backup Report</h3><table class="imagetable"><BR><tr><th>Successful Backups</th><td><font color="green">$SucessReport</font></td></tr><BR><tr><th>Failed Backups</th><td><font color="red">$FailedReport</font></td></tr><BR></table></div>"@
$HTMLEnd = @"</div></body></html>"@
$HTMLmessage = $HTMLHeader + $HTMLMiddle + $HTMLEnd
# Save the report out to a file in the current path
$HTMLmessage | Out-File "C:\scripts\report.html"
# SQL Database Input
function BackupSQLReport ([Data.SqlClient.SqlConnection] $OpenSQLConnection) {
$sqlCommand = New-Object System.Data.SqlClient.SqlCommand
$sqlCommand.Connection = $sqlConnection
# This SQL query will insert 1 row based on the parameters, and then will return the ID
# field of the row that was inserted.
$sqlCommand.CommandText = "SET NOCOUNT ON; " +"INSERT INTO dbo.BackupReports (SenderName,Customer,ReceivedTime) " +"VALUES (@SenderName,@Customer,@ReceivedTime); " +"SELECT SCOPE_IDENTITY() as [InsertedID]; "
$sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@SenderName",[Data.SQLDBType]::NVarChar, 245))) | Out-Null
$sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@Customer",[Data.SQLDBType]::NVarChar, 260))) | Out-Null
$sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@ReceivedTime",[Data.SQLDBType]::DateTime2))) | Out-Null
foreach ($file in $folders) {
# Here we set the values of the pre-existing parameters based on the $file iterator
$sqlCommand.Parameters[0].Value = $file.SenderName
$sqlCommand.Parameters[1].Value = $file.Customer
$sqlCommand.Parameters[2].Value = $file.ReceivedTime
# Run the query and get the scope ID back into $InsertedID
$InsertedID = $sqlCommand.ExecuteScalar()
# Write to the console."Inserted row ID $InsertedID for file " + $file.SenderName
}
}
# Open SQL connection (you have to change these variables)
$DBServer = "Marky-PC\SQLExpress"
$DBName = "DiskSpace"
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "Server=$DBServer;Database=$DBName;Integrated Security=True;"
$sqlConnection.Open()
# Quit if the SQL connection didn't open properly.
if ($sqlConnection.State -ne [Data.ConnectionState]::Open) {"Connection to DB is not open."
Exit
}
# Call the function that does the inserts.
BackupSQLReport($sqlConnection)
# Close the connection.
if ($sqlConnection.State -eq [Data.ConnectionState]::Open) {
$sqlConnection.Close()
}