Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Dudes! Great site. You've saved me hours and hours...and I have just started using your site. I've already passed your URL onto my entire company. Keep up the awesome work. Bingo-bango..."

Geography

Where in the world do Tek-Tips members come from?
ldoddrell (TechnicalUser)
14 May 12 9:38
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!
58sniper (MIS)
14 May 12 15:04
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.

58sniper (MIS)
15 May 12 9:11
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.

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!

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