Welcome › Forums › General PowerShell Q&A › variable is not being reset correctly
- This topic has 3 replies, 3 voices, and was last updated 10 months, 3 weeks ago by
Participant.
-
AuthorPosts
-
-
March 4, 2020 at 5:46 am #207777
Hi
I have this script to disable users in AD and do a couple of other things along the way
the issue that I’m having here is that $Manager is not being reset to nothing.
as result when disabling 1 user I get the manager from the previous disabled userwhen disabling multiple users the first accounts get the manager of the last disabled user
and the 2nd user to be disabled is getting the manager from the 1st user in the list.
what do I need to do to correct this?PowerShell123456789101112131415#select CSV file that contains the people where the groups needs to be removed#csv file needs to be a comma separated file$users = import-csv c:\temp\toRemove.csv$date= Get-Date -Format “yyyy-MM-dd”$lastworkdate = (get-date).AddDays(-1).ToString(“yyyy-MM-dd”)$DisabledOU = “OU=Disabled,OU=Regions,DC=test,DC=com”foreach ($user in $users){#get manager$manager = (get-aduser (get-aduser $user.samAccountName -Properties manager).manager).Name#get all the groups this user is member of an paste this in Note section$groups =Get-ADPrincipalGroupMembership $user.SamAccountNameSet-ADUser $user.samAccountName -Replace @{info=$groups.name -join “[crayon-600edbb2df5d8807784076 inline="true" ]r -
March 5, 2020 at 6:02 am #208242
Any thoughts on this?
-
March 5, 2020 at 8:56 am #208266
I don’t know if this is related, but when I pasted your code in to ISE the following line had a syntax error
Set-ADUser –Identity $user.SamAccountName -add @{“extensionattribute15″= $((Get-Date).ToShortDateString())}
I changed it to this and the code ran, however, i was unable to replicate the issue you are having.
Set-ADUser –Identity $user.SamAccountName -add @{'extensionattribute15'=(Get-Date).ToShortDateString()}
What happens if you put the $manager=$null as the first line of code inside the foreach loop instead of at the end of the loop?
-
March 5, 2020 at 2:15 pm #208374
I would recommend using the breakpoint feature on ISE and putting a breakpoint on the $manager = $null to see if the script actually hits the manager variable and nulls it.
If you are using VSCode you could also use the debugger and watch the $manager variable to see what happens to it.
As a work around you could do something before you set the manager like:
PowerShell12345if($mamanger){Remove-Variable Manager}This would remove the variable if its set.
-
-
AuthorPosts
- The topic ‘variable is not being reset correctly’ is closed to new replies.