Open a computer ID record in a browser via script

dan_gregson
New Contributor II

This would be ideal for our helpdesk/field technicians to launch the script from self service so when they are either remotely logged in or at the machine physically they can verify that the machine is in the correct site and check other things in the id page. I'm new to scripting so passing parameters ect is not my specialty.
Something like this where jamf.com is our jss

!/bin/sh

/usr/local/jamf/bin/jamf recon | grep '<computer_id>' > '/private/var/jss_computer_id.xml'

open -a /Applications/Safari.app https://Jamf.com:8443/computers.html?id='<computer_id>'&o=r

I know this doesn't pass the computer_id, but it does put the id locally in the xml file

1 ACCEPTED SOLUTION

ryan_ball
Valued Contributor

@dan.gregson Here ya go. No need to hard code the JSS URL in there:

#!/bin/bash

jssURL=$(defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url | sed s'/.$//')
id=$(jamf recon | grep '<computer_id>' | xmllint --xpath xmllint --xpath '/computer_id/text()' -)

open -a "Safari" "$jssURL/computers.html?id=$id&o=r"

exit 0

As always with jamf commands you should run with sudo when testing.

View solution in original post

6 REPLIES 6

ryan_ball
Valued Contributor

@dan.gregson Here ya go. No need to hard code the JSS URL in there:

#!/bin/bash

jssURL=$(defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url | sed s'/.$//')
id=$(jamf recon | grep '<computer_id>' | xmllint --xpath xmllint --xpath '/computer_id/text()' -)

open -a "Safari" "$jssURL/computers.html?id=$id&o=r"

exit 0

As always with jamf commands you should run with sudo when testing.

dvasquez
Valued Contributor

Your handy script helped me simplify a gathering logs script I found. The script was mostly correct but the conversion and curl command was a little overdone. Your script helped me keep it simple. So thank you!

dan_gregson
New Contributor II

That is awesome. Thanks so much.

Hugonaut
Valued Contributor II

@ryan.ball

now thats clean script!!!! this is what i came up with for dans other post....then I came here.... lol...I REALLY GOTTA THINK BASH!

#!/usr/bin/osascript

try

    set UNAME to "root"
    set PASSW to "password"

    set jsslinksuffix to "&o=r"

    set rawGREPid to do shell script "sudo /usr/local/bin/jamf recon | grep '<computer_id>'" user name UNAME password PASSW with administrator privileges

    set FourCharacterID to text 14 thru 17 of rawGREPid

    if FourCharacterID contains "<" then set ThreeCharacterID to text 14 thru 16 of rawGREPid

    if FourCharacterID contains "<" then
        do shell script "open -a /Applications/Safari.app https://jss.jamf.com:8443/computers.html?id=" & quoted form of (ThreeCharacterID & jsslinksuffix)
    else
        do shell script "open -a /Applications/Safari.app https://jss.jamf.com:8443/computers.html?id=" & quoted form of (FourCharacterID & jsslinksuffix)
    end if

end try
________________
Looking for a Jamf Managed Service Provider? Look no further than Rocketman
________________


Virtual MacAdmins Monthly Meetup - First Friday, Every Month

cwaldrip
Valued Contributor

Great script @ryan.ball, but you have to wait for Recon to finish running and that can take a couple of minutes. It's almost faster to get the S#, open the jss, and search based on the S#.

I guess you could have a policy run a script to grep the computer_id (shouldn't have to do it more than once, but maybe have the script run monthly) and save it in a file somewhere that you can read from a self service policy later (if it's not there, then it can do the long method). I might look into that a shot... (eat my own dog food)

ryan_ball
Valued Contributor

@cwaldrip Everybody wants instant satisfaction :)

This will do:

#!/bin/bash

# Pass in user credentials in parameters 4 and 5
apiUser="$4"
apiPass="$5"

jssURL=$(defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url | sed s'/.$//')
serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

id=$(curl -sku "$apiUser:$apiPass" "$jssURL/JSSResource/computers/serialnumber/$serial" | xmllint --xpath xmllint --xpath '/computer/general/id/text()' - 2>/dev/null)
if [[ -z "$id" ]]; then
    echo "Could not determine Mac's Jamf Pro ID; exiting."
    exit 1
else
    open -a "Safari" "$jssURL/computers.html?id=$id&o=r"
fi

exit 0