v1.5 = Added logging

+ Added logging to an external file
 + Added a Stop Watch function to record amount of time it takes to process
This commit is contained in:
Zack T
2018-07-02 23:09:50 -07:00
parent 407d6ac244
commit 6ca7ea275e

View File

@@ -2,13 +2,36 @@
Script Name: jamf_assignSiteEA.ps1
By: Zack Thompson / Created: 2/21/2018
Version: 1.4 / Updated: 4/12/2018 / By: ZT
Version: 1.5 / Updated: 7/2/2018 / By: ZT
Description: This script will basically update an EA to the value of the computers Site membership.
#>
Write-Host "jamf_assignSiteEA Process: START"
# Log Location
$Global:LogPath = "C:\Temp\jamf_assignSiteEA.log"
Function Add-LogContent {
Param (
[parameter(Mandatory = $false)]
[switch]$Load,
[parameter(Mandatory = $true)]
$Content
)
If ($Load) {
If ((Get-Item $LogPath).length -gt 1000kb) {
Write-Output "$(Get-Date -Format G) - $Content" > $LogPath
}
Else {
Write-Output "$(Get-Date -Format G) - $Content" >> $LogPath
}
}
Else {
Write-Output "$(Get-Date -Format G) - $Content" >> $LogPath
}
}
Add-LogContent -Load -Content " "
Add-LogContent -Content "jamf_assignSiteEA Process: START"
# ============================================================
# Define Variables
@@ -45,7 +68,7 @@ $getMobileEA="${jamfPS}/JSSResource/mobiledeviceextensionattributes/id/${id_Mobi
function updateSiteList {
Write-Host "Pulling required data..."
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.
@@ -53,19 +76,19 @@ function updateSiteList {
# 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"
Add-LogContent -Content "Site count equal Computer EA Choice Count"
Add-LogContent -Content "Presuming these are up to date"
}
else {
Write-Host "Site count does not equal Computer EA Choice Count"
Add-LogContent -Content "Site count does not equal Computer EA 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..."
Add-LogContent -Content "Finding the missing objects..."
$missingChoices = $(Compare-Object -ReferenceObject $SiteList -DifferenceObject $EASiteList) | ForEach-Object { $_.InputObject }
Write-Host "Adding missing objects to into an XML list..."
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")
@@ -74,18 +97,18 @@ function updateSiteList {
}
# Upload the XML back.
Write-Host "Updating the EA Computer List..."
Add-LogContent -Content "Updating the EA Computer List..."
Invoke-RestMethod -Uri $getComputerEA -Method Put -Credential $APIcredentials -Body $objectOf_EAComputer
}
}
function updateRecord($deviceType, $urlALL, $urlID, $idEA) {
Write-Host "Pulling all ${deviceType} records..."
Add-LogContent -Content "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..."
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}
@@ -97,7 +120,7 @@ function updateRecord($deviceType, $urlALL, $urlID, $idEA) {
$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 ${deviceType} ID: ${ID} -- updating..."
Add-LogContent -Content "Site is incorrect for ${deviceType} ID: ${ID} -- updating..."
[xml]$upload_deviceEA = "<?xml version='1.0' encoding='UTF-8'?><${deviceType}><extension_attributes><extension_attribute><id>${idEA}</id><value>$(${objectOf_deviceGeneral}.$deviceType.general.site.name)</value></extension_attribute></extension_attributes></${deviceType}>"
Try {
@@ -108,13 +131,13 @@ function updateRecord($deviceType, $urlALL, $urlID, $idEA) {
$statusDescription = $_.Exception.Response.StatusDescription
If ($statusCode -notcontains "200") {
Write-host " -> Failed to assign site for ${deviceType} ID: ${ID}"
Write-Host " --> Response: ${statusCode}/${statusDescription}: $($RestError.Message | ForEach { $_.Split(":")[1];} | ForEach { $_.Split([Environment]::NewLine)[0];})"
Add-LogContent -Content " -> Failed to assign site for ${deviceType} ID: ${ID}"
Add-LogContent -Content " --> Response: ${statusCode}/${statusDescription}: $($RestError.Message | ForEach { $_.Split(":")[1];} | ForEach { $_.Split([Environment]::NewLine)[0];})"
}
}
}
}
Write-Host "All ${deviceType} records have been processed."
Add-LogContent -Content "All ${deviceType} records have been processed."
}
# ============================================================
@@ -122,7 +145,7 @@ function updateRecord($deviceType, $urlALL, $urlID, $idEA) {
# ============================================================
# Verify credentials that were provided by doing an API call and checking the result to verify permissions.
Write-Host "Verifying API credentials..."
Add-LogContent -Content "Verifying API credentials..."
Try {
$Response = Invoke-RestMethod -Uri "${jamfPS}/JSSResource/jssuser" -Method Get -Credential $APIcredentials -ErrorVariable RestError -ErrorAction SilentlyContinue
}
@@ -131,17 +154,22 @@ Catch {
$statusDescription = $_.Exception.Response.StatusDescription
If ($statusCode -notcontains "200") {
Write-Host "ERROR: Invalid Credentials or permissions."
Write-Host "Response: ${statusCode}/${statusDescription}"
Write-Host "jamf_assignSiteEA Process: FAILED"
Add-LogContent -Content "ERROR: Invalid Credentials or permissions."
Add-LogContent -Content "Response: ${statusCode}/${statusDescription}"
Add-LogContent -Content "jamf_assignSiteEA Process: FAILED"
Exit
}
}
Write-Host "API Credentials Valid -- continuing..."
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
Write-Host "jamf_assignSiteEA Process: COMPLETE"
$StopWatch.Stop()
Add-LogContent -Content "Mins: " $StopWatch.Elapsed.Minutes "Seconds: " $StopWatch.Elapsed.Seconds
Add-LogContent -Content "jamf_assignSiteEA Process: COMPLETE"