Detection and Remediation Script in Intune

281 views Asked by At

I create a detection and remediation script and upload it in Intune however when I run the remediation script the status shows in the detection (With issues) and remediation (failed). I've been trying to move the exit 1 code but having the same result. I don't know if the problem is on my script, but here's my script:

DetectAdminChange.ps1

# Define the username of the local admin you want to monitor
$adminUsername = "OfflineAdmin"

# Define the path to the log file
$logFilePath = "C:\ps_script_logs\OfflineAdmin Password Change Log.txt"

# Get the current password of the admin user
$currentPassword = (Get-LocalUser -Name $adminUsername).Password

# Check if the password has changed
if ($currentPassword -ne "messyW@ter10") {
    Add-Content -Path $logFilePath -Value "$(Get-Date) - Admin password changed."
    Exit 1
}

RemediateAdminPassword.ps1

# Define the username of the local admin you want to remediate
$adminUsername = "OfflineAdmin"

# Set the default password for the admin user
$password = ConvertTo-SecureString -String "messyW@ter10" -AsPlainText -Force
Set-LocalUser -Name $adminUsername -Password $password
Exit 0

# Set password to not expire and cannot change
Set-LocalUser -Name $adminUsername -PasswordNeverExpires $true -CannotChangePassword $true
Exit 0

# Define the path to the log file
$logFilePath = "C:\ps_script_logs\OfflineAdmin Password Change Log.txt"
Add-Content -Path $logFilePath -Value "$(Get-Date) - Admin password remediated."
Exit 1
0

There are 0 answers