Help with first time API script

easyedc
Valued Contributor II

In the process of moving computers from current Production JSS to new JSS. We're not migrating, we're re-enrolling to start clean. The move is easy, but I'm trying to clean up my old JSS as they migrate.

I figured an easy way to do this would be through the API, but I've not used API for anything yet, and am scratching my head as to what is failing. My script is below.

#!/bin/bash

name=`scutil --get HostName`
curl -X DELETE -u jamfadmin:password https://jsstest.org.com:8443/JSSResource/computers/id/$name

exit 0

My output seams to show that it's not grabbing the host name as expected but I'm not sure why. I'm sure it's blatantly obvious, but for the life of me, I'm not seeing it.

sh --verbose API Delete.sh 
#!/bin/bash

name=`scutil --get HostName`
scutil --get HostName
curl -X DELETE -u jamfadmin:password https://jsstest.org.com:8443/JSSResource/computers/id/$name
<html>
<head>
   <title>Status page</title>
</head>
<body style="font-family: sans-serif;">
<p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Not Found</p>
<p>The server has not found anything matching the request URI</p>
<p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5">here</a>.<br>
Please continue your visit at our <a href="/">home page</a>.
</p>
</body>
</html>

exit 0
1 ACCEPTED SOLUTION

pcrandom
Contributor

I tried your original script, but per other's earlier suggestions changed it to use ComputerName for the variable and for the API to delete by name:

#!/bin/bash

name=`scutil --get ComputerName`
curl -X DELETE -u jamfadmin:password https://jsstest.org.com:8443/JSSResource/computers/name/$name

exit 0

This worked for me. I then tried the serial number script that you had, and I got the same error as you, but at least in my case, I know why it failed. Are you by chance running the SN script on a test VM? When I ran it, I got an error because I was testing it against a VM, and the serial number that the VM gave me had a slash in it, which messes up the command.

A colleague of mine has been using the JSS API more than me, and he's using UDID as also suggested above. So you should be able to do the following:

#!/bin/bash
udid=$(system_profiler SPHardwareDataType | awk '/UUID/ { print $3; }')
echo "$udid"
curl -X DELETE -u jamfadmin:password https://jsstest.org.com:8443/JSSResource/computers/udid/"$udid"
exit 0

(I ran the script through shellcheck.net, and made some changes, like enclosing the command in parentheses instead of backticks.) I tested this and it worked for me. Give it a try.

View solution in original post

7 REPLIES 7

bvrooman
Valued Contributor

Perhaps you want to get the ComputerName attribute instead? In my environment the hostname is frequently computername.domain.company.com, and is sometimes (randomly) blank. The computer name attribute actually can't be blank, and I believe it's what the JSS looks at when deciding the name of a particular managed Mac.

mcooper
New Contributor III

The URL in your script is finding the computer by the JSS ID, rather than the computer name. If you want to find a computer by name instead of JSS ID try

https://jsstest.org.com:8443/JSSResource/computers/name/$name

mm2270
Legendary Contributor III

Like @mcooper suggested, the problem is you can't use the /JSSResource/computers/id/ with a $name variable. That expects to see a numerical JSS ID, not a name. That's why its failing.

My suggestion would be to grab something like the Mac's UUID/UDID or the Serial Number and use that instead. The problem with computer names is sometimes they contain odd characters which need to get encoded to work correctly in a URL. For example an odd or unnamed Mac with a computer name like "Mike's MacBook Pro" has both spaces and the apostrophe that need to get encoded in an API URL to work. Serial Numbers and UUID strings don't have that problem. So, consider using a path like /JSSResource/computers/serialnumber/ or /JSSResource/computers/udid/ instead.

easyedc
Valued Contributor II

Thanks for the clarify @mcooper . My original issue seems to persist.

Manually running the command works:

curl -X DELETE -u jamfadmin:password https://jsstest.org.com:8443/JSSResource/computers/name/ManualEnterComputerName

My issue seems to be related to grabbing/passing through the computer name through my script.

easyedc
Valued Contributor II

So the serial number definitely sounds like the better way to go, however I'm still seeing a failure when running this.

#!/bin/bash
SN=`system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}'`
echo $SN
curl -X DELETE -u jamfadmin:password https://jsstest.org.com:8443/JSSResource/computers/serialnumber/$SN
exit 0

The script still fails to pass the serial number through to the curl command.

sh --verbose API Delete.sh 
#!/bin/bash
SN=`system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}'`
system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}'
echo $SN
CORRECTSERIAL
curl -X DELETE -u jamfadmin:password https://jsstest.org.com:8443/JSSResource/computers/serialnumber/$SN
<html>
<head>
   <title>Status page</title>
</head>
<body style="font-family: sans-serif;">
<p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Not Found</p>
<p>The server has not found anything matching the request URI</p>
<p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5">here</a>.<br>
Please continue your visit at our <a href="/">home page</a>.
</p>
</body>
</html>

exit 0

So I made a dummy script to just output the serial name, which works:

#!/bin/bash
SN=`system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}'`
echo $SN
exit 0

spits out correctly.

sh --verbose SNTest.sh 
#!/bin/bash

SN=`system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}'`
system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}'
echo $SN
CORRECTSERIAL
exit 0

pcrandom
Contributor

I tried your original script, but per other's earlier suggestions changed it to use ComputerName for the variable and for the API to delete by name:

#!/bin/bash

name=`scutil --get ComputerName`
curl -X DELETE -u jamfadmin:password https://jsstest.org.com:8443/JSSResource/computers/name/$name

exit 0

This worked for me. I then tried the serial number script that you had, and I got the same error as you, but at least in my case, I know why it failed. Are you by chance running the SN script on a test VM? When I ran it, I got an error because I was testing it against a VM, and the serial number that the VM gave me had a slash in it, which messes up the command.

A colleague of mine has been using the JSS API more than me, and he's using UDID as also suggested above. So you should be able to do the following:

#!/bin/bash
udid=$(system_profiler SPHardwareDataType | awk '/UUID/ { print $3; }')
echo "$udid"
curl -X DELETE -u jamfadmin:password https://jsstest.org.com:8443/JSSResource/computers/udid/"$udid"
exit 0

(I ran the script through shellcheck.net, and made some changes, like enclosing the command in parentheses instead of backticks.) I tested this and it worked for me. Give it a try.

easyedc
Valued Contributor II

First time I'd heard of shellcheck.net and it seems to have found my issue. It looks like I needed double quotes around my variable to get it to pass cleanly. Once I did that, it worked without issue. That's a pretty awesome site.

#!/bin/bash
 SN=$(system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}')
echo "$SN"
curl -kvX DELETE -u jamfadmin:password https://jsstest.org.com:8443/JSSResource/computers/serialnumber/"$SN"
exit 0