Hello All,
I have the following script:
- Disables users
- Move the disabled users to a disabled OU
- Removes the users from all but Domain Users groups
- Writes the date, UserID, Username, ADGroups to a log file in CSV format.
Currently I need to share my *.PS1 file with my team members and am trying to make the process as generic as possible.
"#Disable User Script
# Import active directory module for running AD cmdlets
Import-module ActiveDirectory
set-executionpolicy remotesigned
Add-PSSnapin Quest.ActiveRoles.ADManagement
$File = Read-host "Enter name of CSV File"
$LogFiles = Read-host "Enter name of CSV File"
#Import CSV File
$list = Import-Csv "\\Server\Shared\SharedFolder\Powershell\DisabledUsers\$File"
#Sets the OU where to move the disabled users
$DisabledOU = "OU=2016,OU=User Accounts,OU=Disabled Accounts,dc=Company,dc=org"
foreach($entry in $list) {
#Date and Time
$datetime = [datetime]::Now.ToString("ddd MM/dd/yyyy HH:mm:ss")
#Export Current User Groups With Date/Time Stamp
$UserID = $entry.SamAccountName
$ADGroup=(get-Qadmemberof $userID)
$username = get-qaduser $userID | select -expandproperty name
ECHO $datetime','$UserID','$Username','$ADGroup|Add-content "\\Server\Shared\SharedFolder\Powershell\DisabledUsers\DisabledLogFiles\$LogFile" -Force
#Disable and Move the User(s)
Disable-QADUser $UserID
Start-Sleep -s 2
Move-QADObject -Identity $UserID -NewParentContainer $DisabledOU
Start-Sleep -s 2
#Remove all groups except Domain Users - By Default Domain Users will remain
Remove-QADMemberOf $UserID -RemoveAll
}"
Successfully:
Disables users
Move user to the specified OU
Remove users from all groups but Domain Users.
Returns the following error when writing log file:
Add-Content : The filename, directory name, or volume label syntax is
incorrect.
At U:\Powershell\DisabledUsers\DisableUsers-New.ps1:26 char:49
+ ECHO $datetime','$UserID','$Username','$ADGroup|Add-content
"\\Server\Sh ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : WriteError: (\\Server...sabledLogFiles\:S
tring) [Add-Content], IOException
+ FullyQualifiedErrorId : GetContentWriterIOError,Microsoft.PowerShell.Com
mands.AddContentCommand
Thanks in advance for any insight ...