Advertisement
Guest User

JSSUpdateEmailFromAD.ps1

a guest
Sep 24th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #CONFIGURE==============
  2. #If false, will tell you what the script will do without actually doing it
  3. $production = $true
  4.  
  5. #region JSS API Credentials
  6. $jssURL = "https://jss.domain.com:8443"
  7. $apiUser = "user"
  8. $apiPass = "password"
  9. #endregion
  10.  
  11. #Smart Group of devices to update user
  12. $smartGroup = "Temp"
  13.  
  14.  
  15. #END CONFIGURE===============
  16.  
  17.  
  18. $jssCredential = New-Object System.Management.Automation.PSCredential ($apiUser, (ConvertTo-SecureString $apiPass -AsPlainText -Force))
  19.  
  20. $usernameList = @()
  21.  
  22.  
  23.  
  24. Function GetJSS($request) {
  25. Return Invoke-RestMethod -Uri $jssURL'/JSSResource/'$request -Credential $jssCredential -Method Get
  26. }
  27.  
  28.  
  29. Function ApplyUser {
  30.  
  31. [CmdletBinding()]
  32. PARAM (
  33. [Parameter(Mandatory=$true)] $ipad,
  34. [Parameter(Mandatory=$true)] [string] $ipademail
  35. )
  36.  
  37.  
  38. $PostXML = "
  39. <mobile_device>
  40. <location>
  41. <email_address>$ipademail</email_address>
  42. </location>
  43. </mobile_device>
  44. "
  45. #write-host $PostXML
  46. #Write-Host "Uploading Command to JSS..."
  47. Invoke-RestMethod -Credential $jssCredential -Uri $jssURL'/JSSResource/mobiledevices/id/'$iPad -Method Put -Body $PostXML -ContentType "text/xml"
  48. }
  49.  
  50. Function RetrieveIpadList {
  51.  
  52. [CmdletBinding()]
  53. PARAM (
  54. [Parameter(Mandatory=$true)] $group
  55. )
  56.  
  57. $rawdata = GetJSS("mobiledevicegroups/name/$group")
  58. $data = $rawData.SelectNodes('//mobile_device_group/mobile_devices/mobile_device')
  59. $ipadList = [System.Collections.ArrayList]@()
  60. foreach ($device in $data) {
  61. [Void]$ipadlist.Add($device.id)
  62. }
  63.  
  64. Return $ipadList
  65. }
  66.  
  67. Function RetrieveIpadDetails {
  68.  
  69. [CmdletBinding()]
  70. PARAM (
  71. [Parameter(Mandatory=$true)] $id
  72. )
  73.  
  74. $rawdata = GetJSS("mobiledevices/id/$id")
  75. $data = $rawData.SelectNodes('//mobile_device')
  76.  
  77. Return $data
  78. }
  79.  
  80. # Function to check if a student already exists in AD
  81. Function GetADEmail($checkThis)
  82. {
  83. $User = Get-ADUser -Filter {samAccountName -eq $checkThis} -Properties mail
  84. If ($User -eq $Null) { Return "" }
  85. Else { Return $User.mail }
  86. }
  87.  
  88. $ipads = RetrieveIpadList -group $smartGroup
  89. Write-Host $ipads.Count "iPads in Group"
  90.  
  91. foreach ($ipad in $ipads) {
  92.  
  93. $ipadinfo = (RetrieveIpadDetails -id $ipad)
  94. if($ipadInfo.location.username) {
  95. $ademail = GetADEmail($ipadInfo.location.username)
  96. if($ademail) {
  97. if($production) {ApplyUser -ipad $ipad -ipademail $ademail}
  98. else {write-host Would apply $ademail to iPad ID $ipad}
  99. }
  100. }
  101.  
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement