Posted on 07-21-2023 12:11 PM
Hi there,
Hoping I can entreat the Hive Mind to help me nail down why I'm getting an error in the last part of this script. I'm putting together a self service policy that will run two scripts. One grabs the current computer name as a variable, deletes all the relevant networking plist files in /Library/Preferences/SystemConfiguration, and then renames the computer to its original name. That one is working great.
The second script is where I am having trouble (I split them into two because for no discernible reason, the first part would throw weird errors when they were combined in one script but now it seems to work fine with them separate despite there being no differences in the command statements).
This script creates a new network location, switches to it, and deletes the old one. But I'm having trouble with the last part. I'll include the script below, and the jamf policy log output from a test machine.
#Created by John Geck, 7/21/2023
#Create a new network location and substitute the “CRPUSD” variable below with the name you choose
originallocation="$(networksetup -getcurrentlocation)"
newlocation="CRPUSD"
networksetup -createlocation "$newlocation" populate
sleep 5
#Switch to your new location
networksetup -switchtolocation "$newlocation"
sleep 5
#Delete the original location. This is hard-coded because I couldn't figure out how to call the existing locations and create them as a variable.
networksetup -deletelocation "$originallocation"
#Wait 15 seconds for network to re-establish itself and run a Jamf Recon
sleep 15
jamf recon
#Notify user they need to reboot
osascript -e 'display alert "Reset Successful" message "Please save any open work and restart your computer."'
#END
Here's the Jamf Error:
Details
[STEP 1 of 5]
Executing Policy Reset Network Settings (Test)
[STEP 2 of 5]
Running script Remove Network Plists (Testing)...
Script exit code: 0
Script result:
[STEP 3 of 5]
Running script Reset Network Location...
Script exit code: 0
Script result: populated
found it! Could not get current network location
** Error: Unable to access the System Configuration database. is not a network location name.
** Error: The parameters were not valid.
Retrieving inventory preferences from https://jss.crpusd.org:8443/...
Finding extension attributes...
Locating accounts...
Locating hard drive information...
Locating package receipts...
Locating printers...
Locating plugins...
Searching path: /Library/Internet Plug-Ins
Locating applications...
Searching path: /System/Applications
Gathering application usage information from the JamfDaemon...
Searching path: /Applications
Locating hardware information (macOS 13.4.1)...
Submitting data to https://jss.crpusd.org:8443/...
<computer_id>2166</computer_id>
button returned:OK
[STEP 4 of 5]
[STEP 5 of 5]
07-21-2023 01:16 PM - edited 07-21-2023 01:18 PM
@johntgeck I think Jamf will try and run it as a shell script anyways, but you're missing a #!/bin/sh at the beginning of the script you posted.
What does the script return if you try and run in from Terminal? (Use the sudo prefix to run as admin)
Posted on 07-24-2023 02:15 PM
Yeah I wasn't sure if I needed the shebang, I tried with and without and it didn't seem to make a difference. I might as well add it back in. @jimmy-swings also suggested I echo out each step to see where things are going off the rails, so I'll do that as well.
It's good to look at this with fresh eyes today, the original impetus to script this was a wireless issue that ended up getting diagnosed as an infrastructure issue, but I like the idea of having this script as a one-button "nuke everything and start with fresh settings" troubleshooter for my field techs.
Posted on 07-22-2023 11:32 PM
@johntgeck I was unable to reproduce this issues however the error it suggests the variable "originallocation" is not being populated.
To help troubleshoot issues remotely, it's best practice to echo out each step and any resulting response. If you're also dependent upon a result, you can ensure it's not empty using a test.
# Get Current Location
originallocation="$(networksetup -getcurrentlocation)"
if [ -z $originallocation ]; then
echo "Current Location not found"
else
echo "Current Location is $originallocation"
fi
Posted on 07-24-2023 02:12 PM
Thanks for the tip, I will definitely make those changes and see what I get.