Convert profiles command output to json and parse for duplicates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :)