Delete all Target History from a Schedule

Uncategorized

Today, we’re going to show how to delete all target history from a schedule using PowerShell.

Summer is almost over. Those of you who work in education may be feeling the stress of back-to-school related responsibilities. PDQ.com is here for you. We’ve got your back. (Unless you need a glass of whiskey, then you’re on your own there.)

If you’re using PDQ products, you’re already ahead of the game. We’ve got packages, and schedules, and collections! (oh my!)

We know how “fun” it is re-imaging and baselining all your computers. Getting everything installed perfectly can be a challenge.

When it comes time to re-image and baseline your computers, however, you’ll probably want to delete any existing target history from your schedules.

So sit back, relax, grab that whiskey and let’s “learn” you something new in PowerShell.

What is Target History?

What’s the target history, you ask? I’m so very glad you asked!

The target history is a record of deployments per target, per package, and per schedule.

Target history tab, schedule window

This target history keeps track of which targets have successfully received any package that is attached to a given schedule.

This allows us to selectively deploy packages when used hand-in-hand with another feature in order to prevent schedules from continually redeploying software to targets.

When Stop deploying to targets once they succeed is selected in the schedule options, it keeps track of deployments per package and per target.

Packages attached to a schedule will stop deploying after a successful deployment

Read more about this option in our documentation.

Sometimes, however, you may find yourself needing to redeploy some software to the same targets via the same schedule. In order to do this, you have two options:

  • Delete and recreate the schedule (boo!)
  • Delete the target history (hurray!)

One example of when you might want to do this is when you reimage a computer with the same host name. Without deleting the target history for a given schedule, the PDQ Deploy schedule thinks the target has already been deployed to successfully.

Fortunately, you can delete the target history directly from the GUI, or if you’re hip and cool and enjoy automation, then you can also use the PDQ Deploy command line interface (CLI).

But, since the CLI is limited to only removing one target at a time, how would you go about removing all target histories from a schedule?!

Enter PowerShell! (cue superhero theme music)

Delete all Target History from a Schedule

With PowerShell, we can find out which computers are part of the schedule’s target history. Then, we can proceed to individually remove each target from the schedule via a loop.

  1. Select Database
    Here, we simply need to identify which database that we want to use. Since we’re working with PDQ Deploy, I’m going to point it to the PDQ Deploy database.

    $db = "C:\ProgramData\Admin Arsenal\PDQ Deploy\Database.db"
    C:\ProgramData\Admin Arsenal\PDQ Deploy\Database.db
  2. Get Schedule ID
    Next, we use sqlite to query the Schedules table to find out the Schedule ID. If you already know the schedule ID, you can skip this step.

    $ScheduleName = "Stop exploding you cowards!"
    $SQL = "SELECT ScheduleID FROM Schedules WHERE Name like '$ScheduleName';"
    $ScheduleID = sqlite3.exe $db $sql
    $ScheduleID
    Use sqlite to query the Schedules table to find out the Schedule ID
  3. Get Target Names with Schedule ID
    Using the result from the previous step, we’ll use the Schedule ID to find out which targets currently have a history for the schedule.

    # Get target names from ScheduleId
    $sql = "select Name from ScheduleComputers where Scheduleid = $ScheduleId;"
    $Computers = sqlite3.exe $db $sql
    $Computers
    Use the Schedule ID to find out which targets currently have a history for the schedule
  4. Loop through list of Names and Delete History
    Now, we can finally loop through each target and remove the target history for our schedule.

    # Remove schedule history for each target and scheduleid) 
    foreach ($Target in $Computers) {
        pdqdeploy deleteschedulehistory -Computer $Target -Schedule $ScheduleID
    }
    Loop through each target and remove the target history for our schedule

Putting it all together

Now that we’ve covered each section, let’s throw it all together for something that’s easier to copy/paste.

#
#   Delete all schedule history for a Schedule
#

$db = "C:\ProgramData\Admin Arsenal\PDQ Deploy\Database.db"

# Get Schedule ID from Schedule Name
$ScheduleName = "Heartbeast"
$sql = "SELECT ScheduleID FROM Schedules WHERE Name like '$ScheduleName';"
$ScheduleID = sqlite3.exe $db $sql

# Get target names from ScheduleId
$sql = "select Name from ScheduleComputers where Scheduleid = $ScheduleId;"
$Computers = sqlite3.exe $db $sql

# Remove schedule history for each target and scheduleid)
foreach ($Target in $Computers) {
   pdqdeploy deleteschedulehistory -Computer $Target -Schedule $ScheduleID
}

Wrapping up

Now, you can go delete those target histories like it’s your job… since it very well may actually be your job. In which case, good job!

Don’t forget to join me every 1st and 3rd Tuesday of the month at 10:00 AM, MDT for a live demonstration of PowerShell!

Happy PowerShelling!

See this in action in a short video:

Leave a Reply

Your email address will not be published. Required fields are marked *