INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Automatic Folder Migration

Automatic Folder Migration

(OP)
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 --> PowerShell

# Import AD into Quest

CODE --> PowerShell

import-module activedirectory

CODE --> PowerShell

# Find the date 365 days ago as a variable

CODE --> PowerShell

$old_p45_profile_path = "\\server\folder\profiles"

CODE --> PowerShell

$old_p45_home_path = "\\server\folder\home"

CODE --> PowerShell

$StartDate = [DateTime]::Now.AddDays(-365)

CODE --> PowerShell

# Search for users that have not logged in for 365 days or more AND their account was created more than 365 days ago

CODE --> PowerShell

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 --> PowerShell

# Read in List of Folders

CODE --> PowerShell

$rows=Import-Csv "inactiveusers.csv"

CODE --> PowerShell

foreach ($row in $rows)

CODE --> PowerShell

$profilePath = $row.ProfilePath

CODE --> PowerShell

takeown /F $profilePath /R /A

CODE --> PowerShell

foreach ($row in $rows)

CODE --> PowerShell

$homeDirectory = $row.HomeDirectory

CODE --> PowerShell

takeown /F $homeDirectory /R /A

CODE --> PowerShell

# Move Profiles

CODE --> PowerShell

foreach ($row in $rows) {

CODE --> PowerShell

if ($profilePath -EQ $NULL) {}

CODE --> PowerShell

else {robocopy $profilePath $old_p45_profile_path /MIR /MOVE /EFSRAW

CODE --> PowerShell

}

CODE --> PowerShell

# Move Home Directories

CODE --> PowerShell

foreach ($row in $rows) {

CODE --> PowerShell

if ($homeDirectory -EQ $NULL) {}

CODE --> PowerShell

else {robocopy $homeDirectory $old_p45_home_path /MIR /MOVE /EFSRAW }

CODE --> PowerShell

}

CODE --> PowerShell

}

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

Cheers!

RE: Automatic Folder Migration

Which cell?

CODE --> PowerShell

$rows = Import-Csv "inactiveusers.csv" | ? {$_.HomeDirectory -ne $null}

might work

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

Stop by the new Tek-Tips group at LinkedIn.

RE: Automatic Folder Migration

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close