I have a script that I crafted up to get a list of account that need disabled, then piped the users and move them to a different OU. It doesn't appear to actually be working. It gets all the way down to the "Start-Sleep" - then doesn't continue
the script - or do anything to the user specified. Write Verbose tells me
Cmdlet Write-Verbose at command pipeline position 1
Supply values for the following parameters
message:
---Script----
# This script will disable all users that are contained within a CSV in Active Directory - Pipe the disabled user and place it into the appropriate OU.
# Create by DR
# January 25th, 2017
##################
#BEGIN VARIABLES
##################
$VerbosePreference = "silentlycontinue"
#Defines the CSV file path to import it
$UsersToDisable = Get-Content "THIS IS WHERE I PUT MY FILE PATH\UsersToDisable.csv"
#Defines the domain and the target domain controller
$LocalDomain = 'THIS IS MY LOCAL DOMAIN'
$TargetDomainController = 'THIS IS MY TARGET DC'
#Defines where to Move Inactive Users
$MoveUsersToOU = 'OUR OU FOR INACTIVE USERS GOES HERE'
#Defines file path to save log locally
$LogPath = "YET ANOTHER LOG PATH\ActiveDirectoryLogs"
##################
#END VARIABLES
##################
#Import Module: Import the Powershell Active Directory module
Import-Module ActiveDirectory
#Timestamp: Create a timestamp for use as part of a directory or file name
$TimeStampBefore = Get-Date -Format s | foreach {$_ -replace ":", "-"}
#Log Folder - Create a Log Foler if one doesn't already exist
If ( -Not (Test-Path -Path $LogPath)) {New-Item -Path $LogPath -ItemType Directory}
#Load Users: Load list of users into a variable
$UsersToDisable
#Document Before: Document user settings before making changes
$CsvBeforePath = $LogPath+ '\DisableUsers-' +$MoveUsersToOU+ '-' +$TimeStampBefore+ '-before.csv'
foreach ($i in $userstodisable) {Get-ADUser -identity $i | Export-Csv -Path $CsvBeforePath}
ForEach ($i in $UsersToDisable)
{
#Disable Users: Disable these Active Directory User Accounts and Move to Target Path
foreach ($i in $usertodisable) {Get-ADUser -identity $i | Disable-ADAccount -identity $i | Move-ADObject -TargetPath (Get-ADOrganizationalUnit -Filter 'Name -eq $MoveUsersToOU')}
Write-Host "Attempting to get AD User, disable, and move to Inactive Users OU" -ForegroundColor Red -BackgroundColor White
Write-Verbose
#Pause: Allow for Active Directory to make replication changes
Write-Host "Waiting 60 seconds for Active Directory to make Replication Changes" -ForegroundColor Cyan -BackgroundColor Blue
Start-Sleep -Seconds 60
# Create a TimeStamp for use as part of a directory file name
$TimeStampAfter = Get-Date -Format s | foreach {$_ -replace ":", "-"}
#Document Users After Making Changes
$CsvAfterPath = $LogPath+'\DisableUsers-'+$MoveUsersToOU+'_'+$TimeStampAfter+'-After.csv'
foreach ($i in $usertodisable) {Get-ADUser -identity $i | Export-Csv -Path $CsvAfterPath}
}
#clear