Tuesday 24 August 2021

Calculate Time to Run the Script


 $starttime = (Get-Date)
    get-mailox

$endtime = (Get-Date)
Write-Host "`n`nTotal Time : $(($endtime-$starttime).totalseconds) Seconds"

Friday 20 August 2021

Try Catch and Send Email

try
{
 It will generate error
}
catch
{
    Write-host $_.Exception.Message -ForegroundColor Yellow
    Write-host $_.Exception.ItemName -ForegroundColor Green
    $ErrorMessage = $_.Exception.Message 
    $FailedItem = $_.Exception.ItemName
    $cred = Get-Credential
    Send-MailMessage -From Sender@domain.com -To Recipient@domain.com -Subject "Script Failed!" -SmtpServer smtp.office365.com -Body "We failed to read file $FailedItem. The error message was $ErrorMessage" -Port 587 -Credential $cred -BodyAsHtml -UseSsl 
    Continue
}
 

Convert CSV to Excel

# Collect all CSV name in a TXT File
(get-childitem "C:\SUMANT\test").FullName > C:\SUMANT\CSVName.txt

(gc "C:\SUMANT\CSVName.txt") | foreach-object{
    [string]$csv = $_
    
    $xlsx = ($csv).replace("csv","xlsx")
    $xl = New-Object -ComObject "Excel.Application"
    $xl.Visible=$False
    $wb = $xl.Workbooks.Open($csv)
    $wb.SaveAs($xlsx, 51)
    $xl.Quit()

Thursday 19 August 2021

Email Queue Report

 Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010;

$Output = $null
$HtmlReport = $null
$Output = [system.collections.arraylist]@()
$HtmlReport=@()

$head = @"
<style>
h1, h5, th { text-align: center; }
table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }
td { font-size: 11px; padding: 5px 20px; color: #000; }
tr { background: #b8d1f3; }
tr:nth-child(even) { background: #dae5f4; }
tr:nth-child(odd) { background: #b8d1f3; }
</style>
"@

$Server = @(
"Server001"
"Server002",
"Server003",
"Server004"
)
foreach($s in $Server)
{
    Write-Host "`n"
    Write-Host "Queue Status for Server: " -ForegroundColor Yellow -NoNewline
    Write-Host "$s" -ForegroundColor Green
    $body="<h3>[ $($s) ] Queue Report: on $(Get-Date)</h3>"
    $data= Get-Queue -Server $s |?{$_.DeliveryType -notlike "*ShadowRedundancy*" } | sort messagecount -Descending| `
Select Identity,DeliveryType,Status,MessageCount,NextHopDomain 
    Write-Host "[ Completed ]" -ForegroundColor Cyan
    $null =$output.Add($data)
    $HtmlReport += $data | ConvertTo-Html -Body $body -Head $head 
}
$Output|Sort-Object MessageCount -Descending|ft

# Details of the Login Username and SMTP server. These can very from one 
$SMTPServer = "smtprelay.office365.com"
$SMTPPort = "25"
#$Username = "sumant.prasad@test.com"
#$Password = "Password"
#Add email address of person in to & cc  whom you are sending email (Multiple addresses should be comma seperated)
$to = "Messaging@test.com,MessagingHealthReport@test.com"
#$cc = "sumant.prasad@test.com"
$subject = "All Location Email Queue Report"
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $HtmlReport
$message.to.add($to)
#$message.cc.add($cc)
$message.IsBodyHtml = $True
$message.from = "MessagingHealthReport@intertrustgroup.com"

<#If needed to send file as attachment
$attachment = "$Path\CaymanMailboxPermission.csv"
$attach_log = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attachment)
#>

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
#$smtp.EnableSSL = $true
#$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)
Write-Host "Email Sent"

Wednesday 18 August 2021

Event Log Error Details

 Param(

        [string]$LogName = "System",
        [string]$ComputerName = $env:COMPUTERNAME,
        [int]$Newest = 500,
        [string]$ReportTitle = "Event Log Report",
        [Parameter(Mandatory,HelpMessage = "Enter the path for HTML")]
        [string]$Path
)
        $data = Get-EventLog -LogName System -EntryType Error -Newest $Newest -ComputerName $ComputerName |`
        Group-Object -Property Source -NoElement

        #Create an HTML report
        $title = "System Log Analysis"
        $footer = "<h5><i>Report Run $(get-date)</i></h5>"
        $css = "https://jdhitsolutions.com/sample.css"
        $precontent = "<h1>$ComputerName</h1><h2>Last $Newest error sources from $LogName</h2>" 
        $data | Sort-Object -Property count,name -Descending | select count,name | `
        ConvertTo-Html -Title $title -PreContent $precontent -PostContent $footer -CssUri $css | Out-File "D:\Sumant\test.html"