Public/Invoke-ADUserLockoutStatusReport.ps1


Function Invoke-ADUserLockoutStatusReport {
# .ExternalHelp .\Invoke-ADUserLockoutStatusReport-help.xml
    [cmdletbinding(SupportsShouldProcess=$true)]
    param(
        [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
        [String]$UserName,
        [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
        [String]$ReferenceComputer,
        [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
        [DateTime]$LockOutTime,
        [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
        [String]$DC,
        [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
        [string]$DisplayName,
        [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
        [string]$LockoutDuration,
        [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
        [String]$SMTPServer,
        [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
        [String]$from,
        [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$False)]
        [String]$AdminEmail,
        [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$False)]
        [String]$CompanyName
       
    )

    BEGIN{
        Import-module ActiveDirectory 
        
    }#BEGIN

    PROCESS{
       
        
        try{
            
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Getting User $($UserName) DisplayName"
                $UserDetails = Get-ADUser -Identity $($UserName) -Properties Name, EmailAddress -ErrorAction Stop
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Genarating report output for $($UserDetails.Name)"
               
                $Report = [pscustomobject][ordered]@{
                            "DisplayName" = $UserDetails.Name
                            "UserName" =$UserName
                            "UserEmail" = $UserDetails.EmailAddress
                            "ReferenceComputer" = $ReferenceComputer
                            "LockoutTime" =$LockOutTime
                            "DC" = $DC
                            }
                $Report

                if($PSCmdlet.ShouldProcess($UserDetails.EmailAddress)){
                    
                        $Report | Send-AccountLockoutNotificationEmail  -SMTPServer $SMTPServer -LockoutDuration $LockoutDuration -from $from  -AdminEmail $AdminEmail -CompanyName $CompanyName 
                }#IF
        }#TRY
        Catch{
            $ErrorMessage = $_.Exception.Message
            $ErrorMessage
        }#CATCH
        


    }#PROCESS

    END{

    }#END





}#Function Invoke-ADUserLockoutStatusReport


Function Send-AccountLockoutNotificationEmail 
{
    [cmdletbinding()]
    Param
    (
    [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
    [string]$DisplayName,
    [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
    [string]$UserName,
    [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
    [string]$ReferenceComputer,
    [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
    [string]$LockOutTime,
    [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
    [string]$LockoutDuration,
    [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
    [String]$SMTPServer,
    [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
    [String]$from,
    [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$False)]
    [String[]]$UserEmail,
    [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$False)]
    [String[]]$AdminEmail,
    [parameter(ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$true,Mandatory=$False)]
    [String]$CompanyName
  
    )
        
      
            Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Setting up HTML Information for email"
            $Subject = "Windows User Accout was locked out for $($DisplayName)"
            $AdminSubject = "User $($DisplayName) was locked out at $($LockOutTime)"
            $Body = "Dear <b>$($DisplayName)</b></font>,"
            $Body += "<p><br><font color=red>Your account $($UserName) was locked out on $($LockOutTime)</font></b></p>" 
            $Body += "<p>You will need to contact the IT Helpdesk to unlock your account or wait for $($LockoutDuration) mins until the system automatically unlocks your account.</p>"
            $Body += "<p>UserName: $($UserName)</p>"
            $AdminBody = "<p>UserName: $($UserName)</p>"
            $Body += "<p>Time Locked Out: $($LockOutTime)</p>"
            $AdminBody += "<p>Time Locked Out: $($LockOutTime)</p>"
            $Body += "<p>Reference Computer: $($ReferenceComputer)</p>"
            $AdminBody += "<p>Reference Computer: $($ReferenceComputer)</p>"
            $Body += "<P>Thank you</p>"
            $Body += "<p>$($CompanyName) IT Support</P>"

           
            if ($UserEmail -ne $null) {
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] User email address detected"
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Sending User EMail to $($UserEmail)"   
                Send-MailMessage -SmtpServer $SMTPServer -To $UserEmail -From $from -Subject $Subject -Body $Body -BodyAsHtml -Priority high 
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Sending Admin EMail to $($AdminEmail)"
                Send-MailMessage -SmtpServer $SMTPServer -To $AdminEmail -From $from -Subject  $AdminSubject -Body $AdminBody -BodyAsHtml -Priority high 
          
            }#IF
            else{
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Sending Mail to $($AdminEmail)"
                Send-MailMessage -SmtpServer $SMTPServer -To $AdminEmail -From $from -Subject  $AdminSubject -Body $AdminBody -BodyAsHtml -Credential $Cred -Priority high 
            }#ELSE

       
 
}# Function Send-AccountLockoutNotificationEmail




# SIG # Begin signature block
# MIIIWAYJKoZIhvcNAQcCoIIISTCCCEUCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck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