I have a script that I have been working on, and I make it so far then it fails:<\/p>\n
I am looking for the script to import a CSV that has three fields: Username,EmployeeID,UserPrincipalName<\/p>\n
The file is called users.csv<\/p>\n
Items the script is supposed to accomplish<\/p>\n
Disable the Account
\nReset PWD
\nExpire the account to todays date
\nClear all groups except Domain Users
\nAPPEND the description filed with text
\nMove the account to the Terminated OU<\/p>\n
Then the script goes to 0365 items<\/p>\n
Remove all 0365 Licences
\nCheck to see if the account is on litigation hold - if the true move to the next line if False set a 720 hold
\nRemove from GAL<\/p>\n
## This portion of the script is AD\n##Please change your path for the file locations \n##\n$NewPWD = \"WeLightTheWay007!\"\nImport-Module ActiveDirectory ##Import AD module to run AD commands from Powershell \n$UsersToDIsable=Import-Csv -Path C:\\scripts\\Users.csv ## Import the CSV into a variable object for ease of use\nForEach ($UserToDisable in $UsersToDisable) \n{\n $User = $UserToDisable.UserName\n Disable-ADAccount -Identity $User #Disables the user in AD\n Set-ADAccountPassword -Identity $User -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $NewPWD -Force) #Sets Password to the value of $NewPWD\n $expire = Get-Date #Set Today's date as Expiration date.\n Set-ADAccountExpiration -Identity $User -DateTime $expire\n #Get all Groups the user is a member of\n $Groups = Get-ADUser -Identity $User -Properties MemberOf\n # Remove all group memberships (will leave Domain Users as this is NOT in the MemberOf property returned by Get-ADUser)\n foreach ($group in $Groups)\n {\n Remove-ADGroupMember -Identity $group -Members $User\n }\n #Set User's Description\n Get-ADUser $User -Properties Description | ForEach-Object { Set-ADUser $_ -Description \"$($_.Description)Part of AD Cleanup Project: $(Get-Date -Format 'MM-dd-yyyy') by ${env:UserDomain}\\${env:UserName}\" }\n Get-ADUser -Identity $User | Move-ADObject –TargetPath \"OU=Terminated,OU=CORPORATE,OU=ALG Users,DC=XXXX,DC=com\" ###### CHANGE THIS TO MATCH YOUR OUs DN!!\n}\n\n#############I KNOW THE SCRIPT WORK TO HERE\n\n##This Portion of the scripts is O365 and uses the UPN Filed in the csv file\n#This portion removes all 0365 Licence\n\nForEach ($user in $Users)\n{\n $User = Get-MsolUser -UserPrincipalName $_.UserPrincipalName\n $Skus = $User.licenses.AccountSkuId\n Set-MsolUserLicense -UserPrincipalName $User.UserPrincipalName -RemoveLicenses $Skus\n }\n\n##This portion is For Litigation Hold\n\nForEach ($user in $Users)\n{\n \n if ($user.LitigationHoldEnabled -eq $false)\n {\n Set-Mailbox -identity $User -LitigationHoldEnabled:$true -LitigationHoldDuration 730\n }\n \n}\n\n##This portion removes user from GAL\n\nForEach ($user in $users)\n{\n Set-MailContact -Identity $user -HiddenFromAddressListsEnabled $true\n }\n<\/code><\/pre>","upvoteCount":4,"answerCount":16,"datePublished":"2020-02-20T16:33:01.000Z","author":{"@type":"Person","name":"travisdhahn","url":"https://community.spiceworks.com/u/travisdhahn"},"suggestedAnswer":[{"@type":"Answer","text":"I have a script that I have been working on, and I make it so far then it fails:<\/p>\n
I am looking for the script to import a CSV that has three fields: Username,EmployeeID,UserPrincipalName<\/p>\n
The file is called users.csv<\/p>\n
Items the script is supposed to accomplish<\/p>\n
Disable the Account
\nReset PWD
\nExpire the account to todays date
\nClear all groups except Domain Users
\nAPPEND the description filed with text
\nMove the account to the Terminated OU<\/p>\n
Then the script goes to 0365 items<\/p>\n
Remove all 0365 Licences
\nCheck to see if the account is on litigation hold - if the true move to the next line if False set a 720 hold
\nRemove from GAL<\/p>\n
## This portion of the script is AD\n##Please change your path for the file locations \n##\n$NewPWD = \"WeLightTheWay007!\"\nImport-Module ActiveDirectory ##Import AD module to run AD commands from Powershell \n$UsersToDIsable=Import-Csv -Path C:\\scripts\\Users.csv ## Import the CSV into a variable object for ease of use\nForEach ($UserToDisable in $UsersToDisable) \n{\n $User = $UserToDisable.UserName\n Disable-ADAccount -Identity $User #Disables the user in AD\n Set-ADAccountPassword -Identity $User -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $NewPWD -Force) #Sets Password to the value of $NewPWD\n $expire = Get-Date #Set Today's date as Expiration date.\n Set-ADAccountExpiration -Identity $User -DateTime $expire\n #Get all Groups the user is a member of\n $Groups = Get-ADUser -Identity $User -Properties MemberOf\n # Remove all group memberships (will leave Domain Users as this is NOT in the MemberOf property returned by Get-ADUser)\n foreach ($group in $Groups)\n {\n Remove-ADGroupMember -Identity $group -Members $User\n }\n #Set User's Description\n Get-ADUser $User -Properties Description | ForEach-Object { Set-ADUser $_ -Description \"$($_.Description)Part of AD Cleanup Project: $(Get-Date -Format 'MM-dd-yyyy') by ${env:UserDomain}\\${env:UserName}\" }\n Get-ADUser -Identity $User | Move-ADObject –TargetPath \"OU=Terminated,OU=CORPORATE,OU=ALG Users,DC=XXXX,DC=com\" ###### CHANGE THIS TO MATCH YOUR OUs DN!!\n}\n\n#############I KNOW THE SCRIPT WORK TO HERE\n\n##This Portion of the scripts is O365 and uses the UPN Filed in the csv file\n#This portion removes all 0365 Licence\n\nForEach ($user in $Users)\n{\n $User = Get-MsolUser -UserPrincipalName $_.UserPrincipalName\n $Skus = $User.licenses.AccountSkuId\n Set-MsolUserLicense -UserPrincipalName $User.UserPrincipalName -RemoveLicenses $Skus\n }\n\n##This portion is For Litigation Hold\n\nForEach ($user in $Users)\n{\n \n if ($user.LitigationHoldEnabled -eq $false)\n {\n Set-Mailbox -identity $User -LitigationHoldEnabled:$true -LitigationHoldDuration 730\n }\n \n}\n\n##This portion removes user from GAL\n\nForEach ($user in $users)\n{\n Set-MailContact -Identity $user -HiddenFromAddressListsEnabled $true\n }\n<\/code><\/pre>","upvoteCount":4,"datePublished":"2020-02-20T16:33:01.000Z","url":"https://community.spiceworks.com/t/script-review-somewhere-its-failing/752182/1","author":{"@type":"Person","name":"travisdhahn","url":"https://community.spiceworks.com/u/travisdhahn"}},{"@type":"Answer","text":"