Skip to content

Instantly share code, notes, and snippets.

@boberito
Last active June 19, 2023 09:31
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save boberito/9bf7294cb206735ab482d60707714393 to your computer and use it in GitHub Desktop.
Convert profiles command output to json and parse for duplicates
#!/usr/bin/env python3
#In order to run this, you must run the script with sudo, as the profiles command requires sudo.
#This is only tested on macOS Ventura, but I believe will work on macOS Monterey. I believe thats when plutil gained -convert json
#output will look like this
import os
import subprocess
import json
cmd = '/usr/bin/profiles -P -o stdout-xml | /usr/bin/grep -v "configuration profiles installed" > /Users/Shared/allprofiles.xml; /usr/bin/plutil -convert json /Users/Shared/allprofiles.xml -r -o -'
profiles_bytes = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE).stdout
profiles_string = profiles_bytes.decode()
profiles_json = json.loads(profiles_string)
list_of_keys = []
list_of_dupes = set()
for profile in profiles_json["_computerlevel"]:
print("Profile Display Name: " + profile["ProfileDisplayName"])
for items in profile["ProfileItems"]:
print("Payload Domain: " + items["PayloadType"])
for key,value in items["PayloadContent"].items():
if key == "PayloadContentManagedPreferences":
for k,v in items["PayloadContent"]['PayloadContentManagedPreferences'].items():
for mcx in items["PayloadContent"]['PayloadContentManagedPreferences'][k]['Forced']:
for mcx_k,mcx_v in mcx['mcx_preference_settings'].items():
print("\t[ManagedPreferences]: MCX: {}, Key: {}, Value: {}".format(k,mcx_k,mcx_v))
if key in list_of_keys:
list_of_dupes.add(mcx_k)
else:
list_of_keys.append(mcx_k)
else:
print("\tKey: {}, Value: {}".format(key,value))
if key in list_of_keys:
list_of_dupes.add(key)
else:
list_of_keys.append(key)
print("--------------------------------")
print()
print("")
if len(list_of_dupes) > 0:
print("----{} Duplicates Found---".format(len(list_of_dupes)))
for dupe in list_of_dupes:
print("\t{}".format(dupe))
@WardsParadox
Copy link

WardsParadox commented Jan 17, 2023

You can skip the conversion and use Plistlib as profiles can also output Plist (using stdout), not just standalone XML. Save you some steps and imports.

But otherwise, nice! Added this to my scripts :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment