Hello,
I’m trying to run a script using AZ copy to delete files older than X days on fileshare, but the script is deleting everything, showing the message:
Unable to convert value “0001-01-01 00:00:00 +0000 UTC” to type “System.DateTime”. Error: "The string was not recognized as a valid DateTime. There is a
unknown word starting at index 26."
No C:\xxxxxxxxxxxxxxx:2
+ $dateTime = [DateTime]$listFiles[$i].Data.Replace('LastModifie …
the script I’m using:
Clear-Host
$dateExecution = Get-Date
$limit = $dateExecution.AddDays(-30)$application = “C:XXXXXXX\azcopy.exe”
$env:AZCOPY_JOB_PLAN_LOCATION=“C:XXXXXX”;
$env:AZCOPY_CONCURRENCY_VALUE = “AUTO”;
$env:AZCOPY_LOG_LOCATION=“C:XXXXXXX”;$URLSTORAGE=“https:XXXXXXXXXXXXXXXXXX”
$SAS=“?XXXXXXXXXXXXXXG”;$Logfile = “C:\XXXXXXXXXXXXXXXXXXXXXX_$dateRef.log”
$expurgoFile = “C:\XXXXXXXXXXXXXXX_”+$dateExecution.tostring(“yyyyMMdd”)+“.txt”
$listFile = “C:\aXXXXXXXXXXXXXXX_”+$dateExecution.tostring(“yyyyMMdd”)+“.csv”Start-Transcript -Append $Logfile
Write-Host “Data/Hora Execucao:” $dateExecution.tostring(“yyyy-MM-dd HH:mm:ss”)
Write-Host “Deleting files older than $limit”$commandline = “$application list “”$URLSTORAGE/$SAS”" --properties LastModifiedTime | Out-File -FilePath $listFile"
invoke-expression $commandline$delimiter = “;”
$Header = “Arquivo”,“Data”,“Conteudo”
$listArquivos = Import-Csv $listFile -Delimiter $delimiter -Header $Headerfor ($i = 0; $i -le $listArquivos.count-1; $i++) {
$fileName = $listArquivos[$i].Arquivo.Replace(‘INFO: ‘,’’)
$dataHora = [DateTime]$listArquivos[$i].Data.Replace(‘LastModifiedTime: ‘,’’).Replace(’ +0000 GMT’,‘’)
if ($dataHora -le $limit){
Write-Host $i “- File to be deleted:” $fileName “-” $dataHora.tostring(“MM/dd/yyyy HH:mm:ss”)
Add-Content -Path $expurgoFile -Value $fileName
}
}if (Test-Path -Path $expurgoFile) {
$commandline = “remove “”$URLSTORAGE$SAS”" --recursive --log-level=INFO --list-of-files “”$expurgoFile"“”
Start-Process -FilePath $application -ArgumentList $commandline -NoNewWindow -Wait
Write-Host “files older than $limit successfully deleted”
} else {
Write-Host “No files older than $limit”
}Stop-Transcript