Hi,
FYI - I have only started my powershell/scripting journey so please forgive my lack of knowledge.
I have inherited a PS script to disable inactive users in AD after 30 days of inactivity.
The script also outputs to a .txt file, a .csv file, and writes into the "notes" field in the telephone tab of the AD user.
The script has been working. But we have now noticed that when the a user has been disabled before and there is an entry in the notes field. The output won't overwrite or add to the notes field when the user is diabled again.
So the correct output is
Disabled due to inactivity - 20/07/2021
The subsequent error output is
CN=Firstname Lastname,OU=Users,DC=mydomain,DC=com
We also get the error below when running the PS
Method invocation failed because [Microsoft.ActiveDirectory.Management.ADUser]
does not contain a method named 'op_Addition'.
At E:\inactiveusers\inactiveusers.ps1:58 char:21
- $Info += " $UpdateInformation - $Date"
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidOperation: (op_Addition:String) [], Runti
meException
- FullyQualifiedErrorId : MethodNotFound
Here is the full script
Any help is appreciated.
<#
TO RUN:
.\Disable-InactiveUsers.ps1 -Remediate
#>
[CmdletBinding()]
param (
[Parameter( Mandatory=$false)]
[int]$TimeFrame = 30,
[Parameter( Mandatory=$false)]
[string]$UpdateInformation = "Disabled due to inactivity",
[Parameter( Mandatory=$false)]
[switch]$Remediate,
[Parameter( Mandatory=$false)]
[string]$LogName = "UserLogNew.txt",
[Parameter( Mandatory=$false)]
[string]$ExclusionsPath = "\\mydomain.com\files\IT\Scripts\InactiveUsers\exclusions.txt",
[Parameter( Mandatory=$false)]
[string]$TriggeredPath = "\\mydomain.com\files\IT\Scripts\InactiveUsers\DisabledLogNew.csv"
)
$Date = Get-Date -Format "dd/MM/yyyy"
$LogDate = Get-Date -Format "yyyy MM dd - HH:mm:ss tt"
$myDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$LogPath = "\\mydomain.com\files\IT\Scripts\InactiveUsers\UsersLogNew.txt"
$Report = New-Object PSObject
$TriggeredUsers = @()
$Exclusions = Get-Content $ExclusionsPath
Import-Module ActiveDirectory
$users = Get-ADUser -Properties name, lastlogondate, SamAccountName, Info -filter {(enabled -eq $true -and SamAccountName -notlike "*service*" -and SamAccountName -notlike "*svc*" -and SamAccountName -notlike "*cnw*")} -SearchBase 'ou=users,dc=mydomain,dc=com'
Function Write-LogFile {
[CmdletBinding()]
param(
[Parameter( Position=0,Mandatory=$true)]
[string]$LogData
)
"$Date - $LogData" | Out-file -FilePath $LogPath -Append
}
foreach ($User in $Users) {
$UserName = $User.DistinguishedName
if ($Exclusions -notcontains $User.SamAccountName) {
if ($User.LastLogonDate -lt (Get-Date).AddDays(-$TimeFrame) -AND $User.LastLogonDate -ne $null) {
if ($Remediate) {
if ($UpdateInformation -ne $null) {
$Info = Get-ADUser $UserName -Properties info | Where-Object {$_.info}
$Info += " $UpdateInformation - $Date"
try {
Set-ADUser -Identity $UserName -Replace @{info="$Info"} -ErrorAction Stop
Write-LogFile -LogData "Successfully set Info field for $($User.Name). New Info: $UpdateInformation - $Date"
}
catch {
Write-LogFile -LogData "Error - Failed to set Info field for $($User.Name) - $_"
}
}
try {
Disable-ADAccount -Identity $UserName -ErrorAction Stop
Write-LogFile -LogData "$($User.Name) successfully disabled"
}
catch {
Write-LogFile -LogData "Error - Failed to disable AD Account $($User.Name) - $_"
}
}
$TriggeredUsers += $User | Select Name,SamAccountName,LastLogonDate,Info
}
}
}
$TriggeredUsers | Format-Table
$TriggeredUsers | Export-Csv $TriggeredPath -NoTypeInformation -Append