Today I had built a script in order to do a clean up of users when they quit or get termed. But I was having an issue with the $Null variable and different issues when using comparison operators like -eq, -ne and -like. I would pull data and the do some testing with if statements but would not get what I was expecting in return. I am a tad new to PowerShell, but you can blame Jeffrey Snover as I read one of his blogs and he said to not worry about being an embarrassed beginner. You can see his post Variable Scopes and $PRIVATE: | Jeffrey Snover's blog
Any pointers in what I am doing wrong would be much appreciated.
Here are the parts I was having issues with:
In this If statement I started with everything equaling $Null, but for some reason it was not respecting the $strRDFExists and $strRPExists. I had to change them to single quotes.
If(($ForwardRefreshed.ForwardingAddress) -eq $Null -and ($UserRefreshed.Manager) -eq $Null -and ($UserRefreshed.ProfilePath) -eq $Null -and ($strRDFExists) -eq '' -and ($strRPExists) -eq '')
{
Write-Verbose "$($User.Name) - Is moved to Terminated - Archived"
Get-ADUser $($User.SamAccountName) | Move-ADObject -TargetPath "OU=Terminated - Archive - No DirSync,DC=Company,DC=com"
$strMoveSetting = 1
}
I also had issues with the same If statements in order to build my output:
I had first set the If statements to just check if they had content by not using comparison operators like -ne or -like. I started having issues so I had to use some $Null and -like operators.
If($UserRefreshed.ProfilePath -ne $Null)
{
$objOutput | Add-Member -Type NoteProperty -Name "ProfilePath" -Value $($UserRefreshed.ProfilePath)
}
If($strRPExists -like "\\*")
{
$objOutput | Add-Member -Type NoteProperty -Name "ProfileExist" -Value $($strRPExists)
}
If($strRDFExists -like "\\*")
{
$objOutput | Add-Member -Type NoteProperty -Name "RedirectedExist" -Value $($strRDFExists)
}
Here is the full script:
function Get-TermedADInformation
{
[CmdletBinding()]
[Alias()]
[OutputType([int])]
Param
(
)
Begin
{
Write-Verbose "Adding starting variables."
# Script Variables
$ScriptLocation = ' "\\Server\C$\1T_Tools\Scheduled Tasks\ThreeMonthTermedUsers.ps1"'
# Email Variables
$MailTo = "helpdesk@Company.com"
$MailFrom = "Alerts-Infrastructure@Company.com"
$MailSubject = "Termed users in past 3 months"
$MailBody = "*** This body uses Variables not this line ***"
$MailRelay = "smtp.Company.com"
$style = "TABLE{border-collapse: collapse;border: 1px solid black;width: 100%;}TH{border-collapse: collapse;border: 1px solid black;background-color: Gray;color: white;text-align:`"left`";}TD{font-size: .85em;border-collapse: collapse;border: 1px solid black;vertical-align:`"Bottom`";padding: 3px 7px 2px 7px;}"
$Output = @()
# Connect to On-Premise Exchange PowerShell
Write-Verbose "Adding PSSession to On-Prem Exchange"
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://Server.FQDN/PowerShell/ -Authentication Kerberos
Import-PSSession $Session
$SessionID = Get-PSSession | Where {$_.ComputerName -eq "Server.FQDN"} | select -ExpandProperty ID
# Need to get all users that have been terminated in the past 3 months
$ThreeMonths = (Get-Date).AddMonths(-3) # DO NOT CHANGE THIS, there are settings that will GSD.
$FourMonths = (Get-Date).AddMonths(-4)
Write-Verbose "Getting user information between dates $ThreeMonths and $FourMonths"
$TermedUserInfo = (Get-ADUser -Filter * -SearchBase "OU=Terminated - Processed,DC=Company,DC=com" -Properties *) | Where-Object {$_.extensionAttribute1 -lt $ThreeMonths -and $_.extensionAttribute1 -gt $FourMonths} | select -Property Name, SamAccountName, Description, Title, PasswordLastSet, ProfilePath, Manager, extensionAttribute1
}
Process
{
try
{
Write-Verbose "Starting Foreach user..."
Foreach($User in $TermedUserInfo)
{
Write-Verbose "Clearing user variables..."
$strMoveSetting = 0
$strForwardingAddress = ""
$strRPExists = ""
$strRDFExists = ""
$UserRefreshed = ""
$strForwardingAddress = Get-Mailbox -Identity ($User.SamAccountName) -ErrorAction SilentlyContinue | Where {$_.ForwardingAddress -ne $Null} | select -Property ForwardingAddress, UserPrincipalName
# Test for Roaming Profiles folder
Write-Verbose "Checking to see if Roaming Profile folders exists"
If(Test-Path "\\RoamingFolders\Profiles$\$($User.SamAccountName)*")
{
$strRPExists = "\\RoamingFolders\Profiles$"
Write-Verbose "Folder exists in $strRPExists"
}
ElseIf(Test-Path "\\PFS-UTH-02\ADRoaming$\$($User.SamAccountName)*")
{
$strRPExists = "\\PFS-UTH-02\ADRoaming$"
Write-Verbose "Folder exists in $strRPExists"
}
ElseIf(Test-Path "\\PDC-TOR-01\ADPROFILES$\$($User.SamAccountName)*")
{
$strRPExists = "\\PDC-TOR-01\ADPROFILES$"
Write-Verbose "Folder exists in $strRPExists"
}
# Test for Redirected Files folder
Write-Verbose "Checking to see if Redirected Files folder exists"
If(Test-Path "\\RoamingFolders\RedirectedFiles$\$($User.SamAccountName)")
{
$strRDFExists = "\\RoamingFolders\RedirectedFiles$"
Write-Verbose "Folder exists in $strRDFExists"
}
ElseIf(Test-Path "\\PFS-UTH-02\RoamingFiles$\$($User.SamAccountName)")
{
$strRDFExists = "\\PFS-UTH-02\RoamingFiles$"
Write-Verbose "Folder exists in $strRDFExists"
}
ElseIf(Test-Path "\\PDC-TOR-01\ADRoaming$\$($User.SamAccountName)")
{
$strRDFExists = "\\PDC-TOR-01\ADRoaming$"
Write-Verbose "Folder exists in $strRDFExists"
}
# Getting work done - Time to make changes
Write-Verbose "If the ProfilePath, Manager or FowardingAddress exist, it will be removed."
If($User.ProfilePath)
{
Set-ADUser -Identity $User.SamAccountName -Clear ProfilePath
Write-Verbose "$($User.SamAccountName) ProfilePath was removed"
}
If($User.Manager)
{
Set-ADUser -Identity $User.SamAccountName -Clear Manager
Write-Verbose "$($User.SamAccountName) Manager was removed"
}
If($strForwardingAddress.UserPrincipalName)
{
Set-Mailbox -Identity $strForwardingAddress.UserPrincipalName -ForwardingAddress $null -DeliverToMailboxAndForward $false
Write-Verbose "$($strForwardingAddress.UserPrincipalName) ForwardingAddress was removed."
}
Write-Verbose "Refreshing User and Mailbox information after the removel process..."
$UserRefreshed = Get-ADUser -Identity $User.SamAccountName -Properties * | Select -Property ProfilePath, Manager
$ForwardRefreshed = Get-Mailbox -Identity ($User.SamAccountName) -ErrorAction SilentlyContinue | Where {$_.ForwardingAddress -ne $Null} | select -Property ForwardingAddress, UserPrincipalName
# Move the user to Terminated - Archive if the: Manager, ProfilePath, ForwardingAddress, RedirectedFiles and Profile Folders are empty.
Write-Verbose "Moving user to Terminated - Archive, if ForwardingAddress, Manager, ProfilePath, RedirectedFiles and Profiles folder are cleaned up."
If(($ForwardRefreshed.ForwardingAddress) -eq $Null -and ($UserRefreshed.Manager) -eq $Null -and ($UserRefreshed.ProfilePath) -eq $Null -and ($strRDFExists) -eq '' -and ($strRPExists) -eq '')
{
Write-Verbose "$($User.Name) - Is moved to Terminated - Archived"
Get-ADUser $($User.SamAccountName) | Move-ADObject -TargetPath "OU=Terminated - Archive - No DirSync,DC=Company,DC=com"
$strMoveSetting = 1
}
# Start putting together the Output
Write-Verbose "Gathering proper data to display in output..."
$objOutput = New-Object System.Object
$objOutput | Add-Member -Type NoteProperty -Name "Name" -Value $($User.Name)
$objOutput | Add-Member -Type NoteProperty -Name "SamAccountName" -Value $($User.SamAccountName)
$objOutput | Add-Member -Type NoteProperty -Name "Termed Date" -Value $($User.extensionAttribute1)
If($UserRefreshed.ProfilePath -ne $Null)
{
$objOutput | Add-Member -Type NoteProperty -Name "ProfilePath" -Value $($UserRefreshed.ProfilePath)
}
If($strRPExists -like "\\*")
{
$objOutput | Add-Member -Type NoteProperty -Name "ProfileExist" -Value $($strRPExists)
}
If($strRDFExists -like "\\*")
{
$objOutput | Add-Member -Type NoteProperty -Name "RedirectedExist" -Value $($strRDFExists)
}
If($UserRefreshed.Manager)
{
$objOutput | Add-Member -Type NoteProperty -Name "Manager" -Value $((($UserRefreshed.Manager).Split(",")[0]).Split("=")[1])
}
If($strForwardingAddress.ForwardingAddress -ne $Null)
{
$objOutput | Add-Member -Type NoteProperty -Name "Forwarding Address" -Value $($ForwardRefreshed.ForwardingAddress)
}
If(!($strForwardingAddress.UserPrincipalName))
{
$objOutput | Add-Member -Type NoteProperty -Name "Forwarding Address" -Value (" `tUser not found On-Prem")
}
If($strMoveSetting -eq 1)
{
$objOutput | Add-Member -Type NoteProperty -Name "User Archived" -Value ("Yes")
}
Else
{
$objOutput | Add-Member -Type NoteProperty -Name "User Archived" -Value ("No")
}
$Output += $objOutput
}
}
finally
{
Write-Verbose "Cleaning Up ..."
Write-Verbose "Removing SessionID"
Remove-PSSession -Id $SessionID
}
}
End
{
If($Output -ne $Null)
{
Write-Verbose "Sending Email..."
$NewEMail = $Output | ConvertTo-Html -Head $Style
Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -BodyAsHtml "Start Date: $($FourMonths) - End Date: $($ThreeMonths) $($ScriptLocation) $($NewEMail)" -SmtpServer $MailRelay
}
}
}
Get-TermedADInformation