I got this awesome script online somebody has done a very well job writing it. everything works just fine, except for when I try to do an entire Network Drive it doesn’t allow me to…
# set variables
$Now = Get-Date
$Days = "2000"
$TargetFolder = "E:\" #logs folder on the server
$LastWrite = $Now.AddDays(-$Days)
$BackupLocation = "C:\Backup" #change this line to your backup destination
# create new directory for file movement
$NewFolder=New-Item -ItemType Directory -Path "$BackupLocation\.\$((Get-Date).ToString('yyyy-MM-dd'))"
# Move directory structure to archive
Get-ChildItem $TargetFolder -Directory -Recurse | Copy-Item -Destination $NewFolder
# get files older than $Days and move them to previously created folder
Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"} | Copy-item -destination ($_.DirectoryName -replace $TargetFolder,$NewFolder)
# 7zip part. Install 7zip on the server in order to make this part workable
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
$Target = "$BackupLocation\$($NewFolder.name).zip"
# 7ziparchive command
sz a -mx=9 $Target $NewFolder
#check if zip has been placed on the share and delete logs folder
$CheckAndDelete= if (Test-Path $Target){
Remove-Item $NewFolder -recurse -force -confirm:$FALSE
}
Start-Sleep -s 10
# end
Since, I didn’t write this script I need an expert help to modified as per my need. I know its possible anyhow I need an expert advice.
Sorry for the late reply If you are still looking for the answer check with the below script I have done some minor changes in it and tested in my system which is working perfectly.
# set variables
$Now = Get-Date
$Days = "10"
$TargetFolder = "E:\\" #logs folder on the server
$LastWrite = $Now.AddDays(-$Days)
$BackupLocation = "C:\\bu\\" #change this line to your backup destination
$NewFolder = "$BackupLocation\$((Get-Date).ToString('yyyy-MM-dd'))\\"
# create new directory for file movement
if (-not(test-path $NewFolder)) {
New-Item -ItemType Directory -Path $NewFolder
}
# Move directory structure to archive
Get-ChildItem $TargetFolder -Directory -Recurse | Copy-Item -Destination $NewFolder -Verbose
# get files older than $Days and move them to previously created folder
Get-Childitem $TargetFolder -Recurse -File | Where {$_.LastWriteTime -le "$LastWrite"} |
foreach{Copy-Item $_.fullname -Destination ($_.fullname -replace $TargetFolder,$NewFolder) -verbose}
# 7zip part. Install 7zip on the server in order to make this part workable
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
$Target = "$BackupLocation\$(($lastwrite).ToString('yyyy-MM-dd')).zip"
# 7ziparchive command
sz a -mx=9 $Target $NewFolder
#check if zip has been placed on the share and delete logs folder
$CheckAndDelete = if (Test-Path $Target) {
Remove-Item $NewFolder -recurse -force -confirm:$FALSE
$RootName = 'most recent 10-29'
$LogDate = Get-Date -Format d
$FileName = $RootName + $LogDate + '.txt'
}
Start-Sleep -s 10
# end
$Now = Get-Date
$Days = “10”
$TargetFolder = “E:\” #logs folder on the server
$LastWrite = $Now.AddDays(-$Days)
$BackupLocation = “C:\bu\” #change this line to your backup destination
$NewFolder = “$BackupLocation$((Get-Date).ToString(‘yyyy-MM-dd’))\”
create new directory for file movement
if (-not(test-path $NewFolder)) {
New-Item -ItemType Directory -Path $NewFolder
}