Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatic Folder Migration

Status
Not open for further replies.

ldoddrell

Technical User
May 14, 2012
1
GB
Afternoon all,

I have the following script that is designed to discover old AD accounts, filter the results to a certain user group (accounts starting 'P4'), export to CSV, forcefully give my security group ownership of the directories and then use RoboCopy to migrate the folders to a server share.

RoboCopy is where I'm facing a silly issue I can't find a straight forward answer to. Not all the users have HomeDirectories in AD and so when RoboCopy comes across a null value, it takes places it into my command but unwittingly thinks it's the sourcedir.

Here is the code:

Code:
# Import AD into Quest
Code:
import-module activedirectory
Code:
# Find the date 365 days ago as a variable
Code:
	$old_p45_profile_path = "\\server\folder\profiles"
Code:
	$old_p45_home_path = "\\server\folder\home"
Code:
	$StartDate = [DateTime]::Now.AddDays(-365)
Code:
# Search for users that have not logged in for 365 days or more AND their account was created more than 365 days ago
Code:
	Search-ADAccount -UsersOnly -AccountInactive -TimeSpan 365.00:00:00 | %{Get-QADUser $_.ObjectGuid -CreatedBefore $StartDate} | select samaccountname, name, whenCreated, lastLogonTimeStamp, profilePath, homeDirectory | where-object {$_.samaccountname -like "P4*"}| export-csv c:\output\inactiveusers.csv -NoTypeInformation
Code:
# Read in List of Folders
Code:
	$rows=Import-Csv "inactiveusers.csv"
Code:
		foreach ($row in $rows)
Code:
			$profilePath = $row.ProfilePath
Code:
				takeown /F $profilePath /R /A
Code:
		foreach ($row in $rows)
Code:
			$homeDirectory = $row.HomeDirectory
Code:
				takeown /F $homeDirectory /R /A
Code:
# Move Profiles
Code:
	foreach ($row in $rows) {
Code:
		if ($profilePath -EQ $NULL) {}
Code:
		else {robocopy $profilePath $old_p45_profile_path /MIR /MOVE /EFSRAW
Code:
                             }
Code:
# Move Home Directories
Code:
	foreach ($row in $rows) {
Code:
		if ($homeDirectory -EQ $NULL) {}
Code:
		else {robocopy $homeDirectory $old_p45_home_path /MIR /MOVE /EFSRAW }
Code:
				}
Code:
}

How do I get RoboCopy to ignore a null cell in the CSV and move to the next cell?

Cheers!
 
I meant to add that that code you posted is pretty ugly. Lots of room for improvement there, including parameter validation, error checking, and using only a single ForEach loop.


Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top