Hi all
I am a newbie to Powershell, and with a lot of Googling I managed to get this script put together that does what I need for moving files.
I want to use this script to mirror folders and files, and I know that Powershell cannot do Mirror, so I want to replace the move part with a Robocopy job, but cannot get it to work
function Write-Log($string)
{
$outStr = "" + $Timestamp +" "+$string
Write-Output $outStr
}
Function Start-Moving
{
Param
(
$JobFolderFrom,
$JobFolderTo,
$JobLog
)
$Timestamp = Get-Date -Format "yyyy-MM-ddTHH-mm-ss"
$Log = "C:\support\scripts\logs\" + $JobLog + " " + $Timestamp + ".log"
Start-Transcript -path $Log -append -Force -NoClobber
try {
Write-Log "------------ Start of Log ------------"
#Write-Log ""
# get all file objects to use in moving
$files=Get-ChildItem -path $JobFolderFrom -Recurse -File
# Move the file and its folder.
$files |
ForEach-Object{
Write-Log " Moving File --> $_. To $JobFolderTo"; Move-Item $_.Fullname -Destination $JobFolderTo
}
# output statistics
Write-Output "**********************"
Write-Output "Number of old files moved: $($files.Count)"
Write-Log "------------- End of Log -------------"
}
Stop-Transcript
}
Start-Moving -JobTime -5 -JobFolderFrom 'C:\Test1' -JobFolderTo 'C:\Test2' -JobLog 'Test1'