mrbios Posted May 18, 2015 Posted May 18, 2015 I'm after a script which will go through the following steps: 1) find all disabled users within a given OU 2) pull the home folder path from AD for those disabled users 3) Move all the disabled home users home folders, most likely using robocopy, to an archive folder Anyone have anything that can do that? I can find scripts for finding all disabled users in AD, but nothing that can find them all from a specific OU, so I'm failing at the first hurdle. Alternatively if anyone knows of any programs that can do this rather than scripts that'd save some effort!
halbaradkenafin Posted May 18, 2015 Posted May 18, 2015 I'm after a script which will go through the following steps: 1) find all disabled users within a given OU 2) pull the home folder path from AD for those disabled users 3) Move all the disabled home users home folders, most likely using robocopy, to an archive folder Anyone have anything that can do that? I can find scripts for finding all disabled users in AD, but nothing that can find them all from a specific OU, so I'm failing at the first hurdle. Alternatively if anyone knows of any programs that can do this rather than scripts that'd save some effort! I've got a script which does almost exactly that, here's a modification that will do what you want: $Users = Get-ADUser -Filter {Enabled -eq "False" -SearchBase "OU=,OU=SomeParent,OU=Other Parent,DC=Domain,DC=Local" New-Item -Path "\\Server\Destination\Folder\Here" -ItemType Directory -Force -WhatIf Foreach ($User in $Users) { Move-Item -Path $User.HomeDirectory -Destination "\\Server\Destination\Folder\Here" -WhatIf } Update the paths and run it as is first to ensure it's going to do what you want and then remove the -WhatIf and run it again to get it to do it's thing. 1
mrbios Posted May 18, 2015 Author Posted May 18, 2015 (edited) Awesome, I'm nearly there.... getting this error running your script: + $Users = Get-ADUser <<<< -Filter {Enabled -eq "False" -SearchBase "OU=Y2007,OU=Students,OU=Archway Users,DC=Archway,DC=local"} + CategoryInfo : ParserError: ( [Get-ADUser], ADFilterParsingException + FullyQualifiedErrorId : Error parsing query: 'Enabled -eq "False" -SearchBase "OU=Y2007,OU=Students,OU=Archway Users,DC=Archway,DC=local"' Error Message: 'Operator Not supported: -SearchBase' at position: '21'.,Microsoft.ActiveDirectory.Management.Comm ands.GetADUser Before that it was complaining about a missing closed bracket, so i may have put that in the wrong place (I assumed it needed to be at the end of the first line) EDIT: AH the close bracket was meant to be before searchbase EDIT2: Ok I understand how this is working now but I'm obviously getting something wrong wit hthe path as it's complaining at me with this error: Move-Item : Cannot bind argument to parameter 'Path' because it is null. Edited May 18, 2015 by mrbios
halbaradkenafin Posted May 18, 2015 Posted May 18, 2015 (edited) Try commenting out the move-item line (# at the start of the line) and then adding in Write-Host $User.homeDirectory and see if it produces what it's supposed to. If it doesn't then wrapping it in $() should work. Edit: Need to add "-Properties HomeDirectory" to the end of the Get-ADUsers query as it doesn't return that by default and so when you try to call it from the Move-Item command it has no value. Edited May 18, 2015 by halbaradkenafin 1
mrbios Posted May 18, 2015 Author Posted May 18, 2015 (edited) I appear to end up with a large blank output after the "What if: Performing operation "Create Directory" on Target "Destination:" but the users in the given OU definitely have a home directory set EDIT: Ah just seen your edit as i came to say that i just ran this command: PS H:\> Get-ADUser -Filter {Enabled -eq "False"} -SearchBase "OU=Disabled,OU=Y2007,OU=Students,OU=Archway Users,DC=Archw ay,DC=local" -properties scriptpath, homedrive, homedirectory | ft Name, scriptpath, homedrive, homedirectory and they were defeinitely showing up, i'll run your edit now Edited May 18, 2015 by mrbios
mrbios Posted May 18, 2015 Author Posted May 18, 2015 (edited) Wohoo it works, thank you! Didn't expect to get a working answer so fast! Cleanup on isle AD! EDIT: A tidy 236gb archived off the main storage and no longer taking up space on the backups. Edited May 18, 2015 by mrbios
Garacesh Posted May 18, 2015 Posted May 18, 2015 (edited) I'm always a fan of not creating arrays where they're not needed. There's no sense creating an array of users if you're only going to refer to it the once. Might as well just run the ForEach on the command itself. ForEach ($HomePath in (Get-ADUser -Filter {Enabled -eq $false} -Property HomeDirectory).HomeDirectory) { [indent]Move-Item -Path $HomePath -Destination "\\Server\Destination\Folder\Here"[/indent] } I know you've already sorted it now, just chipping in my 2 cents/best practise. (Worth noting my script doesn't include a searchbase, so it will scan for all disabled users in every OU, which probably ain't your intention. Just cobbled it together in 2 minutes) Edited May 18, 2015 by Garacesh
halbaradkenafin Posted May 18, 2015 Posted May 18, 2015 Wohoo it works, thank you! Didn't expect to get a working answer so fast! It's been a slow day for me and I spend way too much time working with Powershell.
halbaradkenafin Posted May 18, 2015 Posted May 18, 2015 I'm always a fan of not creating arrays where they're not needed. There's no sense creating an array of users if you're only going to refer to it the once. Might as well just run the ForEach on the command itself. ForEach ($HomePath in (Get-ADUser -Filter {Enabled -eq $false} -Property HomeDirectory).HomeDirectory) { [indent]Move-Item -Path $HomePath -Destination "\\Server\Destination\Folder\Here"[/indent] } I know you've already sorted it now, just chipping in my 2 cents/best practise. (Worth noting my script doesn't include a searchbase, so it will scan for all disabled users in every OU, which probably ain't your intention. Just cobbled it together in 2 minutes) You can go a step further and make it a onliner if you wanted to Get-ADUser -Searchbase "some ou path" -Filter {Enabled -"False"} -Properties HomeDirectory | Foreach { Move-Item $_.HomeDirectory "\\somewhere else"} But for something that's likely to be run again (probably around the same time each year) then I'd go for a slightly longer and more readable script, perhaps even with some calculation in it for which year group to archive so it can be set as a scheduled task to run on X date each year and just ignored after that (documented somewhere of course).
Garacesh Posted May 18, 2015 Posted May 18, 2015 (edited) perhaps even with some calculation in it for which year group to archive so it can be set as a scheduled task to run on X date each year and just ignored after that (documented somewhere of course). [string]$Year = ("Y" + (Get-Date).AddYears(-5)) ForEach ($HomePath in (Get-ADUser -Filter {Enabled -eq $false} -SearchBase "OU=Disabled,OU=$Year,OU=Students,OU=Archway Users,DC=Archway,DC=Local -Property HomeDirectory).HomeDirectory) { Move-Item -Path $HomePath -Destination "\\Server\Destination\Folder\$Year" } (Annoyingly, after saying I hate creating redundant arrays, you kinda have to for $Year as you can't run "-SearchBase OU=(Get-Date)" Edited May 18, 2015 by Garacesh
halbaradkenafin Posted May 18, 2015 Posted May 18, 2015 [string]$Year = ("Y" + (Get-Date).AddYears(-5)) ForEach ($HomePath in (Get-ADUser -Filter {Enabled -eq $false} -SearchBase "OU=Disabled,OU=$Year,OU=Students,OU=Archway Users,DC=Archw -Property HomeDirectory).HomeDirectory) {Move-Item -Path $HomePath -Destination "\\Server\Destination\Folder\$Year" } (Annoyingly, after saying I hate creating redundant arrays, you kinda have to for $Year as you can't run "-SearchBase OU=(Get-Date)" As the SearchBase is a string you can wrap the Get-Date in $() to make it evaluate as a variable first so you could do: Get-ADuser -Searchbase "OU=Y$((Get-Date).AddYears(-5)),OU=Path,DC=Domain,DC=Local" -Filter {Enabled -eq "False"} =Proprties HomeDirectory
Garacesh Posted May 18, 2015 Posted May 18, 2015 (edited) Today I learned! I'm sure I've tried that before (I wrote a similar script to archive and remove user accounts of year groups who have left) but I'll keep that in mind. Thanks! Edit: Ah, that magic $ makes all the difference. That I did not try. I always forget you can do that.. I seem to subconsciously only associate $ with arrays (Unless it's $_.Property in Where-Object) Still, I think I'll keep my OU/Folder removal as $Year as before you execute there's a Write-Warning that displays exactly where it's going to modify before you even do anything (and then requires you to type "Continue" before it executes.. Probably being over-cautious but ah well) Edited May 18, 2015 by Garacesh
halbaradkenafin Posted May 18, 2015 Posted May 18, 2015 Edit: Ah, that magic $ makes all the difference. That I did not try. I always forget you can do that.. I seem to subconsciously only associate $ with arrays (Unless it's $_.Property in Where-Object) You're probably thinking of @() which is for declaring arrays (empty or otherwise) Still, I think I'll keep my OU/Folder removal as $Year as before you execute there's a Write-Warning that displays exactly where it's going to modify before you even do anything (and then requires you to type "Continue" before it executes.. Probably being over-cautious but ah well) That's the great thing about Powershell, there are plenty of ways to handle a problem and you go with what works for you and your environment.
ascott2 Posted May 18, 2015 Posted May 18, 2015 @{} is a hashtable @() is an array Been caught out on that one many a time.
halbaradkenafin Posted May 18, 2015 Posted May 18, 2015 I thought @() was hash tables? Or is that @{}? Hashtables are @{}, and I'm finding that I use them for more than I'd expected as they've got some neat tricks that work well.
Garacesh Posted May 18, 2015 Posted May 18, 2015 I discovered hashtables whilst writing a fire emergency script.. It interrogates our door controllers and writes out [sTAFF NAME], [WHERE FOB WAS LAST USED]. I haven't used them elsewhere yet, but they do seem like they could be incredibly handy.
ascott2 Posted May 18, 2015 Posted May 18, 2015 You can also have nested hashtables if you really want to get complicated.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now