mirror of
https://github.com/MLBZ521/MacAdmin.git
synced 2026-02-03 14:03:26 +00:00
v1.6 = Enabling the function to update the Device EA Site Lists
+ Updated the Update EA List function to better handle adds and removes of the Sites in Jamf + Added error checking for the upload as well
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
Script Name: jamf_assignSiteEA.ps1
|
||||
By: Zack Thompson / Created: 2/21/2018
|
||||
Version: 1.5 / Updated: 7/2/2018 / By: ZT
|
||||
Version: 1.6 / Updated: 7/3/2018 / By: ZT
|
||||
|
||||
Description: This script will basically update an EA to the value of the computers Site membership.
|
||||
|
||||
@@ -42,7 +42,7 @@ $id_EAComputer="43"
|
||||
$id_EAMobileDevice="1"
|
||||
|
||||
# Setup Credentials
|
||||
$jamfAPIUser = ""
|
||||
$jamfAPIUser = "APIUsername"
|
||||
# Define Password from within the script.
|
||||
# $jamfAPIPassword = ConvertTo-SecureString -String 'SecurePassPhrase' -AsPlainText -Force
|
||||
# Create an encrypted password file.
|
||||
@@ -60,63 +60,116 @@ $getComputer="${jamfPS}/JSSResource/computers/id"
|
||||
$getMobileDevices="${jamfPS}/JSSResource/mobiledevices"
|
||||
$getMobileDevice="${jamfPS}/JSSResource/mobiledevices/id"
|
||||
$getComputerEA="${jamfPS}/JSSResource/computerextensionattributes/id/${id_EAComputer}"
|
||||
$getMobileEA="${jamfPS}/JSSResource/mobiledeviceextensionattributes/id/${id_MobileComputer}"
|
||||
$getMobileEA="${jamfPS}/JSSResource/mobiledeviceextensionattributes/id/${id_EAMobileDevice}"
|
||||
|
||||
# ============================================================
|
||||
# Functions
|
||||
# ============================================================
|
||||
|
||||
function updateSiteList {
|
||||
function updateEAList($deviceType, $urlEA) {
|
||||
|
||||
Add-LogContent -Content "Pulling required data..."
|
||||
# Get a list of all Sites.
|
||||
$objectOf_Sites = Invoke-RestMethod -Uri $getSites -Method Get -Credential $APIcredentials
|
||||
# Get the ComputerEA for Site.
|
||||
$objectOf_EAComputer = Invoke-RestMethod -Uri $getComputerEA -Method Get -Credential $APIcredentials
|
||||
# Get the List of Site choices from the EA.
|
||||
Add-LogContent -Content "Pulling the Site choices in the ${deviceType} EA..."
|
||||
$objectOf_EAdeviceType = Invoke-RestMethod -Uri $urlEA -Method Get -Credential $APIcredentials
|
||||
|
||||
# Compare the Sites count to the list of Choices from the ComputerEA.
|
||||
if ( $objectOf_Sites.sites.site.Count -eq $($objectOf_EAComputer.computer_extension_attribute.input_type.popup_choices.choice.Count - 1) ) {
|
||||
Add-LogContent -Content "Site count equal Computer EA Choice Count"
|
||||
Add-LogContent -Content "Presuming these are up to date"
|
||||
}
|
||||
else {
|
||||
Add-LogContent -Content "Site count does not equal Computer EA Choice Count"
|
||||
# Build lists for comparisions.
|
||||
$SiteList = $objectOf_Sites.sites.site.Name
|
||||
$EASiteList = $($objectOf_EAdeviceType."${deviceType}_extension_attribute".input_type.popup_choices.choice)
|
||||
|
||||
$SiteList = $objectOf_Sites.sites.site | ForEach-Object { $_.Name }
|
||||
$EASiteList = $objectOf_EAComputer.computer_extension_attribute.input_type.popup_choices.choice
|
||||
# Compare the two lists to find the objects that are missing from the EA List.
|
||||
Add-LogContent -Content "Finding the missing objects..."
|
||||
$missingChoices = $(Compare-Object -ReferenceObject $SiteList -DifferenceObject $EASiteList) | ForEach-Object { $_.InputObject }
|
||||
# Check to see if the EA has been set before.
|
||||
If ($($objectOf_EAdeviceType."${deviceType}_extension_attribute".input_type.popup_choices.choice.Count) -eq "0") {
|
||||
|
||||
Add-LogContent -Content "Adding missing objects to into an XML list..."
|
||||
# For each missing value, add it to the original retrived XML list.
|
||||
ForEach ( $choice in $missingChoices ) {
|
||||
$newChoice = $objectOf_EAComputer.CreateElement("choice")
|
||||
Add-LogContent -Content "EA list is empty...adding choices to the EA..."
|
||||
ForEach ($choice in $objectOf_Sites.sites.site.Name) {
|
||||
Add-LogContent -Content " + ${choice}"
|
||||
$newChoice = $objectOf_EAdeviceType.CreateElement("choice")
|
||||
$newChoice.InnerXml = $choice
|
||||
$objectOf_EAComputer.SelectSingleNode("//popup_choices").AppendChild($newChoice)
|
||||
$objectOf_EAdeviceType.SelectSingleNode("//popup_choices").AppendChild($newChoice) | Out-Null
|
||||
}
|
||||
|
||||
# Upload the XML back.
|
||||
Add-LogContent -Content "Updating the EA Computer List..."
|
||||
Invoke-RestMethod -Uri $getComputerEA -Method Put -Credential $APIcredentials -Body $objectOf_EAComputer
|
||||
Add-LogContent -Content "Updating the ${deviceType} EA List..."
|
||||
Try {
|
||||
$Response = Invoke-RestMethod -Uri $urlEA -Method Put -Credential $APIcredentials -Body $objectOf_EAdeviceType -ErrorVariable RestError -ErrorAction SilentlyContinue
|
||||
}
|
||||
Catch {
|
||||
$statusCode = $_.Exception.Response.StatusCode.value__
|
||||
$statusDescription = $_.Exception.Response.StatusDescription
|
||||
|
||||
If ($statusCode -notcontains "200") {
|
||||
Add-LogContent -Content " -> Failed to update the EA List for ${deviceType}s"
|
||||
Add-LogContent -Content " --> Response: ${statusCode}/${statusDescription}: $($RestError.Message | ForEach { $_.Split(":")[1];} | ForEach { $_.Split([Environment]::NewLine)[0];})"
|
||||
}
|
||||
}
|
||||
}
|
||||
Else {
|
||||
|
||||
# Compare the two lists to find the objects that need to be added or removed from the EA List.
|
||||
Add-LogContent -Content "Comparing choices in the ${deviceType} EA to the Site list..."
|
||||
$addChoices = $(Compare-Object -ReferenceObject $SiteList -DifferenceObject $EASiteList) | Where-Object { $_.SideIndicator -eq '<=' } | ForEach-Object { $_.InputObject }
|
||||
$removeChoices = $(Compare-Object -ReferenceObject $SiteList -DifferenceObject $EASiteList) | Where-Object { $_.SideIndicator -eq '=>' } | ForEach-Object { $_.InputObject }
|
||||
|
||||
# Check if there are any differences that needs to be made.
|
||||
If ($addChoices.Count -ne 0 -or $removeChoices.Count -ne 0) {
|
||||
|
||||
If ($addChoices.Count -ne 0) {
|
||||
# For each missing value, add it to the original retrived XML list.
|
||||
Add-LogContent -Content "Adding choices to the EA list..."
|
||||
ForEach ( $choice in $addChoices ) {
|
||||
Add-LogContent -Content " + ${choice}"
|
||||
$newChoice = $objectOf_EAdeviceType.CreateElement("choice")
|
||||
$newChoice.InnerXml = $choice
|
||||
$objectOf_EAdeviceType.SelectSingleNode("//popup_choices").AppendChild($newChoice) | Out-Null
|
||||
}
|
||||
}
|
||||
|
||||
If ($removeChoices.Count -ne 0) {
|
||||
# For each invalid value, remove it from the original retrived XML list.
|
||||
Add-LogContent -Content "Removing choices from EA list..."
|
||||
ForEach ( $choice in $removeChoices ) {
|
||||
Add-LogContent -Content " - ${choice}"
|
||||
$XMLNode = $objectOf_EAdeviceType.SelectSingleNode("//choice[.='${choice}']")
|
||||
$XMLNode.ParentNode.RemoveChild($XMLNode) | Out-Null
|
||||
}
|
||||
}
|
||||
|
||||
# Upload the XML back to the JPS.
|
||||
Add-LogContent -Content "Updating the ${deviceType} EA List in the JPS..."
|
||||
Try {
|
||||
$Response = Invoke-RestMethod -Uri $urlEA -Method Put -Credential $APIcredentials -Body $objectOf_EAdeviceType -ErrorVariable RestError -ErrorAction SilentlyContinue
|
||||
}
|
||||
Catch {
|
||||
$statusCode = $_.Exception.Response.StatusCode.value__
|
||||
$statusDescription = $_.Exception.Response.StatusDescription
|
||||
|
||||
If ($statusCode -notcontains "200") {
|
||||
Add-LogContent -Content " -> Failed to update the EA List for ${deviceType}s"
|
||||
Add-LogContent -Content " --> Response: ${statusCode}/${statusDescription}: $($RestError.Message | ForEach { $_.Split(":")[1];} | ForEach { $_.Split([Environment]::NewLine)[0];})"
|
||||
}
|
||||
}
|
||||
}
|
||||
Else {
|
||||
Add-LogContent -Content "The ${deviceType} EA choices are up to date."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function updateRecord($deviceType, $urlALL, $urlID, $idEA) {
|
||||
function updateDeviceRecord($deviceType, $urlALL, $urlID, $idEA) {
|
||||
|
||||
# Get a list of all records.
|
||||
Add-LogContent -Content "Pulling all ${deviceType} records..."
|
||||
# Get a list of all records
|
||||
$objectOf_Devices = Invoke-RestMethod -Uri $urlALL -Method Get -Credential $APIcredentials
|
||||
|
||||
# Get the ID of each device.
|
||||
Add-LogContent -Content "Pulling data for each individual ${deviceType} record..."
|
||||
# Get the ID of each device
|
||||
$deviceList = $objectOf_Devices."${deviceType}s"."${deviceType}" | ForEach-Object {$_.ID}
|
||||
|
||||
ForEach ( $ID in $deviceList ) {
|
||||
# Get Computer's General Section
|
||||
# Get Computer's General Section.
|
||||
$objectOf_deviceGeneral = Invoke-RestMethod -Uri "${urlID}/${ID}/subset/General" -Method Get -Credential $APIcredentials
|
||||
|
||||
# Get Computer's Extention Attribute Section
|
||||
# Get Computer's Extention Attribute Section.
|
||||
$objectOf_deviceEA = Invoke-RestMethod -Uri "${urlID}/${ID}/subset/extension_attributes" -Method Get -Credential $APIcredentials
|
||||
|
||||
If ( $objectOf_deviceGeneral.$deviceType.general.site.name -ne $($objectOf_deviceEA.$deviceType.extension_attributes.extension_attribute | Select-Object ID, Value | Where-Object { $_.id -eq $idEA }).value) {
|
||||
@@ -166,9 +219,16 @@ Add-LogContent -Content "API Credentials Valid -- continuing..."
|
||||
$StopWatch = New-Object -TypeName System.Diagnostics.Stopwatch
|
||||
$StopWatch.Start()
|
||||
|
||||
# Call Update function for each device type
|
||||
updateRecord computer $getComputers $getComputer $id_EAComputer
|
||||
updateRecord mobile_device $getMobileDevices $getMobileDevice $id_EAMobileDevice
|
||||
# Get a list of all Sites.
|
||||
$objectOf_Sites = Invoke-RestMethod -Uri $getSites -Method Get -Credential $APIcredentials
|
||||
|
||||
# Call EA Update function for each device type
|
||||
updateEAList computer $getComputerEA
|
||||
updateEAList mobile_device $getMobileEA
|
||||
|
||||
# Call Device Update function for each device type
|
||||
updateDeviceRecord computer $getComputers $getComputer $id_EAComputer
|
||||
updateDeviceRecord mobile_device $getMobileDevices $getMobileDevice $id_EAMobileDevice
|
||||
|
||||
$StopWatch.Stop()
|
||||
Add-LogContent -Content "Mins: " $StopWatch.Elapsed.Minutes "Seconds: " $StopWatch.Elapsed.Seconds
|
||||
|
||||
Reference in New Issue
Block a user