Unable to delete local backups

  • 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" -DisableNameChecking

    set-executionpolicy unrestricted

    set-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

    }

    Thanks.

  • Are you getting errors? Show something more please.

    -- Gianluca Sartori

  • How does it fail?

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • BTW This is not a free peer review service. I am happy to help with specific issues though.

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply