JAMF Computer Search

HGonzalez
New Contributor

Hello Everyone,

I made this program awhile back as my first try using JAMF's API's and this was more for fun. The purpose of this program to is to allow administrators and those who have atleast read-only permissions to JAMF Pro to search for computer objects via computer name or serial number (if they are different). Feel free to modify this to meet your needs! All you need to do is replace the "YouSubDomain" string with whatever your JAMF subdomain is and you can also edit the "Software" section on line 213, to search for specific applications installed on that device. I left 2 examples, incase you're not familiar with Python, that you can mimic for new applications you want to see. Also, if you don't want to first prompt to appear, please comment out or delete lines 256-271, before you begin to edit or the numbers will be off for the above.

1b1044c3a5904a00b5dc47ae2eb07dec

    #!/usr/bin/python

    import os, sys, commands, urllib2, json, base64, time
    from getpass import getpass
    import subprocess as sb
    from subprocess import PIPE

    def JAMFcall(x):
        request = urllib2.Request(x)
        request.add_header('Accept', 'application/json')
        request.add_header('Authorization', 'Basic ' + base64.b64encode(UserName + ':' + PassWord))
        response = urllib2.urlopen(request)
        response_json = json.loads(response.read())
        return response_json
    def Search(x,y):
        if x == "Computer Name":
            SearchCriteria = y.replace(" ", "%20")
            Buttons = """buttons {"OK"}"""
            try:
                MatchUrl = "https://"YourSubDomain".jamfcloud.com/JSSResource/computers/match/{}".format(SearchCriteria)
                NameResponse = JAMFcall(MatchUrl)
                SerialUrl = "https://"YourSubDomain".jamfcloud.com/JSSResource/computers/match/{}".format(NameResponse["computers"][0]["serial_number"])
                SerialResponse = JAMFcall(SerialUrl)
                ComputerId = SerialResponse["computers"][0]["id"]
                ComputerUrl = "https://"YourSubDomain".jamfcloud.com/JSSResource/computers/id/{}".format(ComputerId)
                IdResponse = JAMFcall(ComputerUrl)
                LocalAccounts = IdResponse["computer"]["groups_accounts"]["local_accounts"]
                Software = IdResponse["computer"]["software"]["applications"]
                DeviceName = SerialResponse["computers"][0]["name"]
                SerialNumberReturn = NameResponse["computers"][0]["serial_number"]
                ManagedStatus = IdResponse["computer"]["general"]["remote_management"]["managed"]
                OSVersion = IdResponse["computer"]["hardware"]["os_version"]
                ADBoundStatus = IdResponse["computer"]["hardware"]["active_directory_status"]
                IPAddress = IdResponse["computer"]["general"]["last_reported_ip"]
                DeviceModel = IdResponse["computer"]["hardware"]["model"]
                DeviceCPU = IdResponse["computer"]["hardware"]["processor_type"]
                TotalRAM = IdResponse["computer"]["hardware"]["total_ram"]
                BatteryLevel = IdResponse["computer"]["hardware"]["battery_capacity"]
                TotalStorage = IdResponse["computer"]["hardware"]["storage"][0]["drive_capacity_mb"]
                Accounts = ""
                Adobe2019 = ""
                Adobe2018 = ""
                K4Downloader = ""
                for b in LocalAccounts:
                    Accounts += str(b["name"])
                    Accounts += str(" |")
                    Accounts += str(" Admin:")
                    Accounts += str(b["administrator"])
                    Accounts += str("| FV Enabled:")
                    Accounts += str(b["filevault_enabled"])
                    Accounts += "
		"
                for c in Software:
                    if c["name"] == "Adobe Premiere Pro CC 2019.app":
                        Adobe2019 = "Adobe Premiere 2019 Detected."
                    elif c["name"] == "Adobe Premiere Pro CC 2018.app":
                        Adobe2018 = "Adobe Premiere 2018 Detected."
                    elif c["name"] == "4K Video Downloader.app":
                        K4Downloader = "4K Downloader Detected."

                StartPrompt = """
                ***OS Info*** 
                Device Name: {}
                Serial Number: {}
                Device Managed By JAMF: {}
                Device OS Version: {}
                Device Local Accounts:
                {}

                ***Network Info***
                Active Directory Bound: {}
                IP Address: {}

                ****Hardware Info****
                Device Model: {}
                Device CPU: {}
                Device Total RAM: {}MB
                Device Battery Level: {}%
                Device Total Storage: {}MB

                ***Software***
                {}
                {}
                {}
                """.format(DeviceName, SerialNumberReturn, ManagedStatus, OSVersion, Accounts, ADBoundStatus, IPAddress, DeviceModel, DeviceCPU, TotalRAM, BatteryLevel, TotalStorage, Adobe2019, Adobe2018, K4Downloader)
                ReturnValue = sb.Popen("""osascript -e 'display dialog "{}" {} with title "JAMF Computer Finder  default button 1"'""".format(StartPrompt, Buttons), shell=True)
            except urllib2.HTTPError as e:
                ErrorOccured = sb.call("""osascript -e 'display dialog "Oh no! Something went wrong. Your error code is {}, please use the list below to find the cause of the error:

                    404 - Your search term could not be found.
                    401 - Your JAMF credentials are 
                    unauthorized for this search.
                    502 - Bad Gateway, there is an issue on the 
                    server end." with title "JAMF Computer Finder" {} default button 1'""".format(e.code, Buttons), shell=True)
            except IndexError:
                ErrorOccured = sb.call("""osascript -e 'display dialog "Oh no! Your search term could not be found, please try again." buttons {"OK"} default button 1 with title "JAMF Computer Finder"'""", shell=True)

        elif x == "Serial Number":
            Buttons = """buttons {"OK"}"""
            try:
                MatchUrl = "https://"YourSubDomain".jamfcloud.com/JSSResource/computers/match/{}".format(y)
                SerialResponse = JAMFcall(MatchUrl)
                ComputerId = SerialResponse["computers"][0]["id"]
                ComputerUrl = "https://"YourSubDomain".jamfcloud.com/JSSResource/computers/id/{}".format(ComputerId)
                IdResponse = JAMFcall(ComputerUrl)
                LocalAccounts = IdResponse["computer"]["groups_accounts"]["local_accounts"]
                Software = IdResponse["computer"]["software"]["applications"]
                LocalAccounts = IdResponse["computer"]["groups_accounts"]["local_accounts"]
                Software = IdResponse["computer"]["software"]["applications"]
                DeviceName = SerialResponse["computers"][0]["name"]
                SerialNumberReturn = y
                ManagedStatus = IdResponse["computer"]["general"]["remote_management"]["managed"]
                OSVersion = IdResponse["computer"]["hardware"]["os_version"]
                ADBoundStatus = IdResponse["computer"]["hardware"]["active_directory_status"]
                IPAddress = IdResponse["computer"]["general"]["last_reported_ip"]
                DeviceModel = IdResponse["computer"]["hardware"]["model"]
                DeviceCPU = IdResponse["computer"]["hardware"]["processor_type"]
                TotalRAM = IdResponse["computer"]["hardware"]["total_ram"]
                BatteryLevel = IdResponse["computer"]["hardware"]["battery_capacity"]
                TotalStorage = IdResponse["computer"]["hardware"]["storage"][0]["drive_capacity_mb"]
                Accounts = ""
                Adobe2019 = ""
                Adobe2018 = ""
                K4Downloader = ""
                for b in LocalAccounts:
                    Accounts += str(b["name"])
                    Accounts += str(" |")
                    Accounts += str(" Admin:")
                    Accounts += str(b["administrator"])
                    Accounts += str("| FV Enabled:")
                    Accounts += str(b["filevault_enabled"])
                    Accounts += "
"
                for c in Software:
                    if c["name"] == "Adobe Premiere Pro CC 2019.app":
                        Adobe2019 = "Adobe Premiere 2019 Detected."
                    elif c["name"] == "Adobe Premiere Pro CC 2018.app":
                        Adobe2018 = "Adobe Premiere 2018 Detected."
                    elif c["name"] == "4K Video Downloader.app":
                        K4Downloader = "4K Downloader Detected."

                StartPrompt = """
                ***OS Info*** 
                Device Name: {}
                Serial Number: {}
                Device Managed By JAMF: {}
                Device OS Version: {}
                Device Local Accounts:
                {}

                ***Network Info***
                Active Directory Bound: {}
                IP Address: {}

                ****Hardware Info****
                Device Model: {}
                Device CPU: {}
                Device Total RAM: {}MB
                Device Battery Level: {}%
                Device Total Storage: {}MB

                ***Software***
                {}
                {}
                {}
                """.format(DeviceName, SerialNumberReturn, ManagedStatus, OSVersion, Accounts, ADBoundStatus, IPAddress, DeviceModel, DeviceCPU, TotalRAM, BatteryLevel, TotalStorage, Adobe2019, Adobe2018, K4Downloader)
                ReturnValue = sb.Popen("""osascript -e 'display dialog "{}" {} with title "JAMF Computer Finder"'""".format(StartPrompt, Buttons), shell=True)

            except urllib2.HTTPError as e:
                ErrorOccured = sb.call("""osascript -e 'display dialog "Oh no! Something went wrong. Your error code is {}, please use the list below to find the cause of the error:

                    404 - Your search term could not be found.
                    401 - Your JAMF credentials are 
                    unauthorized for this search.
                    502 - Bad Gateway, there is an issue on the 
                    server end." with title "JAMF Computer Finder" {} default button 1'""".format(e.code, Buttons), shell=True)
            except IndexError:
                ErrorOccured = sb.call("""osascript -e 'display dialog "Oh no! Your search term could not be found, please try again." buttons {"OK"} default button 1 with title "JAMF Computer Finder"'""", shell=True)
        elif x == "Local Account":
            SearchCriteria = y.replace(" ", "%20")
            Buttons = """buttons {"OK"}"""
            try:
                MatchUrl = "https://"YourSubDomain".jamfcloud.com/JSSResource/computers/match/name/{}".format(SearchCriteria)
                NameResponse = JAMFcall(MatchUrl)
                SerialUrl = "https://"YourSubDomain".jamfcloud.com/JSSResource/computers/match/{}".format(NameResponse["computers"][0]["serial_number"])
                SerialResponse = JAMFcall(SerialUrl)
                ComputerId = SerialResponse["computers"][0]["id"]
                ComputerUrl = "https://"YourSubDomain".jamfcloud.com/JSSResource/computers/id/{}".format(ComputerId)
                IdResponse = JAMFcall(ComputerUrl)
                LocalAccounts = IdResponse["computer"]["groups_accounts"]["local_accounts"]
                Software = IdResponse["computer"]["software"]["applications"]
                DeviceName = SerialResponse["computers"][0]["name"]
                SerialNumberReturn = NameResponse["computers"][0]["serial_number"]
                ManagedStatus = IdResponse["computer"]["general"]["remote_management"]["managed"]
                OSVersion = IdResponse["computer"]["hardware"]["os_version"]
                ADBoundStatus = IdResponse["computer"]["hardware"]["active_directory_status"]
                IPAddress = IdResponse["computer"]["general"]["last_reported_ip"]
                DeviceModel = IdResponse["computer"]["hardware"]["model"]
                DeviceCPU = IdResponse["computer"]["hardware"]["processor_type"]
                TotalRAM = IdResponse["computer"]["hardware"]["total_ram"]
                BatteryLevel = IdResponse["computer"]["hardware"]["battery_capacity"]
                TotalStorage = IdResponse["computer"]["hardware"]["storage"][0]["drive_capacity_mb"]
                Accounts = ""
                Adobe2019 = ""
                Adobe2018 = ""
                K4Downloader = ""
                for b in LocalAccounts:
                    Accounts += str(b["name"])
                    Accounts += str(" |")
                    Accounts += str(" Admin:")
                    Accounts += str(b["administrator"])
                    Accounts += str("| FV Enabled:")
                    Accounts += str(b["filevault_enabled"])
                    Accounts += "
		"
                for c in Software:
                    if c["name"] == "Adobe Premiere Pro CC 2019.app":
                        Adobe2019 = "Adobe Premiere 2019 Detected."
                    elif c["name"] == "Adobe Premiere Pro CC 2018.app":
                        Adobe2018 = "Adobe Premiere 2018 Detected."

                StartPrompt = """
                ***OS Info*** 
                Device Name: {}
                Serial Number: {}
                Device Managed By JAMF: {}
                Device OS Version: {}
                Device Local Accounts:
                {}

                ***Network Info***
                Active Directory Bound: {}
                IP Address: {}

                ****Hardware Info****
                Device Model: {}
                Device CPU: {}
                Device Total RAM: {}MB
                Device Battery Level: {}%
                Device Total Storage: {}MB

                ***Software***
                {}
                {}
                {}
                """.format(DeviceName, SerialNumberReturn, ManagedStatus, OSVersion, Accounts, ADBoundStatus, IPAddress, DeviceModel, DeviceCPU, TotalRAM, BatteryLevel, TotalStorage, Adobe2019, Adobe2018, K4Downloader)
                ReturnValue = sb.Popen("""osascript -e 'display dialog "{}" {} with title "JAMF Computer Finder  default button 1"'""".format(StartPrompt, Buttons), shell=True)
            except urllib2.HTTPError as e:
                ErrorOccured = sb.call("""osascript -e 'display dialog "Oh no! Something went wrong. Your error code is {}, please use the list below to find the cause of the error:

                    404 - Your search term could not be found.
                    401 - Your JAMF credentials are 
                    unauthorized for this search.
                    502 - Bad Gateway, there is an issue on the 
                    server end." with title "JAMF Computer Finder" {} default button 1'""".format(e.code, Buttons), shell=True)
            except IndexError:
                ErrorOccured = sb.call("""osascript -e 'display dialog "Oh no! Your search term could not be found, please try again." buttons {"OK"} default button 1 with title "JAMF Computer Finder"'""", shell=True)

    JAMFHelper = "/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
    WindowType = "hud"
    StartPrompt = "Welcome to the JAMF Computer Finder, if you have any suggestions for improvement, please let your JAMF Administrator know."
    Button1 = "Cancel"
    Button2 = "Continue"
    BorderTitle = "JAMF Computer Finder"
    AlignDescription = "left" 
    AlignHeading = "center"
    DefaultButton = "1"
    JAMFIcon = "/Library/Application Support/JAMF/Jamf.app/Contents/Resources/AppIcon.icns"
    userChoice = sb.call("{} -windowType {} -lockHUD -title {} -icon {} -defaultButton {} -description {} -alignDescription {} -alignHeading {} -button1 {}".format(JAMFHelper, WindowType, BorderTitle, JAMFIcon, DefaultButton, StartPrompt, AlignDescription, AlignHeading, Button2), shell=True)

    UserName = sb.Popen("""osascript -e 'set MyName to text returned of (display dialog "Enter your JAMF Username" default answer "" with title "JAMF Computer Finder" buttons {"OK"} default button 1)'""", shell=True, stdout=PIPE).communicate()[0].rstrip()
    PassWord = sb.Popen("""osascript -e 'set MyPass to text returned of (display dialog "Enter your JAMF Password:" with hidden answer default answer "" with title "JAMF Computer Finder" buttons {"OK"} default button 1)'""", shell=True, stdout=PIPE).communicate()[0].rstrip()
    url = "Empty"
    Continue = "Yes"

    while Continue == "Yes":
        SearchTerm = sb.Popen("""osascript -e 'set MyButton to the button returned of (display dialog "What will you be searching with?"with title "JAMF Computer Finder" buttons {"Computer Name", "Serial Number"})'""", shell=True, stdout=PIPE).communicate()[0].rstrip()
        SearchTerm.replace(" ", "")
        SearchCriteria = sb.Popen("""osascript -e 'set MyName to text returned of (display dialog "Please Enter Your Search Below:" default answer "" with title "JAMF Computer Finder" buttons {"OK"} default button 1)'""", shell=True, stdout=PIPE).communicate()[0].rstrip()
        Search(SearchTerm, SearchCriteria)
        time.sleep(5)
        Continue = str(sb.Popen("""osascript -e 'set MyCoice to the button returned of (display dialog "Would You Like To Search Again?" with title "JAMF Computer Finder" buttons {"No", "Yes"} default button 2)'""", shell=True, stdout=PIPE).communicate()[0].rstrip())
0 REPLIES 0