Powershell : Query Server Backup Tasks (on a batch of servers)

This script will focus around backups of Domain Controllers but can be cusomized for any type of server, just update the code where required!

In this example we have Domain Controller backups being completed by "Windows Server Backup"  as this is a reliable backup application that can also restore your backups without issues, if it is not installed on the server then you can add that role to any Windows server with the command:

Install-WindowsFeature Windows-Server-Backup

When installed you can setup the backup job remotely via the MMC and when you choose your backup options this will then create a scheduled task that performs the backup of the server as below:



This during the initial setup will be set to daily, which may be fine, if you wish to change this you can look for the scheduled task in the Backup folder and update this task to the desired intervals but updating the "schedule" tab.

When running this task ensure you use a service account and not your own account, that will cause issues when you change your password!

When this has been setup and you backups are running, I wanted an easy way to get the schedule from the servers with a script so you could see the "next runtime" easily.

Script : ADDSBackupChecker.ps1

# Get all Domain Controllers from Active Directory
$DomainControllers = Get-ADDomainController -Filter * | Select-Object Name, HostName

# Create an array to store results
$Results = @()
foreach ($DC in $DomainControllers) {
    try {
        Write-Host "`nChecking $($DC.Name)..." -ForegroundColor Cyan        

        # Query tasks directly using schtasks without WinRM
        $taskList = schtasks.exe /query /s $DC.Name /fo CSV | ConvertFrom-Csv        

        # Find Windows Backup related tasks
        $backupTasks = $taskList | Where-Object { 
            $_.'TaskName' -like '*WindowsBackup*' -or 
            $_.'TaskName' -like '*Windows Backup*' 
        }       
        if ($backupTasks) {
            foreach ($task in $backupTasks) {
                Write-Host "Found backup task: $($task.'TaskName')" -ForegroundColor Green                

                # Get detailed info for the specific task

                $taskDetail = schtasks.exe /query /s $DC.Name /tn $task.'TaskName' /v /fo CSV | ConvertFrom-Csv               
                $Results += [PSCustomObject]@{
                    ServerName = $DC.Name
                    TaskName = $task.'TaskName'
                    TaskState = $taskDetail.'Status'
                    Schedule = $taskDetail.'Schedule Type'
                    NextRun = $taskDetail.'Next Run Time'
                    LastRun = $taskDetail.'Last Run Time'
                }
            }
        }
        else {
            Write-Host "No Windows Backup tasks found" -ForegroundColor Yellow
            $Results += [PSCustomObject]@{
                ServerName = $DC.Name
                TaskName = "Not Found"
                TaskState = "Not Found"
                Schedule = "N/A"
                NextRun = "N/A"
                LastRun = "N/A"
            }
        }
    }
    catch {
        Write-Warning "Error accessing $($DC.Name): $($_.Exception.Message)"
        $Results += [PSCustomObject]@{
            ServerName = $DC.Name
            TaskName = "Error"
            TaskState = "Error"
            Schedule = "Access Error: $($_.Exception.Message)"
            NextRun = "N/A"
            LastRun = "N/A"
        }
    }
}

# Display results in a formatted table
Write-Host "`nResults Summary:" -ForegroundColor Yellow
$Results | Format-Table -AutoSize -Wrap

# Export to CSV
$Results | Export-Csv -Path "WindowsBackupSchedule.csv" -NoTypeInformation

# Display summary of servers with issues
$issueServers = $Results | Where-Object { $_.TaskState -eq "Error" -or $_.TaskName -eq "Not Found"
}
if ($issueServers) {
    Write-Host "`nServers with Issues:" -ForegroundColor Red
    $issueServers | Format-Table ServerName, TaskState, Schedule -AutoSize
}

This will show you the output on screen as below:


You will also get a CSV file outputted to the folder where the script is executed from for convienance.

Previous Post Next Post

نموذج الاتصال