Skip to content

Commit

Permalink
Merge pull request #100 from almenscorner/feature-devicemanagementset…
Browse files Browse the repository at this point in the history
…tings

Feature devicemanagementsettings
  • Loading branch information
almenscorner authored Mar 20, 2023
2 parents 13df052 + bcae181 commit 44b7e2f
Show file tree
Hide file tree
Showing 15 changed files with 1,227 additions and 37 deletions.
75 changes: 75 additions & 0 deletions src/IntuneCD/backup_customAttributeShellScript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python3

"""
This module backs up all Custom Attribute Shell scripts in Intune.
"""

import os
import base64

from .clean_filename import clean_filename
from .graph_request import makeapirequest
from .graph_batch import batch_assignment, get_object_assignment, batch_request
from .save_output import save_output
from .remove_keys import remove_keys

# Set MS Graph endpoint
ENDPOINT = "https://graph.microsoft.com/beta/deviceManagement/deviceCustomAttributeShellScripts/"
ASSIGNMENT_ENDPOINT = (
"https://graph.microsoft.com/beta/deviceManagement/deviceManagementScripts"
)


# Get all Custom Attribute Shell scripts and save them in specified path
def savebackup(path, output, exclude, token):
"""
Saves all Custom Attribute Shell scripts in Intune to a JSON or YAML file and script files.
:param path: Path to save the backup to
:param output: Format the backup will be saved as
:param exclude: If "assignments" is in the list, it will not back up the assignments
:param token: Token to use for authenticating the request
"""

config_count = 0
configpath = path + "/" + "Custom Attributes/"
data = makeapirequest(ENDPOINT, token)
script_ids = []
for script in data["value"]:
script_ids.append(script["id"])

assignment_responses = batch_assignment(
data,
"deviceManagement/deviceCustomAttributeShellScripts/",
"?$expand=assignments",
token,
)
script_data_responses = batch_request(
script_ids, "deviceManagement/deviceCustomAttributeShellScripts/", "", token
)

for script_data in script_data_responses:
config_count += 1
if "assignments" not in exclude:
assignments = get_object_assignment(script_data["id"], assignment_responses)
if assignments:
script_data["assignments"] = assignments

script_data = remove_keys(script_data)

print("Backing up Custom Attribute: " + script_data["displayName"])

# Get filename without illegal characters
fname = clean_filename(script_data["displayName"])

# Save Shell script as JSON or YAML depending on configured value in "-o"
save_output(output, configpath, fname, script_data)

# Save Shell script data to the script data folder
if not os.path.exists(configpath + "Script Data/"):
os.makedirs(configpath + "Script Data/")
decoded = base64.b64decode(script_data["scriptContent"]).decode("utf-8")
f = open(configpath + "Script Data/" + script_data["fileName"], "w")
f.write(decoded)

return config_count
41 changes: 41 additions & 0 deletions src/IntuneCD/backup_deviceManagementSettings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python3

"""
This module backs up Device Management settings in Intune.
"""

from .clean_filename import clean_filename
from .graph_request import makeapirequest
from .save_output import save_output
from .remove_keys import remove_keys

# Set MS Graph endpoint
ENDPOINT = "https://graph.microsoft.com/beta/deviceManagement/settings"


# Get settings and save in specified path
def savebackup(path, output, token):
"""
Save Device Management Setting to a JSON or YAML file.
:param path: Path to save the backup to
:param output: Format the backup will be saved as
:param token: Token to use for authenticating the request
"""

config_count = 0

configpath = path + "/" + "Device Management Settings/"
data = makeapirequest(ENDPOINT, token)

if data:
config_count += 1
data = remove_keys(data)
print("Backing up Device Management Settings")

# Get filename without illegal characters
fname = clean_filename("settings")
# Save settings as JSON or YAML depending on configured value in "-o"
save_output(output, configpath, fname, data)

return config_count
12 changes: 12 additions & 0 deletions src/IntuneCD/run_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def start():
"ConfigurationPolicies",
"ConditionalAccess",
"EnrollmentConfigurations",
"DeviceManagementSettings",
"CustomAttributes",
],
nargs="+",
)
Expand Down Expand Up @@ -185,6 +187,11 @@ def run_backup(path, output, exclude, token):

config_count += savebackup(path, output, exclude, token)

if "DeviceManagementSettings" not in exclude:
from .backup_deviceManagementSettings import savebackup

config_count += savebackup(path, output, token)

if "NotificationTemplate" not in exclude:
from .backup_notificationTemplate import savebackup

Expand Down Expand Up @@ -270,6 +277,11 @@ def run_backup(path, output, exclude, token):

config_count += savebackup(path, output, exclude, token)

if "CustomAttributes" not in exclude:
from .backup_customAttributeShellScript import savebackup

config_count += savebackup(path, output, exclude, token)

if "ConfigurationPolicies" not in exclude:
from .backup_configurationPolicies import savebackup

Expand Down
Loading

0 comments on commit 44b7e2f

Please sign in to comment.