Remove PC from all schedule if not in active directory anymore

Comments

7 comments

  • Colby Bouma

    Are you looking to remove "PC01" from Targets or Target History?

    0
    Comment actions Permalink
  • Jean-François Guénet

    Ok here what ive done and it seem to work fine.  Ive enable this script every 15 minutes so if i delete a pc from AD it will automaticly delete the pc from all schedules in PDQ Deploy.  If that can serve for anyone ill be glad :) 

     

    #Set directory of the script

    $scriptPath = $MyInvocation.MyCommand.Path
    $ScriptDir = Split-Path $scriptpath

    # Delete all .txt in $ScriptDir if the exist
    IF (Test-Path $ScriptDir\*.txt)
    {
    Remove-Item $ScriptDir\*.txt
    }


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

    # Get all computername from PDQ Database
    $sql = "select Name from ScheduleComputers"
    $ComputersInPDQ = sqlite3.exe $db $sql

    # Sort all Computername and remove duplicate
    $ComputersInPDQ = $ComputersInPDQ | Select-Object -Unique | Sort-Object

    #Send all computername to text file
    Set-Content -Path $ScriptDir\ComputersInPDQList.txt -Value $ComputersInPDQ

    # Read the list so we can remove all text after the caracters "." because when we will find pc in AD we use the "name" property
    $ComputersInPDQList = Get-Content $ScriptDir\ComputersInPDQList.txt
    Foreach ($line in $ComputersInPDQList) {
    $index = $line.IndexOf(".")
    IF ($index -eq -1) {
    Add-Content -Path $ScriptDir\ComputersInPDQ.txt -Value $line.ToUpper()
    }
    Else {
    $line = $line.Substring(0,$line.IndexOf('.'))
    Add-Content -Path $ScriptDir\ComputersInPDQ.txt -Value $line.ToUpper()
    }
    }

    # We sort the list again and remove duplicate
    $ComputersInPDQ = Get-Content $ScriptDir\ComputersInPDQ.txt
    $ComputersInPDQ = $ComputersInPDQ | Select-Object -Unique | Sort-Object
    #Send all computername to text file
    Set-Content -Path $ScriptDir\ComputersInPDQSortDeduplicate.txt -Value $ComputersInPDQ



    # Get all computername from AD in OU "XXX"
    $ComputersInAD = Get-ADComputer -Filter * -SearchBase "OU=XXX, DC=XXX, DC=XXX, DC=XX, DC=XX" | Select-Object -ExpandProperty Name

    # Sort all Computername and remove duplicate
    $ComputersInAD = $ComputersInAD | Select-Object -Unique | Sort-Object
    Add-Content -Path $ScriptDir\ComputersInAD.txt -Value $ComputersInAD.ToUpper()


    # Check if PC in PDQ is in AD. If not we delete the pc from all schedules
    Foreach($linePDQ in Get-Content $ScriptDir\ComputersInPDQSortDeduplicate.txt) {

    # Dont do anything if the computername start with these PC name below. These pc are not in my domain. These are Workgroup pc that ive deploy software so we don't want to remove them
    IF($linePDQ.SubString(0,$linePDQ.length-2) -ne "ACTUALITE" -And $linePDQ.SubString(0,$linePDQ.length-2) -ne "ADOS" -And $linePDQ.SubString(0,$linePDQ.length-2) -ne "JEUNESSE" -And $linePDQ.SubString(0,$linePDQ.length-2) -ne "LAB" -And $linePDQ.SubString(0,$linePDQ.length-2) -ne "PCADOS" -And $linePDQ.SubString(0,$linePDQ.length-2) -ne "PROF" ) {
    IF (-Not [bool]((Get-Content $ScriptDir\ComputersInAD.txt) -like $linePDQ)) {
    # If we don't find the pc in AD we delete it from pdq scedules
    Write-Host "not found it"
    #Add ".domain name" because the computer in PDQ database are like that
    Write-Host $linePDQ".xxx.xxx.xxx.xxx"
    #We delete the pc from all schedule
    pdqdeploy deleteschedulehistory -Computer $linePDQ".xxx.xxx.xxx.xxx" -AllSchedules
    #We log wich pc we delete from all schedules
    $date = Get-Date -f "yyyy-MM-dd HH:mm"
    Add-Content -Path $ScriptDir\Logs\ComputersDeleteFromAllScedules.txt "$linePDQ was delete from all schedules in PDQ Deploy on $date"
    }
    }
    }

     

     

    One issue ive got is that there is on pc that don't want to be delete from PDQ Deploy database

    Any hints how to force a pc to be remove from the database ? 

     

    Thanks !

     

     

    0
    Comment actions Permalink
  • Colby Bouma

    If you don't mind me asking, why are you removing these computers from Target History? Target History is only used in conjunction with the feature "Stop deploying to targets once they succeed". If you're trying to avoid deploying to computers that are no longer in AD, I'd recommend using an OU as your Target. This way it will pull a list of computers from that OU every time the Schedule runs.

    1
    Comment actions Permalink
  • Jean-François Guénet

    Yes that's what im using but if for expemple ive got pc "TI01" in my AD and that it has all the software deploy on it it will be in the Target History List.  If i remove this pc "TI01" from AD it will still be in the Target History List.  If next time i had a pc to my domain and name it again "TI01" it will not get the software

     

    Am i clear:)  ?

    0
    Comment actions Permalink
  • Jean-François Guénet

    Do you know why some computers are not completly delete from all schedules  ?

    When im running the command : pdqdeploy deleteschedulehistory -Computer $linePDQ".xxx.xxx.xxx.xxx" -AllSchedules

    And that i check all my schedules i don't see the computer in the target history anymore

    But if im running this command bellow it still show my computer 4 times 

     

    # Connect to PDQ Database
    $db = "C:\ProgramData\Admin Arsenal\PDQ Deploy\Database.db"
    $sql = "select Name from ScheduleComputers where Name = 'TI18.domainename.qc.ca'"
    $ComputersInPDQ = sqlite3.exe $db $sql
    Write-Host $ComputersInPDQ
    TI18.domainename.qc.ca TI18.domainename.qc.ca TI18.domainename.qc.ca TI18.domainename.qc.ca

     

    Thanks for your help !

     

    0
    Comment actions Permalink
  • Jean-François Guénet

    Here what ive found these 4 entries belong to schedule ID #1

     

    $db = "C:\ProgramData\Admin Arsenal\PDQ Deploy\Database.db"
    $ScheduleName = "Deploy Default Software For All PC"
    $SQL = "SELECT ScheduleID FROM Schedules WHERE Name like '$ScheduleName';"
    $ScheduleID = sqlite3.exe $db $sql
    $ScheduleID

    1

    # Get target names from ScheduleId
    $sql = "select Name from ScheduleComputers where Scheduleid = $ScheduleId and Name = 'TI18.domainename.qc.ca';"
    $Computers = sqlite3.exe $db $sql
    $Computers

    TI18.domainename.qc.ca
    TI18.domainename.qc.ca
    TI18.domainename.qc.ca
    TI18.domainename.qc.ca

    In the gui interface of this schedule i don't see any Target History with TI18.domainename.qc.ca

    Could this be a bug ? 

    0
    Comment actions Permalink
  • Jean-François Guénet

    If that can help i did this command to retreive all fields in the ScheduleComputers table

    # Get target names from ScheduleId
    $sql = "select * from ScheduleComputers where Scheduleid = $ScheduleId and Name = 'TI18.domainename.qc.ca';"
    sqlite3.exe $db $sql
    56|1|TI18.domainename.qc.ca|1|1|4|
    62|1|TI18.domainename.qc.ca|0|0|16|
    68|1|TI18.domainename.qc.ca|0|0|19|
    80|1|TI18.domainename.qc.ca|0|0|37|

    0
    Comment actions Permalink

Please sign in to leave a comment.