Conditional_Execution.ps1

if ($LastPoint.CreationTime -le $date){
Create-RestorePoint
}
if (($HoursStart -eq $start) -and ($HoursEnd -eq $finish)) {
continue}
else {
Set-ActiveHours
}
foreach ($audioDevice in $audioDevices) {
if ($audioDevices.Status -eq 'BAD') {
Fix-Audio
}
}
if (!($TestDomain)) {
Test-Domain
}
if (!($TestWifiSense)) {
Disable-WiFiSense
}
if ($serviceState.Status -ne 'Running') {
Check-ServiceCrash
}
foreach ($NetAdapter in $NetAdapter) {
if ($NetAdapter.DHCP -eq 'Disabled') {
Check-DhcpEnabled
}
}
if (!($tpm)) {
Check-TPMEnabled
}
if (!($global:DNS1 -xor $global:DNS2)) {
Check-DNS
}
if ($frag -ge '10') {
Check-Fragment
}

$PageFile = Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management' -Name 'ClearPageFileAtShutdown'
if ($PageFile.ClearPageFileAtShutdown -ne '1') {
Clear-PageFile
}

$conditions = (

    $errorCache.Count -gt 0 -xor
    $frag -ge '10' -xor
    $Score.WinSPRLevel -ge '7' -xor
    $check -eq $false -xor
    $tag -ne 'TAG_NAME' -xor
    $computerName -ne 'COMPUTER_NAME' -xor
    $description -ne 'COMPUTER_DESCRIPTION' -xor
    $SerialNumber -ne $serial -xor
    $acls.Owner -ne '$OwnerAccess.Owner' -xor
    $acls.Access -ne '$OwnerAccess.Access' -xor
    $ports.State -eq 'Established Internet' -xor
    $ports.LocalPort -ne $ActivePorts -xor
    $SysUsers -and $System -ne $SysUserList.User -and $SysUserList.System -xor
    $speed -ge '1' -xor
    $drivers.Date -le $date -xor
    $LastPoint -eq 'The last attempt to restore the computer failed.' -xor
    $battery -eq 'BAD' -xor
    $DomainChecker -eq 'WORKGROUP' -xor
    $Ping.Timeout -ge '40000' -xor
    $secboot -eq $false -xor
    $uptime -ge '24:00:00.0000000' -xor
    $FreeSpace -le '0.100000000000000' -xor
    $pro -ge '99.0' -xor
    $mem -le '1000' -xor
    $temp -ge '60.00' -xor
    $lock.ProtectionStatus -eq 'Off' -xor
    $AV.AntivirusEnabled -eq $false -xor
    $log.LevelDisplayName -eq 'Critical', 'Warning', '$powerEventIDs' -xor
    $lang -ne 'en-GB' -xor
    $country -ne 'United Kingdom' -xor
    $timezone -ne 'GMT Standard Time' -xor
    $DNS -ne $DNS1 -and $DNS -ne $DNS2 -xor
    $compatibilityIssues.Count -gt 0 -xor
    $hardwareFailures -xor
    $deliveryProblems -xor
    $wifiInt.LinkSpeed -le '50' -xor
    $ethInt.LinkSpeed -le '50' -xor
    $audioDevice -eq 'BAD' -xor
    $physicalDisks -eq 'BAD'

    )

    if ($conditions) {

    $csvPath = "$computerName.$date_error_report.csv"
    $compressedPath = "$computerName.$date_compressed_error_report.csv"

    $errorObjects = $errorCache | ForEach-Object {
        [PSCustomObject]@{
            ErrorTime = $_.ErrorDetails.TimeGenerated
            ErrorMessage = $_.Exception.Message
            ErrorStackTrace = $_.ScriptStackTrace
        }
    }

    $errorObjects | Export-Csv -Path $csvPath -NoTypeInformation | Compress-Archive -Path $csvPath -DestinationPath $compressedPath

    $from = "your.email@domain.com"
    $to = "helpdesk@domain.com"
    $subject = "Alert: Errors found on $computerName"
    $body = "Please check report $computerName.$date."

    # Slack
    $Token = "YOUR_SLACK_API_TOKEN"
    $Channel = "YOUR_SLACK_CHANNEL_ID"
    Send-SlackMessage -Token $Token -Channel $Channel -Message $body

    # Teams
    $ChannelId = "TEAMS_CHANNEL_ID"
    Connect-MicrosoftTeams
    New-TeamChannelMessage -GroupId $ChannelId -Message $body             

    # Email
    $smtpServer = "your.smtp.server"
    Send-MailMessage -SmtpServer $smtpServer -From $from -To $to -Subject $subject -Body $body -Attachment $compressedPath -BodyAsHtml -Priority High -DeliveryNotificationOption OnFailure
    }
    }
}