From e5afea0e6108776602f7e217456bf4932baa6b99 Mon Sep 17 00:00:00 2001 From: Zack T Date: Fri, 23 Feb 2018 01:31:15 -0700 Subject: [PATCH 01/10] v0.1 = Created function for updating the EA's list of Sites + Created function for updating the EA's list of Sites --- Jamf Pro/Admin Tools/jamf_ea_Site.ps1 | 56 +++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Jamf Pro/Admin Tools/jamf_ea_Site.ps1 diff --git a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 new file mode 100644 index 0000000..f721a12 --- /dev/null +++ b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 @@ -0,0 +1,56 @@ +<# + +Script Name: jamf_ea_Site.ps1 +By: Zack Thompson / Created: 2/21/2018 +Version: 0.1 / Updated: 2/21/2018 / By: ZT + +Description: This script will basically update an EA to the value of the computers Site membership. + +#> + +$jamfAPIUser = "" +$jamfAPIPassword = ConvertTo-SecureString -String "" -AsPlainText -Force +$APIcredentials = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $jamfAPIUser, $jamfAPIPassword + +$jamfPS="https://newjss.company.com:8443" +$getSites="${jamfPS}/JSSResource/sites" +$getComputerEA="${jamfPS}/JSSResource/computerextensionattributes/id/43" +#$mobileEA="${jamfPS}/JSSResource/mobiledeviceextensionattributes/name/Jamf%20Site" +$getComputers="${jamfPS}/JSSResource/computers" +$computersGeneral="${jamPS}/JSSResource/computers/id/subset/General" +$computersEA="${jamPS}/JSSResource/computers/id/subset/extension_attribute" + +#[xml]$template_ComputerEA = "Pop-up Menudefault" + +[xml]$template_ComputerEA = $objectComputerEA.InnerXml + +function updateSiteList { + +$objectOfSites = Invoke-RestMethod -Uri $getSites -Method Get -Credential $APIcredentials +$objectComputerEA = Invoke-RestMethod -Uri $getComputerEA -Method Get -Credential $APIcredentials + +if ( $objectOfSites.sites.site.Count -eq $($objectComputerEA.computer_extension_attribute.input_type.popup_choices.choice.Count - 1) ) { + Write-Host "Site count equal Computer EA Choice Count" + Write-Host "Presuming these are up to date" +} +else { + Write-Host "Site count does not equal Computer EA Choice Count" + + $SiteList = $objectOfSites.sites.site | ForEach-Object {$_.Name} + $missingChoices = $(Compare-Object -ReferenceObject $($objectOfSites.sites.site | ForEach-Object {$_.Name}) -DifferenceObject $objectComputerEA.computer_extension_attribute.input_type.popup_choices.choice) | ForEach-Object {$_.InputObject} + $missingChoices + + ForEach ( $choice in $missingChoices ) { + Write-Host $choice + $newElement = $template_ComputerEA.CreateElement("choice") + $newElement.InnerXml = $choice + $template_ComputerEA.SelectSingleNode("//popup_choices").AppendChild($newElement) + Invoke-RestMethod -Uri $getComputerEA -Method Put -Credential $APIcredentials -Body $template_ComputerEA + } +} + +} + +$getComputers = Invoke-RestMethod -Method Get -Uri $getComputers -Credential $APIcredentials +$getComputers.computers.computer + From 9a615e26dc3e51bbff254ef464c094c69d7e7273 Mon Sep 17 00:00:00 2001 From: Zack T Date: Fri, 23 Feb 2018 10:22:04 -0700 Subject: [PATCH 02/10] General cleanup of current code General cleanup of current code --- Jamf Pro/Admin Tools/jamf_ea_Site.ps1 | 34 +++++++++++++++++---------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 index f721a12..243de7c 100644 --- a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 +++ b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 @@ -8,45 +8,55 @@ Description: This script will basically update an EA to the value of the comput #> +# ============================================================ +# Define Variables +# ============================================================ + +# Setup Credentials $jamfAPIUser = "" $jamfAPIPassword = ConvertTo-SecureString -String "" -AsPlainText -Force $APIcredentials = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $jamfAPIUser, $jamfAPIPassword +# Setup API URLs $jamfPS="https://newjss.company.com:8443" $getSites="${jamfPS}/JSSResource/sites" $getComputerEA="${jamfPS}/JSSResource/computerextensionattributes/id/43" -#$mobileEA="${jamfPS}/JSSResource/mobiledeviceextensionattributes/name/Jamf%20Site" +$getMobileEA="${jamfPS}/JSSResource/mobiledeviceextensionattributes/id/" $getComputers="${jamfPS}/JSSResource/computers" $computersGeneral="${jamPS}/JSSResource/computers/id/subset/General" $computersEA="${jamPS}/JSSResource/computers/id/subset/extension_attribute" -#[xml]$template_ComputerEA = "Pop-up Menudefault" - -[xml]$template_ComputerEA = $objectComputerEA.InnerXml +# ============================================================ +# Functions +# ============================================================ function updateSiteList { -$objectOfSites = Invoke-RestMethod -Uri $getSites -Method Get -Credential $APIcredentials -$objectComputerEA = Invoke-RestMethod -Uri $getComputerEA -Method Get -Credential $APIcredentials +$objectOf_Sites = Invoke-RestMethod -Uri $getSites -Method Get -Credential $APIcredentials +$objectOf_ComputerEA = Invoke-RestMethod -Uri $getComputerEA -Method Get -Credential $APIcredentials +[xml]$xml_ComputerEA = $objectOfComputerEA.InnerXml -if ( $objectOfSites.sites.site.Count -eq $($objectComputerEA.computer_extension_attribute.input_type.popup_choices.choice.Count - 1) ) { + +if ( $objectOf_Sites.sites.site.Count -eq $($objectOf_ComputerEA.computer_extension_attribute.input_type.popup_choices.choice.Count - 1) ) { Write-Host "Site count equal Computer EA Choice Count" Write-Host "Presuming these are up to date" } else { Write-Host "Site count does not equal Computer EA Choice Count" - $SiteList = $objectOfSites.sites.site | ForEach-Object {$_.Name} - $missingChoices = $(Compare-Object -ReferenceObject $($objectOfSites.sites.site | ForEach-Object {$_.Name}) -DifferenceObject $objectComputerEA.computer_extension_attribute.input_type.popup_choices.choice) | ForEach-Object {$_.InputObject} + $SiteList = $objectOf_Sites.sites.site | ForEach-Object {$_.Name} + $missingChoices = $(Compare-Object -ReferenceObject $($objectOf_Sites.sites.site | ForEach-Object {$_.Name}) -DifferenceObject $objectOf_ComputerEA.computer_extension_attribute.input_type.popup_choices.choice) | ForEach-Object {$_.InputObject} $missingChoices ForEach ( $choice in $missingChoices ) { Write-Host $choice - $newElement = $template_ComputerEA.CreateElement("choice") + $newElement = $xml_ComputerEA.CreateElement("choice") $newElement.InnerXml = $choice - $template_ComputerEA.SelectSingleNode("//popup_choices").AppendChild($newElement) - Invoke-RestMethod -Uri $getComputerEA -Method Put -Credential $APIcredentials -Body $template_ComputerEA + $xml_ComputerEA.SelectSingleNode("//popup_choices").AppendChild($newElement) + } + + #Invoke-RestMethod -Uri $getComputerEA -Method Put -Credential $APIcredentials -Body $template_ComputerEA } } From 52df98095e1ba0eed3d9f1298f209ee65509fb92 Mon Sep 17 00:00:00 2001 From: Zack T Date: Fri, 23 Feb 2018 11:05:48 -0700 Subject: [PATCH 03/10] Further Code Improvement Further Code Improvement --- Jamf Pro/Admin Tools/jamf_ea_Site.ps1 | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 index 243de7c..7811c9f 100644 --- a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 +++ b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 @@ -32,11 +32,17 @@ $computersEA="${jamPS}/JSSResource/computers/id/subset/extension_attribute" function updateSiteList { -$objectOf_Sites = Invoke-RestMethod -Uri $getSites -Method Get -Credential $APIcredentials -$objectOf_ComputerEA = Invoke-RestMethod -Uri $getComputerEA -Method Get -Credential $APIcredentials -[xml]$xml_ComputerEA = $objectOfComputerEA.InnerXml +# Get a list of all Sites + $objectOf_Sites = Invoke-RestMethod -Uri $getSites -Method Get -Credential $APIcredentials +# Get the ComputerEA for Site + $objectOf_ComputerEA = Invoke-RestMethod -Uri $getComputerEA -Method Get -Credential $APIcredentials +# Dump the current configuration into a XML variable + [xml]$xml_ComputerEA = $objectOf_ComputerEA.InnerXml + $objectOf_Sites.sites.site.Count + $($objectOf_ComputerEA.computer_extension_attribute.input_type.popup_choices.choice.Count - 1) +# Computer the Sites count to the list of Choices from the ComputerEA if ( $objectOf_Sites.sites.site.Count -eq $($objectOf_ComputerEA.computer_extension_attribute.input_type.popup_choices.choice.Count - 1) ) { Write-Host "Site count equal Computer EA Choice Count" Write-Host "Presuming these are up to date" @@ -46,17 +52,18 @@ else { $SiteList = $objectOf_Sites.sites.site | ForEach-Object {$_.Name} $missingChoices = $(Compare-Object -ReferenceObject $($objectOf_Sites.sites.site | ForEach-Object {$_.Name}) -DifferenceObject $objectOf_ComputerEA.computer_extension_attribute.input_type.popup_choices.choice) | ForEach-Object {$_.InputObject} - $missingChoices + $missingChoices.count # Just here for dev purposes, so I know how many are missing. ForEach ( $choice in $missingChoices ) { - Write-Host $choice - $newElement = $xml_ComputerEA.CreateElement("choice") - $newElement.InnerXml = $choice - $xml_ComputerEA.SelectSingleNode("//popup_choices").AppendChild($newElement) - + # Write-Host $choice + $newChoice = $xml_ComputerEA.CreateElement("choice") + $newChoice.InnerXml = $choice + $xml_ComputerEA.SelectSingleNode("//popup_choices").AppendChild($newChoice) } - #Invoke-RestMethod -Uri $getComputerEA -Method Put -Credential $APIcredentials -Body $template_ComputerEA + $xml_ComputerEA.computer_extension_attribute.input_type.popup_choices.choice.Count + + #Invoke-RestMethod -Uri $getComputerEA -Method Put -Credential $APIcredentials -Body $new_xml_ComputerEA } } From 5b279c179e6dc8f6aedc6f566b26dfd58466dcc6 Mon Sep 17 00:00:00 2001 From: Zack T Date: Fri, 23 Feb 2018 16:04:34 -0700 Subject: [PATCH 04/10] v0.2 = Final version of the updateSiteList() Function + Finalized the updateSiteList() Function --- Jamf Pro/Admin Tools/jamf_ea_Site.ps1 | 67 +++++++++++++++------------ 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 index 7811c9f..cd01fe4 100644 --- a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 +++ b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 @@ -2,7 +2,7 @@ Script Name: jamf_ea_Site.ps1 By: Zack Thompson / Created: 2/21/2018 -Version: 0.1 / Updated: 2/21/2018 / By: ZT +Version: 0.2 / Updated: 2/23/2018 / By: ZT Description: This script will basically update an EA to the value of the computers Site membership. @@ -12,6 +12,10 @@ Description: This script will basically update an EA to the value of the comput # Define Variables # ============================================================ +# Jamf EA IDs +$id_EAComputer="43" +$id_MobileComputer="43" + # Setup Credentials $jamfAPIUser = "" $jamfAPIPassword = ConvertTo-SecureString -String "" -AsPlainText -Force @@ -20,7 +24,7 @@ $APIcredentials = New-Object –TypeName System.Management.Automation.PSCredenti # Setup API URLs $jamfPS="https://newjss.company.com:8443" $getSites="${jamfPS}/JSSResource/sites" -$getComputerEA="${jamfPS}/JSSResource/computerextensionattributes/id/43" +$getComputerEA="${jamfPS}/JSSResource/computerextensionattributes/id/${id_EAComputer}" $getMobileEA="${jamfPS}/JSSResource/mobiledeviceextensionattributes/id/" $getComputers="${jamfPS}/JSSResource/computers" $computersGeneral="${jamPS}/JSSResource/computers/id/subset/General" @@ -32,42 +36,45 @@ $computersEA="${jamPS}/JSSResource/computers/id/subset/extension_attribute" function updateSiteList { -# Get a list of all Sites - $objectOf_Sites = Invoke-RestMethod -Uri $getSites -Method Get -Credential $APIcredentials -# Get the ComputerEA for Site - $objectOf_ComputerEA = Invoke-RestMethod -Uri $getComputerEA -Method Get -Credential $APIcredentials -# Dump the current configuration into a XML variable - [xml]$xml_ComputerEA = $objectOf_ComputerEA.InnerXml + Write-Host "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 - $objectOf_Sites.sites.site.Count - $($objectOf_ComputerEA.computer_extension_attribute.input_type.popup_choices.choice.Count - 1) - -# Computer the Sites count to the list of Choices from the ComputerEA -if ( $objectOf_Sites.sites.site.Count -eq $($objectOf_ComputerEA.computer_extension_attribute.input_type.popup_choices.choice.Count - 1) ) { - Write-Host "Site count equal Computer EA Choice Count" - Write-Host "Presuming these are up to date" -} -else { - Write-Host "Site count does not equal Computer EA Choice Count" - - $SiteList = $objectOf_Sites.sites.site | ForEach-Object {$_.Name} - $missingChoices = $(Compare-Object -ReferenceObject $($objectOf_Sites.sites.site | ForEach-Object {$_.Name}) -DifferenceObject $objectOf_ComputerEA.computer_extension_attribute.input_type.popup_choices.choice) | ForEach-Object {$_.InputObject} - $missingChoices.count # Just here for dev purposes, so I know how many are missing. - - ForEach ( $choice in $missingChoices ) { - # Write-Host $choice - $newChoice = $xml_ComputerEA.CreateElement("choice") - $newChoice.InnerXml = $choice - $xml_ComputerEA.SelectSingleNode("//popup_choices").AppendChild($newChoice) + # 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) ) { + Write-Host "Site count equal Computer EA Choice Count" + Write-Host "Presuming these are up to date" } + else { + Write-Host "Site count does not equal Computer EA Choice Count" - $xml_ComputerEA.computer_extension_attribute.input_type.popup_choices.choice.Count + $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. + Write-Host "Finding the missing objects..." + $missingChoices = $(Compare-Object -ReferenceObject $SiteList -DifferenceObject $EASiteList) | ForEach-Object {$_.InputObject} - #Invoke-RestMethod -Uri $getComputerEA -Method Put -Credential $APIcredentials -Body $new_xml_ComputerEA + Write-Host "Adding missing objects to into an XML list..." + # For each missing value, add it to the original retrived XML list. + ForEach ( $choice in $missingChoices ) { + # Write-Host $choice + $newChoice = $objectOf_EAComputer.CreateElement("choice") + $newChoice.InnerXml = $choice + $objectOf_EAComputer.SelectSingleNode("//popup_choices").AppendChild($newChoice) + } + + # Upload the XML back. + Write-Host "Updating the EA Computer List..." + Invoke-RestMethod -Uri $getComputerEA -Method Put -Credential $APIcredentials -Body $objectOf_EAComputer + } } + } + $getComputers = Invoke-RestMethod -Method Get -Uri $getComputers -Credential $APIcredentials $getComputers.computers.computer From 34ee66b1e4504aefbe737563dc276ef6ab51494a Mon Sep 17 00:00:00 2001 From: Zack T Date: Fri, 23 Feb 2018 16:15:56 -0700 Subject: [PATCH 05/10] v0.3 = Created function to update EA for Computer + Created function to update EA for Computer --- Jamf Pro/Admin Tools/jamf_ea_Site.ps1 | 32 +++++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 index cd01fe4..0598502 100644 --- a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 +++ b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 @@ -2,7 +2,7 @@ Script Name: jamf_ea_Site.ps1 By: Zack Thompson / Created: 2/21/2018 -Version: 0.2 / Updated: 2/23/2018 / By: ZT +Version: 0.3 / Updated: 2/23/2018 / By: ZT Description: This script will basically update an EA to the value of the computers Site membership. @@ -27,8 +27,7 @@ $getSites="${jamfPS}/JSSResource/sites" $getComputerEA="${jamfPS}/JSSResource/computerextensionattributes/id/${id_EAComputer}" $getMobileEA="${jamfPS}/JSSResource/mobiledeviceextensionattributes/id/" $getComputers="${jamfPS}/JSSResource/computers" -$computersGeneral="${jamPS}/JSSResource/computers/id/subset/General" -$computersEA="${jamPS}/JSSResource/computers/id/subset/extension_attribute" +$getComputer="${jamfPS}/JSSResource/computers/id" # ============================================================ # Functions @@ -71,10 +70,29 @@ function updateSiteList { } } +function updateComputers { + # Get a list of all Computers + $objectOf_Computers = Invoke-RestMethod -Uri $getComputers -Method Get -Credential $APIcredentials + # Get the ID of each computer + $computerList = $objectOf_Computers.computers.computer | ForEach-Object {$_.ID} + + ForEach ( $ID in $computerList ) { + + # Get Computer's General Section + $objectOf_computerGeneral = Invoke-RestMethod -Uri "${getComputer}/${ID}/subset/General" -Method Get -Credential $APIcredentials + + # Get Computer's Extention Attribute Section + $objectOf_computerEA = Invoke-RestMethod -Uri "${getComputer}/${ID}/subset/extension_attributes" -Method Get -Credential $APIcredentials + + If ( $objectOf_computerGeneral.computer.general.site.name -eq $($objectOf_computerEA.computer.extension_attributes.extension_attribute | Select-Object ID, Value | Where-Object { $_.id -eq $id_EAComputer } | Select-Object Value)) { + Write-Host "Site is set!" + } + else{ + Write-host "Site is incorrect -- updating..." + [xml]$upload_ComputerEA = "${id_EAComputer}$(${objectOf_computerGeneral}.computer.general.site.name)" + Invoke-RestMethod -Uri "${getComputer}/${ID}" -Method Put -Credential $APIcredentials -Body $test_ComputerEA + } + } } - -$getComputers = Invoke-RestMethod -Method Get -Uri $getComputers -Credential $APIcredentials -$getComputers.computers.computer - From 5416c949d9d8b0be38651a585bfcbb90de977bad Mon Sep 17 00:00:00 2001 From: Zack T Date: Fri, 23 Feb 2018 16:37:46 -0700 Subject: [PATCH 06/10] v0.4 = Changing workflow of the EA - While working through this and testing, found a different way of doing this -- Leaving updateSiteList() Function for others, but I'm not using in my workflow + Finalized the update Site EA for Computers, removed from function --- Jamf Pro/Admin Tools/jamf_ea_Site.ps1 | 43 ++++++++++++++------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 index 0598502..92ac942 100644 --- a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 +++ b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 @@ -2,7 +2,7 @@ Script Name: jamf_ea_Site.ps1 By: Zack Thompson / Created: 2/21/2018 -Version: 0.3 / Updated: 2/23/2018 / By: ZT +Version: 0.4 / Updated: 2/23/2018 / By: ZT Description: This script will basically update an EA to the value of the computers Site membership. @@ -70,29 +70,30 @@ function updateSiteList { } } -function updateComputers { - # Get a list of all Computers - $objectOf_Computers = Invoke-RestMethod -Uri $getComputers -Method Get -Credential $APIcredentials - # Get the ID of each computer - $computerList = $objectOf_Computers.computers.computer | ForEach-Object {$_.ID} +# ============================================================ +# Bits Staged... +# ============================================================ - ForEach ( $ID in $computerList ) { +Write-Host "Pulling all computer records..." +# Get a list of all Computers +$objectOf_Computers = Invoke-RestMethod -Uri $getComputers -Method Get -Credential $APIcredentials - # Get Computer's General Section - $objectOf_computerGeneral = Invoke-RestMethod -Uri "${getComputer}/${ID}/subset/General" -Method Get -Credential $APIcredentials - - # Get Computer's Extention Attribute Section - $objectOf_computerEA = Invoke-RestMethod -Uri "${getComputer}/${ID}/subset/extension_attributes" -Method Get -Credential $APIcredentials +Write-Host "Pulling data for each computer record..." +# Get the ID of each computer +$computerList = $objectOf_Computers.computers.computer | ForEach-Object {$_.ID} - If ( $objectOf_computerGeneral.computer.general.site.name -eq $($objectOf_computerEA.computer.extension_attributes.extension_attribute | Select-Object ID, Value | Where-Object { $_.id -eq $id_EAComputer } | Select-Object Value)) { - Write-Host "Site is set!" - } - else{ - Write-host "Site is incorrect -- updating..." - [xml]$upload_ComputerEA = "${id_EAComputer}$(${objectOf_computerGeneral}.computer.general.site.name)" - Invoke-RestMethod -Uri "${getComputer}/${ID}" -Method Put -Credential $APIcredentials -Body $test_ComputerEA - } +ForEach ( $ID in $computerList ) { + + # Get Computer's General Section + $objectOf_computerGeneral = Invoke-RestMethod -Uri "${getComputer}/${ID}/subset/General" -Method Get -Credential $APIcredentials + + # Get Computer's Extention Attribute Section + $objectOf_computerEA = Invoke-RestMethod -Uri "${getComputer}/${ID}/subset/extension_attributes" -Method Get -Credential $APIcredentials + + If ( $objectOf_computerGeneral.computer.general.site.name -ne $($objectOf_computerEA.computer.extension_attributes.extension_attribute | Select-Object ID, Value | Where-Object { $_.id -eq $id_EAComputer } | Select-Object Value)) { + Write-host "Site is incorrect for computer ID: ${ID} -- updating..." + [xml]$upload_ComputerEA = "${id_EAComputer}$(${objectOf_computerGeneral}.computer.general.site.name)" + Invoke-RestMethod -Uri "${getComputer}/${ID}" -Method Put -Credential $APIcredentials -Body $test_ComputerEA } - } From 6e48c8bc70a6a72a6c2fda683264983a90048186 Mon Sep 17 00:00:00 2001 From: Zack T Date: Fri, 23 Feb 2018 21:56:54 -0700 Subject: [PATCH 07/10] v0.5 = Added Mobile Device Support & Function to update both Device Types + Added support for Mobile Devices + Created function to handle updating of both Device Types --- Jamf Pro/Admin Tools/jamf_ea_Site.ps1 | 64 ++++++++++++++++----------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 index 92ac942..0c00fad 100644 --- a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 +++ b/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 @@ -2,7 +2,7 @@ Script Name: jamf_ea_Site.ps1 By: Zack Thompson / Created: 2/21/2018 -Version: 0.4 / Updated: 2/23/2018 / By: ZT +Version: 0.5 / Updated: 2/23/2018 / By: ZT Description: This script will basically update an EA to the value of the computers Site membership. @@ -14,7 +14,7 @@ Description: This script will basically update an EA to the value of the comput # Jamf EA IDs $id_EAComputer="43" -$id_MobileComputer="43" +$id_EAMobileDevice="1" # Setup Credentials $jamfAPIUser = "" @@ -24,10 +24,12 @@ $APIcredentials = New-Object –TypeName System.Management.Automation.PSCredenti # Setup API URLs $jamfPS="https://newjss.company.com:8443" $getSites="${jamfPS}/JSSResource/sites" -$getComputerEA="${jamfPS}/JSSResource/computerextensionattributes/id/${id_EAComputer}" -$getMobileEA="${jamfPS}/JSSResource/mobiledeviceextensionattributes/id/" $getComputers="${jamfPS}/JSSResource/computers" $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}" # ============================================================ # Functions @@ -70,30 +72,38 @@ function updateSiteList { } } +function updateRecord($deviceType, $urlALL, $urlID, $idEA) { + + Write-Host "Pulling all ${deviceType} records..." + # Get a list of all records + $objectOf_Devices = Invoke-RestMethod -Uri $urlALL -Method Get -Credential $APIcredentials + + Write-Host "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 ) { + $ID = "822" + # Get Computer's General Section + $objectOf_deviceGeneral = Invoke-RestMethod -Uri "${urlID}/${ID}/subset/General" -Method Get -Credential $APIcredentials + + # 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) { + Write-host "Site is incorrect for computer ID: ${ID} -- updating..." + [xml]$upload_deviceEA = "<${deviceType}>${idEA}$(${objectOf_deviceGeneral}.$deviceType.general.site.name)" + Invoke-RestMethod -Uri "${urlID}/${ID}" -Method Put -Credential $APIcredentials -Body $upload_deviceEA + } + } + + +} + # ============================================================ # Bits Staged... # ============================================================ -Write-Host "Pulling all computer records..." -# Get a list of all Computers -$objectOf_Computers = Invoke-RestMethod -Uri $getComputers -Method Get -Credential $APIcredentials - -Write-Host "Pulling data for each computer record..." -# Get the ID of each computer -$computerList = $objectOf_Computers.computers.computer | ForEach-Object {$_.ID} - -ForEach ( $ID in $computerList ) { - - # Get Computer's General Section - $objectOf_computerGeneral = Invoke-RestMethod -Uri "${getComputer}/${ID}/subset/General" -Method Get -Credential $APIcredentials - - # Get Computer's Extention Attribute Section - $objectOf_computerEA = Invoke-RestMethod -Uri "${getComputer}/${ID}/subset/extension_attributes" -Method Get -Credential $APIcredentials - - If ( $objectOf_computerGeneral.computer.general.site.name -ne $($objectOf_computerEA.computer.extension_attributes.extension_attribute | Select-Object ID, Value | Where-Object { $_.id -eq $id_EAComputer } | Select-Object Value)) { - Write-host "Site is incorrect for computer ID: ${ID} -- updating..." - [xml]$upload_ComputerEA = "${id_EAComputer}$(${objectOf_computerGeneral}.computer.general.site.name)" - Invoke-RestMethod -Uri "${getComputer}/${ID}" -Method Put -Credential $APIcredentials -Body $test_ComputerEA - } -} - +# Call Update function for each device type +updateRecord computer $getComputers $getComputer $id_EAComputer +updateRecord mobile_device $getMobileDevices $getMobileDevice $id_EAMobileDevice From 84545325b38757182ab622ebbafd8baba6793470 Mon Sep 17 00:00:00 2001 From: Zack T Date: Mon, 26 Feb 2018 16:53:28 -0700 Subject: [PATCH 08/10] v1.0 = Production Ready + Renamed file --- .../{jamf_ea_Site.ps1 => jamf_assignSiteEA.ps1} | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) rename Jamf Pro/Admin Tools/{jamf_ea_Site.ps1 => jamf_assignSiteEA.ps1} (95%) diff --git a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 b/Jamf Pro/Admin Tools/jamf_assignSiteEA.ps1 similarity index 95% rename from Jamf Pro/Admin Tools/jamf_ea_Site.ps1 rename to Jamf Pro/Admin Tools/jamf_assignSiteEA.ps1 index 0c00fad..a57376f 100644 --- a/Jamf Pro/Admin Tools/jamf_ea_Site.ps1 +++ b/Jamf Pro/Admin Tools/jamf_assignSiteEA.ps1 @@ -1,8 +1,8 @@ <# -Script Name: jamf_ea_Site.ps1 +Script Name: jamf_assignSiteEA.ps1 By: Zack Thompson / Created: 2/21/2018 -Version: 0.5 / Updated: 2/23/2018 / By: ZT +Version: 1.0 / Updated: 2/26/2018 / By: ZT Description: This script will basically update an EA to the value of the computers Site membership. @@ -22,7 +22,7 @@ $jamfAPIPassword = ConvertTo-SecureString -String "" -AsPlainText -Force $APIcredentials = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $jamfAPIUser, $jamfAPIPassword # Setup API URLs -$jamfPS="https://newjss.company.com:8443" +$jamfPS="https://jss.company.com:8443" $getSites="${jamfPS}/JSSResource/sites" $getComputers="${jamfPS}/JSSResource/computers" $getComputer="${jamfPS}/JSSResource/computers/id" @@ -83,7 +83,6 @@ function updateRecord($deviceType, $urlALL, $urlID, $idEA) { $deviceList = $objectOf_Devices."${deviceType}s"."${deviceType}" | ForEach-Object {$_.ID} ForEach ( $ID in $deviceList ) { - $ID = "822" # Get Computer's General Section $objectOf_deviceGeneral = Invoke-RestMethod -Uri "${urlID}/${ID}/subset/General" -Method Get -Credential $APIcredentials @@ -96,8 +95,6 @@ function updateRecord($deviceType, $urlALL, $urlID, $idEA) { Invoke-RestMethod -Uri "${urlID}/${ID}" -Method Put -Credential $APIcredentials -Body $upload_deviceEA } } - - } # ============================================================ From 7118314c041b79b666be3635f12315fbd9d3ae55 Mon Sep 17 00:00:00 2001 From: Zack T Date: Mon, 26 Feb 2018 17:12:11 -0700 Subject: [PATCH 09/10] v1.1 = Added a check on the API credentials provided + Added a check on the API credentials provided to verify that they were valid and had permissions --- Jamf Pro/Admin Tools/jamf_assignSiteEA.ps1 | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Jamf Pro/Admin Tools/jamf_assignSiteEA.ps1 b/Jamf Pro/Admin Tools/jamf_assignSiteEA.ps1 index a57376f..fb6934d 100644 --- a/Jamf Pro/Admin Tools/jamf_assignSiteEA.ps1 +++ b/Jamf Pro/Admin Tools/jamf_assignSiteEA.ps1 @@ -2,7 +2,7 @@ Script Name: jamf_assignSiteEA.ps1 By: Zack Thompson / Created: 2/21/2018 -Version: 1.0 / Updated: 2/26/2018 / By: ZT +Version: 1.1 / Updated: 2/26/2018 / By: ZT Description: This script will basically update an EA to the value of the computers Site membership. @@ -18,7 +18,7 @@ $id_EAMobileDevice="1" # Setup Credentials $jamfAPIUser = "" -$jamfAPIPassword = ConvertTo-SecureString -String "" -AsPlainText -Force +$jamfAPIPassword = ConvertTo-SecureString -String 'SecurePassPhrase' -AsPlainText -Force $APIcredentials = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $jamfAPIUser, $jamfAPIPassword # Setup API URLs @@ -101,6 +101,21 @@ function updateRecord($deviceType, $urlALL, $urlID, $idEA) { # Bits Staged... # ============================================================ +# Verify credentials that were provided by doing an API call and checking the result to verify permissions. +try { + $Response = Invoke-RestMethod -Uri "${jamfPS}/JSSResource/jssuser" -Method Get -Credential $APIcredentials -ErrorVariable RestError -ErrorAction SilentlyContinue +} +catch { + $statusCode = $_.Exception.Response.StatusCode.value__ + $statusDescription = $_.Exception.Response.StatusDescription + + if ($statusCode -notcontains "200") { + Write-Host "ERROR: Invalid Credentials or permissions." + Write-Host "Response: ${statusCode}/${statusDescription}" + exit + } +} + # Call Update function for each device type updateRecord computer $getComputers $getComputer $id_EAComputer updateRecord mobile_device $getMobileDevices $getMobileDevice $id_EAMobileDevice From 0b9fb173069da45a31303f2481894d0ec0cbfb76 Mon Sep 17 00:00:00 2001 From: Zack T Date: Mon, 26 Feb 2018 17:20:07 -0700 Subject: [PATCH 10/10] v1.2 = Changed password acquisition method + Changed password acquisition method based on suggestion from a colleague --- Jamf Pro/Admin Tools/jamf_assignSiteEA.ps1 | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Jamf Pro/Admin Tools/jamf_assignSiteEA.ps1 b/Jamf Pro/Admin Tools/jamf_assignSiteEA.ps1 index fb6934d..eeee082 100644 --- a/Jamf Pro/Admin Tools/jamf_assignSiteEA.ps1 +++ b/Jamf Pro/Admin Tools/jamf_assignSiteEA.ps1 @@ -2,7 +2,7 @@ Script Name: jamf_assignSiteEA.ps1 By: Zack Thompson / Created: 2/21/2018 -Version: 1.1 / Updated: 2/26/2018 / By: ZT +Version: 1.2 / Updated: 2/26/2018 / By: ZT Description: This script will basically update an EA to the value of the computers Site membership. @@ -18,7 +18,13 @@ $id_EAMobileDevice="1" # Setup Credentials $jamfAPIUser = "" -$jamfAPIPassword = ConvertTo-SecureString -String 'SecurePassPhrase' -AsPlainText -Force +# Define Password from within the script. + # $jamfAPIPassword = ConvertTo-SecureString -String 'SecurePassPhrase' -AsPlainText -Force +# Create an encrypted password file. + # $exportPassPhrase = 'SecurePassPhrase' | ConvertTo-Securestring -AsPlainText -Force + # $exportPassPhrase | ConvertFrom-SecureString | Out-File $PSScriptRoot\Cred.txt +# Read in encrypted password. + $jamfAPIPassword = Get-Content $PSScriptRoot\jamf_assignSiteEA_Creds.txt | ConvertTo-SecureString $APIcredentials = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $jamfAPIUser, $jamfAPIPassword # Setup API URLs @@ -60,7 +66,6 @@ function updateSiteList { Write-Host "Adding missing objects to into an XML list..." # For each missing value, add it to the original retrived XML list. ForEach ( $choice in $missingChoices ) { - # Write-Host $choice $newChoice = $objectOf_EAComputer.CreateElement("choice") $newChoice.InnerXml = $choice $objectOf_EAComputer.SelectSingleNode("//popup_choices").AppendChild($newChoice)