Hi,
I've cobbled together some code which essentially searches AD for disabled accounts, matches the SamAccountName to a source folder and if a sub folder exists for that SamAccountName, copies the data to a new destination. Many many thanks to all those
all over the web that I've plagiarised!
Anywho....
The purpose is to move "leaver" data from live to cold storage and stop backing up data unnecessarily.
It works. In testing. Sort of.
The result that I get is that the output of SamAccountName is actually "@{SamAccountName=testuser}" rather than just "testuser". So when setting $Destination, the path is malformed. If I manually create the folder "@{SamAccountName=testuser}"
then it works just fine, as you would expect.....
Let me know if you can help?
------------------
Here is the error:
------------------
Copying data from @{SamAccountName=testuser} to D:\Powershell\CopiedProfiles\@{SamAccountName=testuser}
Copy-Item : Cannot find path 'D:\Powershell\Profiles\@{SamAccountName=testuser}' because it does not exist.
At D:\Powershell\CopyLeaverstoDestination.ps1:20 char:5
+ Copy-Item -Path $Source -Destination $Destination -Recurse -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (D:\Powershell\P...Name=testuser}:String) [Copy-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
-----------------
Here is the code:
-----------------
$Sources = Search-ADAccount -AccountDisabled -UsersOnly | select SamAccountName | sort SamAccountName
$Root = "D:\Powershell\CopiedProfiles"
foreach ($Source in ($Sources)) {
$Destination = Join-Path -Path $Root -ChildPath (Split-Path -Path $Source -Leaf)
if (Test-Path $Destination)
{
Write-Host $Source FOLDER ALREADY EXISTS....NOT COPYING THIS FOLDER...
}
Else
{
Write-Host Copying data from $Source to $Destination
Copy-Item -Path $Source -Destination $Destination -Recurse -Force
}
}