Quantcast
Channel: SQLServerCentral » Programming » Powershell » Latest topics
Viewing all articles
Browse latest Browse all 467

Unable to delete local backups

$
0
0
Hi,I am unable to delete the local 4 backup folders from multiple servers.Can you suggest whether my query is doing anything wrong?import-module "SQLPS" -DisableNameCheckingset-executionpolicy unrestrictedset-location "C:\backups\";$ErrorActionPreference = "SilentlyContinue";$servers = get-content "C:\backups\serverlist.txt";$filePathOutput = "C:\backups\output_old_bkp.txt";$cyclecount = 1;try{foreach ($server in $servers){ "****SERVER NAME**** : $($server)" | out-file $filePathOutput -append"----------------------------------------------------------------------" | out-file $filePathOutput -append $database1 = Remove-Item "\\$server\D$\SQL80\MSSQL'$prd1\backup\database1" -force $database2 = Remove-Item "\\$server\D$\SQL80\MSSQL'$prd1\backup\database2" -force $database3 = Remove-Item "\\$server\D$\SQL80\MSSQL$prd1\backup\database3" -force $database4 = Remove-Item "\\$server\D$\SQL80\MSSQL$prd1\backup\database4" -force if ($database1 -ne $NULL) { write-host "Deleting Folder $database1" -ForegroundColor "DarkRed" Remove-Item $database1.FullName -force | out-null } if ($database2 -ne $NULL) { write-host "Deleting Folder $database2" -ForegroundColor "DarkRed" Remove-Item $database2.FullName -force | out-null } if ($database3 -ne $NULL) { write-host "Deleting Folder $database3" -ForegroundColor "DarkRed" Remove-Item $database3.FullName -force | out-null } if ($database4 -ne $NULL) { write-host "Deleting Folder $database4" -ForegroundColor "DarkRed" Remove-Item $database4.FullName -force | out-null } Write-Host "Processing Server: $($server), Cycle : $($cyclecount)" -Fore Red Invoke-Sqlcmd -ServerInstance $server -Database "master" -Connectiontimeout 20 -QueryTimeout 40 | Out-File -filePath "C:\backups\output_old_bkp.txt" -Append #Invoke-Sqlcmd -ServerInstance $server -Connectiontimeout 20 -QueryTimeout 40 -Database "master" -InputFile "C:\backups\drop_local_bkp_remote.sql” | Out-File -filePath "C:\backups\output_old_bkp.txt" -Append Write-Host "Processing completed on Server: $($server)" -Fore Green; $cyclecount += 1 } } catch { <# $err = $_.Exception write-output $err.Message while( $err.InnerException ) { $err = $err.InnerException write-output $err.Message;#> Write-Warning "Error processing command"; $error[0]; $Exception = $Error[0].Exception }finally { $ErrorActionPrefernce = $eap }

Viewing all articles
Browse latest Browse all 467

Trending Articles