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:
How do I get RoboCopy to ignore a null cell in the CSV and move to the next cell?
Cheers!
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!