<# .SYNOPSIS .DESCRIPTION .NOTES This script is intended for use in a test or production environment. Make sure to test the script in a non-production environment before running it in production. Author: D.de Kooker - info@dcomputers.nl Version: 0.1 DISCLAIMER: Use scripts at your own risk, if there is anything I can help you with I will try but I do not take responsibility for the way that anyone else uses my scripts. Sharing is caring. Share your knowledge with the world so that everybody can learn from it. .LINK The latest version can Always be found on my GIT page on the link below: https://git.dcomputers.nl/Dcomputers/PowershellScripts #> #region script parameters #endregion #region Global script settings and variables #General $Version = "v0.1" $logfilelocation = "$($MyInvocation.MyCommand.Path | Split-Path -Parent)\Logs" $logfilename = "$(Get-Date -Format yyyyMMddHHmmss).log" $exportfilelocation = "$($MyInvocation.MyCommand.Path | Split-Path -Parent)\Exports" $summaryfilename = "$(Get-Date -Format yyyyMMddHHmmss)-Summary.txt" #Azure Enterprise app configuration $STR_TenantID = "" $STR_AppID = "" $STR_ClientSecret = "" #Email report settings $STR_SMTPServer = "" $STR_SMTPServerPort = "" $STR_SMTPUsername = "" $STR_SMTPPassword = "" $STR_EmailSubject= "Powershell script template - $(Get-Date -Format "dd-MM-yyyy")" $STR_SMTPFromaddress = "ICT Servicedesk " $STR_Receivers = "servicedesk@contoso.com,systemengineer1@contoso.com" #List of commaseperated emailaddresses #endregion #region functions function SendMailv2 ($To,$Subject,$Body,$Attachments = @()){ $SMTPClient = New-Object Net.Mail.SmtpClient($STR_SMTPServer, $STR_SMTPServerPort) # $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($STR_SMTPUsername, $STR_SMTPPassword); $SMTPMessage = New-Object System.Net.Mail.MailMessage($STR_SMTPFromaddress,$To,$Subject,$Body) $SMTPMessage.IsBodyHTML = $true # Add attachments if provided if ($Attachments.Count -gt 0) { foreach ($attachment in $Attachments) { $SMTPMessage.Attachments.Add((New-Object System.Net.Mail.Attachment($attachment))) } } $SMTPClient.Send($SMTPMessage) } function Initiate-Log { # Get current user and session information $username = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name $computerName = $env:COMPUTERNAME $sessionID = $pid $date = Get-Date -Format "yyyy-MM-dd HH:mm:ss" # Write log header $logHeader = "[$date] Log initiated by $username on $computerName (Session ID: $sessionID)" Add-Content -Path $logfilelocation\$logfilename -Value "**********************" Add-Content -Path $logfilelocation\$logfilename -Value "LogFile initiation" Add-Content -Path $logfilelocation\$logfilename -Value "Start time: $date" Add-Content -Path $logfilelocation\$logfilename -Value "Username: $username" Add-Content -Path $logfilelocation\$logfilename -Value "Machine: $computerName" Add-Content -Path $logfilelocation\$logfilename -Value "Process ID: $sessionID" Add-Content -Path $logfilelocation\$logfilename -Value "Script Version: $Version" Add-Content -Path $logfilelocation\$logfilename -Value "Script Source: https://git.dcomputers.nl/Dcomputers/PowershellScripts" Add-Content -Path $logfilelocation\$logfilename -Value "**********************" } function Write-Log { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [string]$Message, [Parameter(Mandatory=$false)] [ValidateSet("INFO", "WARNING", "ERROR")] [string]$Level = "INFO" ) $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $logmessage = "[$timestamp] [$Level] $Message" Add-Content -Path $logfilelocation\$logfilename -Value $logmessage } function Write-Summary { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [string]$Message ) Add-Content -Path $logfilelocation\$summaryfilename -Value $Message } #endregion #region prerequisites check #Create log directory if not present and initiate logfile if (!(test-path $logfilelocation)) {mkdir $logfilelocation} if (!(test-path $exportfilelocation)) {mkdir $exportfilelocation} Initiate-Log #Check if the required Powershell Modules are available $modules = @("") foreach ($module in $modules) { if (!(Get-Module -Name $module -ListAvailable)) { Write-Host "The $module module is not installed. Please install it and try again." Write-Log -Message "The $module module is not installed. Please install it and try again." -Level ERROR exit 1 } else { Import-Module $module Write-Log -Message "The $module module is loaded." -Level INFO } } #endregion #region script where action is taken #endregion #region send reports and generate summary report # Send the report via email $emailbody = @" "@ $emailattachments = $() SendMailv2 -To $STR_Receivers -Subject $STR_EmailSubject -Body $emailbody -Attachments $emailattachments #endregion