sthildas Posted November 14, 2016 Posted November 14, 2016 I am trying to write a script that when users leave and are moved to a Leavers OU, a scheduled script runs and exports their email to PST, moves their Home Folder to an acrchive and clears all their custom attributes. The PST export works, the clearing of the custom attributes works but I cannot get the moving the home folders to work, any assistance would be appreciated. # Import Modules Import-PSSession $Session -AllowClobber -DisableNameChecking # Variables $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://server.domain.co.uk/PowerShell/ -Authentication Kerberos $OU = "domain/leavers/staffleavers" $Search = "OU=StaffLeavers,OU=Leavers,DC=domain,DC=co,DC=uk" $filepath = "\\server\archive$\PST" $movesource = "\\server\homefolders$" + $userhome.name $movedestination = "\\server\archive$" # Move leavers email to PST on Archive share Get-Mailbox -OrganizationalUnit $OU | foreach { New-MailboxExportRequest -Mailbox $_.Alias -FilePath ($FilePath+($_.Alias)+".pst")} # Move leavers home folder to Archive share Get-ADUser -Filter * -SearchBase $Search | ForEach-Object ($_.samaccountname) { if (test-path($movesource)) { Move-Item -Path $movesource -Destination $movedestination -force } } # Clear all leavers custom attributes Get-ADUser -Filter * -SearchBase $Search | Set-ADUSer -Clear "extensionAttribute1" , "extensionAttribute2" , "extensionAttribute3" , "extensionAttribute4" , "extensionAttribute15"
HPlum78 Posted November 15, 2016 Posted November 15, 2016 Where is the var $userhome.name being defined or am I just not seeing it?
sthildas Posted November 15, 2016 Author Posted November 15, 2016 Your correct this is were I'm struggling, our folder structure is \\server\share\sAMAccountName. The Get-ADUser is pulling in the correct users but I cannot get this to pass to the Move-Item and only move those users home folders. I am assuming the error lies in the $MoveSource but I dont understand why it is moving all users home folders and not just the user folders that have been selected with the Get-ADUser. I would have assumed if the $username.name wasnt being processed then it wouldnt have moved any folders and would have failed. Any help in pointing me in the right direction appreciated
HPlum78 Posted November 15, 2016 Posted November 15, 2016 (edited) So the reason it is not throwing an error is down to the construct of that $movesource var what is being passed is the root as and as far as the script is concerned that's fine.... anyhow something like this will get you on the way # Variables $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://server.domain.co.uk/PowerShell/ -Authentication Kerberos $OU = "domain/leavers/staffleavers" $Search = "OU=StaffLeavers,OU=Leavers,DC=domain,DC=co,DC =uk" $user = Get-ADUser -Filter * -SearchBase $Search $filepath = \\server\archive$\PST $Users = Get-ADUser -Filter -SearchBase $Search $movesource = \\server\homefolders$\ $movedestination = "\\server\archive$" # Move leavers home folder to Archive share $users | % { $fldrLoc = "$movesource$($_.SamAccountName)" write-host $flsrLoc Move-Item -Path $FldrLoc -Destination $movedestination -force } ##### End Its rough round the edges but its a starter for 10, the write-host bit can go its just a visual way to check the folder path looks like it should. Could do with some error catching in this type of script just in case it starts to do something bat sh*t crazy! it should be enough to get you on your way mind. I will call out % is short hand (alias) for ForEach-Object Edited November 15, 2016 by HPlum78 Missing $moveDest
sthildas Posted November 15, 2016 Author Posted November 15, 2016 I have changed the script to $Search = "OU=StaffLeavers,OU=Leavers,DC=domain,DC=co,DC=uk" $movesource = "\\server\HomeFolders$\$($user.samaccountname)" $movedest = "\\server\share$" $Users = Get-ADUser -Filter {(Enabled -eq $false)} -SearchBase $Search -Properties HomeDirectory | select -property SamAccountName,homeDirectory foreach($User in $Users) { # Move leavers home folder to Archive share Move-Item -Path $movesource -Destination $movedest -force -WhatIf } This seems to find the correct users but it pulls in the same user over and over again. We have 11 users in the $Search OU and when I run the above it tries to move one user 11 times. What if: Performing the operation "Move Directory" on target "Item: \\filesrv1\homefolders$\user1 Destination: \\server\share$\user1". What if: Performing the operation "Move Directory" on target "Item: \\filesrv1\homefolders$\user1 Destination: \\server\share$\user1". What if: Performing the operation "Move Directory" on target "Item: \\filesrv1\homefolders$\user1 Destination: \\server\share$\user1". What if: Performing the operation "Move Directory" on target "Item: \\filesrv1\homefolders$\user1 Destination: \\server\share$\user1". What if: Performing the operation "Move Directory" on target "Item: \\filesrv1\homefolders$\user1 Destination: \\server\share$\user1". What if: Performing the operation "Move Directory" on target "Item: \\filesrv1\homefolders$\user1 Destination: \\server\share$\user1". What if: Performing the operation "Move Directory" on target "Item: \\filesrv1\homefolders$\user1 Destination: \\server\share$\user1". What if: Performing the operation "Move Directory" on target "Item: \\filesrv1\homefolders$\user1 Destination: \\server\share$\user1". What if: Performing the operation "Move Directory" on target "Item: \\filesrv1\homefolders$\user1 Destination: \\server\share$\user1". What if: Performing the operation "Move Directory" on target "Item: \\filesrv1\homefolders$\user1 Destination: \\server\share$\user1". What if: Performing the operation "Move Directory" on target "Item: \\filesrv1\homefolders$\user1 Destination: \\server\share$\user1". I am still missing something? I tried with your suggestion and it works, could you help me understand why mine above still doesnt? I know I am so close.
HPlum78 Posted November 15, 2016 Posted November 15, 2016 $movesource = \\server\HomeFolders$\$($user.samaccountname)" Thing is you are setting the above var outside of your loop for moving the folder, you need to set the var after each iteration. Like I have done here:- $users | % { $fldrLoc = "$movesource$($_.SamAccountName)" write-host $flsrLoc Move-Item -Path $FldrLoc -Destination $movedestination -force } don't forget that its a top down run, so any declarations at the top of your script are static, any var that's needs to be redefined has to be done inside of your looping logic. (think that makes sense!)
sthildas Posted November 15, 2016 Author Posted November 15, 2016 Thanks very much, it works great. I have applied the same logic or what I believe to be the same logic to my PST archive. I dont want it to export the mailboxes to PST if the PST file already exists so I added an if statement so that it would check to see if the PST file existed and if it did then it wouldnt run. # Variables $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange.domain.co.uk/PowerShell/ -Authentication Kerberos $OU = "domain.co.uk/leavers/staffleavers" $filepath = "\\server\archive$\PST" # Move leavers email to PST on Archive share Get-Mailbox -OrganizationalUnit $OU | foreach {$Destination = $FilePath+($_.Alias)+".pst"} if (-not (Test-Path $Destination)) { Get-Mailbox -OrganizationalUnit $OU | foreach { New-MailboxExportRequest -Mailbox $_.Alias -FilePath ($FilePath+($_.Alias)+".pst")} } I am sure this could be cleaned up but it works. Again thanks for the help.
HPlum78 Posted November 15, 2016 Posted November 15, 2016 (edited) No problem, I often write scripts to do a job and then sit saying to myself I could do this or that to make it better. Thing is I produce a lot of scripts for doing various things and sometimes I have to just let them go and add it to the backlog. Never gets done mind. Edited November 15, 2016 by HPlum78 1
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