$oldRootServerPath = "\\localhost\share\"
$newRootServerPath = "\\localhost\share2\"
$errorLog = "C:\Users\Administrator\Desktop\error.txt"
$copyLog = "C:\Users\Administrator\Desktop\copy.txt"
$sourceOU = "OU=TestCompany,OU=Clients,OU=zUsers,DC=myDC,DC=local"
function WriteError($message)
{
$stamp = Get-Date
$error = $stamp.ToString() + " -- " + $message
Add-Content -Path $errorLog -Value $error
}
function WriteCopy($message)
{
$stamp = Get-Date
$msg = $stamp.ToString() + " -- " + $message
Add-Content -Path $copyLog -Value $msg
}
function CopyHomeDirectory($source)
{
$newRoot = $source -replace [regex]::Escape($oldRootServerPath),$newRootServerPath
if (!(Test-Path $newRoot))
{
New-Item -ItemType Directory $newRoot
$acl = Get-Acl $source
Set-Acl -Path $newRoot -AclObject $acl
}
$folders = Get-ChildItem $source -Recurse | ?{$_.PSIsContainer}
foreach ($folder in $folders)
{
try {
$oldFolder = $folder.FullName
$acl = Get-Acl $oldFolder
$newFolder = $oldFolder -replace [regex]::Escape($oldRootServerPath),$newRootServerPath
if (!(Test-Path $newFolder)) {
New-Item -ItemType Directory $newFolder
Set-Acl -Path $newFolder -AclObject $acl
}
}
catch {
$eMessage = "Could not copy folder " + $oldFolder
WriteError($eMessage)
$errors++
}
}
$files = Get-ChildItem $source -Recurse | ?{!$_.PsIsContainer}
foreach ($file in $files)
{
try {
$oldFile = $file.FullName
$acl = Get-Acl $oldFile
$newFile = $oldFile -replace [regex]::Escape($oldRootServerPath),$newRootServerPath
Copy-Item $oldFile -Destination $newFile
Set-Acl -Path $newFile -AclObject $acl
}
catch {
$eMessage = "Could not copy file " + $oldFile
WriteError($eMessage)
$errors++
}
}
}
$users = Get-ADUser -Filter * -SearchBase $sourceOU -Properties homeDirectory | ?{$_.homeDirectory} | select sAMAccountName,homeDirectory
foreach ($user in $users)
{
$errors = 0
$path = $user.homeDirectory.ToString()
CopyHomeDirectory($path)
$copyMsg = "Copy completed for " + $path + " -- " + $errors + " errors occurred."
WriteCopy($copyMsg)
$newPath = $path -replace [regex]::Escape($oldRootServerPath),$newRootServerPath
Set-AdUser -Identity $user.sAMAccountName -HomeDrive "H:" -HomeDirectory $newPath
}
FirstName,SurName
Charles,Bronson
River,Phoenix
replace line 5 with the complete path to it:$userfile = 'C:\Users\Administrator\Desktop\Users.CSV'
and line 70 with$users = Import-CSV $userfile | % { Get-ADUser -Filter {GivenName -eq $_.FirstName -and SurName -eq $_.SurName} -Properties homeDirectory | ?{$_.homeDirectory} | select sAMAccountName,homeDirectory
$oldRootServerPath = "\\localhost\share\"
$newRootServerPath = "\\localhost\share2\"
$errorLog = "C:\Users\Administrator\Desktop\error.txt"
$copyLog = "C:\Users\Administrator\Desktop\copy.txt"
$userfile = 'C:\Users\Administrator\Desktop\name.CSV'
function WriteError($message)
{
$stamp = Get-Date
$error = $stamp.ToString() + " -- " + $message
Add-Content -Path $errorLog -Value $error
}
function WriteCopy($message)
{
$stamp = Get-Date
$msg = $stamp.ToString() + " -- " + $message
Add-Content -Path $copyLog -Value $msg
}
function CopyHomeDirectory($source)
{
$newRoot = $source -replace [regex]::Escape($oldRootServerPath),$newRootServerPath
if (!(Test-Path $newRoot))
{
New-Item -ItemType Directory $newRoot
$acl = Get-Acl $source
Set-Acl -Path $newRoot -AclObject $acl
}
$folders = Get-ChildItem $source -Recurse | ?{$_.PSIsContainer}
foreach ($folder in $folders)
{
try {
$oldFolder = $folder.FullName
$acl = Get-Acl $oldFolder
$newFolder = $oldFolder -replace [regex]::Escape($oldRootServerPath),$newRootServerPath
if (!(Test-Path $newFolder)) {
New-Item -ItemType Directory $newFolder
Set-Acl -Path $newFolder -AclObject $acl
}
}
catch {
$eMessage = "Could not copy folder " + $oldFolder
WriteError($eMessage)
$errors++
}
}
$files = Get-ChildItem $source -Recurse | ?{!$_.PsIsContainer}
foreach ($file in $files)
{
try {
$oldFile = $file.FullName
$acl = Get-Acl $oldFile
$newFile = $oldFile -replace [regex]::Escape($oldRootServerPath),$newRootServerPath
Copy-Item $oldFile -Destination $newFile -whatif
Set-Acl -Path $newFile -AclObject $acl -whatif
}
catch {
$eMessage = "Could not copy file " + $oldFile
WriteError($eMessage)
$errors++
}
}
}
$users = Import-CSV $userfile | % {Get-ADUser -Filter "sAMAccountName -eq '$($_.SamAccountName)'" -Properties homeDirectory | ?{$_.homeDirectory} | select sAMAccountName,homeDirectory }
foreach ($user in $users)
{
$errors = 0
$path = $user.homeDirectory.ToString()
CopyHomeDirectory($path)
$copyMsg = "Copy completed for " + $path + " -- " + $errors + " errors occurred."
WriteCopy($copyMsg)
$newPath = $path -replace [regex]::Escape($oldRootServerPath),$newRootServerPath
Set-AdUser -Identity $user.sAMAccountName -HomeDrive "H:" -HomeDirectory $newPath -whatif
}
Set-AdUser -Identity $user.sAMAccountName -HomeDrive "H:" -HomeDirectory $newPath -whatif
Copy-Item $oldFile -Destination $newFile -whatif
Set-Acl -Path $newFile -AclObject $acl -whatif
Am I correct that you want to change the script from searching in a specific OU to using specific names coming from the CSV?