Skip to content

Commit

Permalink
Merge pull request #1933 from iserrano76/Logs
Browse files Browse the repository at this point in the history
Change from Write-SimpleLogFile to OutputOverrides on Set-ExchAVExclusions
  • Loading branch information
dpaulson45 authored Jan 22, 2024
2 parents a7142fa + c7f3db0 commit 2c04dd4
Showing 1 changed file with 52 additions and 40 deletions.
92 changes: 52 additions & 40 deletions Setup/SetExchAVExclusions/Set-ExchAVExclusions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ If you want to get the full list of expected exclusions you should use the param
You can export the Exclusion List with the parameter FileName
.PARAMETER -ListRecommendedExclusions
.PARAMETER ListRecommendedExclusions
Show the full list of expected exclusions.
.PARAMETER -FileName
.PARAMETER FileName
Export the full list of expected exclusions in the defined FileName.
.PARAMETER SkipVersionCheck
Expand All @@ -52,15 +52,6 @@ Skip script version verification.
.PARAMETER ScriptUpdateOnly
Just update script version to latest one.
.INPUTS
For Set Parameter Set Identifier(Switch):
Optional Parameter -FileName
For List Parameter Set Identifier(Switch):
Required Parameter -ListRecommendedExclusions
Optional Parameter -FileName
.EXAMPLE
.\Set-ExchAVExclusions.ps1
This will run Set-ExchAVExclusions Script against the local server.
Expand Down Expand Up @@ -102,7 +93,19 @@ param (
. $PSScriptRoot\..\..\Shared\Confirm-ExchangeShell.ps1
. $PSScriptRoot\..\..\Shared\Get-ExchAVExclusions.ps1
. $PSScriptRoot\..\..\Shared\ScriptUpdateFunctions\Test-ScriptVersion.ps1
. $PSScriptRoot\..\..\Diagnostics\AVTester\Write-SimpleLogFile.ps1
. $PSScriptRoot\..\..\Shared\LoggerFunctions.ps1
. $PSScriptRoot\..\..\Shared\OutputOverrides\Write-Host.ps1
. $PSScriptRoot\..\..\Shared\OutputOverrides\Write-Warning.ps1

function Write-HostLog ($message) {
if (![string]::IsNullOrEmpty($message)) {
$Script:HostLogger = $Script:HostLogger | Write-LoggerInstance $message
}
}

$Script:HostLogger = Get-NewLoggerInstance -LogName "SetExchAvExclusions" -LogDirectory $PSScriptRoot
SetWriteHostAction ${Function:Write-HostLog}
SetWriteWarningAction ${Function:Write-HostLog}

$BuildVersion = ""

Expand All @@ -123,41 +126,44 @@ if ((-not($SkipVersionCheck)) -and
return
}

# Log file name
$LogFile = "$PSScriptRoot\SetExchAvExclusions.log"

# Confirm that we are an administrator
if (-not (Confirm-Administrator)) {
Write-Error "Please run as Administrator"
Write-Host "[ERROR]: Please run as Administrator" -ForegroundColor Red
exit
}

if (-not $ListRecommendedExclusions) {
if ( $($host.Version.Major) -lt 5 -or ( $($host.Version.Major) -eq 5 -and $($host.Version.Minor) -lt 1) ) {
Write-Error "This version of Windows do not have Microsoft Defender"
Write-Host "[ERROR]: This version of Windows do not have Microsoft Defender" -ForegroundColor Red
exit
}

$checkCmdLet = $null
$checkCmdLet = Get-Command Get-MpComputerStatus -ErrorAction SilentlyContinue
if ($null -eq $checkCmdLet) {
Write-Host "Get-MpComputerStatus cmdLet is not available" -ForegroundColor Red
Write-Host "This script only sets Exclusions on Microsoft Defender" -ForegroundColor Red
Write-Host "[ERROR]: Get-MpComputerStatus cmdLet is not available" -ForegroundColor Red
Write-Host "[ERROR]: This script only sets Exclusions on Microsoft Defender" -ForegroundColor Red
Write-Host "If you have any other Antivirus you can use -ListRecommendedExclusions parameter to get the Recommended Exclusion List"
exit
} else {
$mpStatus = $null
$mpStatus = Get-MpComputerStatus -ErrorAction SilentlyContinue
if ($null -eq $mpStatus) {
Write-Host "We cannot get Microsoft Defender information" -ForegroundColor Red
Write-Host "This script only sets Exclusions on Microsoft Defender" -ForegroundColor Red
Write-Host "[ERROR]: We cannot get Microsoft Defender information" -ForegroundColor Red
Write-Host "[ERROR]: This script only sets Exclusions on Microsoft Defender" -ForegroundColor Red
Write-Host "If you have any other Antivirus you can use -ListRecommendedExclusions parameter to get the Recommended Exclusion List"
exit
} else {
if (-not $mpStatus.AntivirusEnabled ) {
Write-Warning "Microsoft Defender is not enabled."
Write-Warning "We will apply the exclusions but they do not take effect until you Enabled Microsoft Defender."
Write-Host "If you have any other Antivirus you can use -ListRecommendedExclusions parameter to get the Recommended Exclusion List"
} else {
if (-not $mpStatus.RealTimeProtectionEnabled) {
Write-Warning "RealTime protection is not enabled."
Write-Warning "We will apply the exclusions but they do not take effect until you Enabled RealTime Protection."
Write-Host "If you have any other Antivirus you can use -ListRecommendedExclusions parameter to get the Recommended Exclusion List"
}
}
}
}
Expand All @@ -167,87 +173,91 @@ $serverExchangeInstallDirectory = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Exch

# Check Exchange registry key
if (-not $serverExchangeInstallDirectory ) {
Write-Warning "Failed to find the Exchange installation Path registry key"
Write-Host "[ERROR]: Failed to find the Exchange installation Path registry key" -ForegroundColor Red
exit
}

# Check the installation path
if (-not ( Test-Path $($serverExchangeInstallDirectory.MsiInstallPath) -PathType Container) ) {
Write-Warning "Failed to find the Exchange installation Path"
Write-Host "[ERROR]: Failed to find the Exchange installation Path" -ForegroundColor Red
exit
}

# Check Exchange is 2013, 2016 or 2019
if ( -not ( $($serverExchangeInstallDirectory.MsiProductMajor) -eq 15 -and `
($($serverExchangeInstallDirectory.MsiProductMinor) -eq 0 -or $($serverExchangeInstallDirectory.MsiProductMinor) -eq 1 -or $($serverExchangeInstallDirectory.MsiProductMinor) -eq 2 ) ) ) {
Write-Warning "This script is designed for Exchange 2013, 2016 or 2019"
Write-Host "[ERROR]: This script is designed for Exchange 2013, 2016 or 2019" -ForegroundColor Red
exit
}

$ExchangePath = $serverExchangeInstallDirectory.MsiInstallPath

#Check if the file path exists
if ($FileName -like '*\*') {
if (-not (Test-Path $FileName.Substring(0, $FileName.LastIndexOf("\")))) {
Write-Warning "FilePath does not exists"
Write-Host "[ERROR]: FilePath does not exists" -ForegroundColor Red
exit
}
}

$ExchangePath = $serverExchangeInstallDirectory.MsiInstallPath

# Check Exchange Shell and Exchange installation
$exchangeShell = Confirm-ExchangeShell
if (-not($exchangeShell.ShellLoaded)) {
Write-Warning "Failed to load Exchange Shell Module..."
Write-Host "[ERROR]: Failed to load Exchange Shell Module..." -ForegroundColor Red
exit
}

Write-Host " "
Write-Host "Exclusions Paths:" -ForegroundColor DarkGreen
# Create the Array List
Write-Host "`r`nExclusions Paths:" -ForegroundColor DarkGreen
$BaseFolders = New-Object Collections.Generic.List[string]
$BaseFolders = Get-ExchAVExclusionsPaths -ExchangePath $ExchangePath -MsiProductMinor ([byte]$serverExchangeInstallDirectory.MsiProductMinor)
if ($FileName) {
"[Paths]" | Out-File $FileName
}
foreach ($folder in $BaseFolders) {
if ($ListRecommendedExclusions) {
Write-Host ("$folder")
Write-Host "$folder"
} else {
Write-SimpleLogFile -String ("Adding $folder") -LogFile $LogFile -OutHost
Write-Host "Adding: $folder"
Add-MpPreference -ExclusionPath $folder
}
if ($FileName) {
$folder | Out-File $FileName -Append
}
}

Write-Host "`r`nExclusions Extensions:" -ForegroundColor DarkGreen
Write-Host " "
Write-Host "Exclusions Extensions:" -ForegroundColor DarkGreen
$extensionsList = New-Object Collections.Generic.List[string]
$extensionsList = Get-ExchAVExclusionsExtensions -MsiProductMinor ([byte]$serverExchangeInstallDirectory.MsiProductMinor)
if ($FileName) {
"`r`n[Extensions]" | Out-File $FileName -Append
}
foreach ($extension in $extensionsList) {
if ($ListRecommendedExclusions) {
Write-Host ("$extension")
Write-Host "$extension"
} else {
Write-SimpleLogFile -String ("Adding $extension") -LogFile $LogFile -OutHost
Write-Host "Adding: $extension"
Add-MpPreference -ExclusionExtension $extension
}
if ($FileName) {
$extension | Out-File $FileName -Append
}
}

Write-Host "`r`nExclusions Processes:" -ForegroundColor DarkGreen
Write-Host " "
Write-Host "Exclusions Processes:" -ForegroundColor DarkGreen
$processesList = New-Object Collections.Generic.List[string]
$processesList = Get-ExchAVExclusionsProcess -ExchangePath $ExchangePath -MsiProductMinor ([byte]$serverExchangeInstallDirectory.MsiProductMinor)
if ($FileName) {
"`r`n[Processes]" | Out-File $FileName -Append
}
foreach ($process in $processesList) {
if ($ListRecommendedExclusions) {
Write-Host ("$process")
Write-Host "$process"
} else {
Write-SimpleLogFile -String ("Adding $process") -LogFile $LogFile -OutHost
Write-Host "Adding: $process"
Add-MpPreference -ExclusionPath $process
Add-MpPreference -ExclusionProcess $process
}
Expand All @@ -256,8 +266,10 @@ foreach ($process in $processesList) {
}
}

Write-Host " "
if ($ListRecommendedExclusions) {
Write-Host ('')
Write-Host "Exclusions Detection Completed" -ForegroundColor Green
} else {
Write-Host "Exclusions Applied" -ForegroundColor Green
}

Write-SimpleLogFile -String ("Exclusions Completed") -LogFile $LogFile -OutHost
Write-Host " "

0 comments on commit 2c04dd4

Please sign in to comment.