Advertisement
MrDupont

WaitForOnboarding

Sep 25th, 2023 (edited)
1,721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 2.99 KB | Source Code | 0 0
  1. # Script to track the process HYbrid Join, MDM enrollment, Defender for endpoint onboarding.
  2.  
  3. $logfilepath="C:\Windows\Temp\DefenderOnboarding.log"
  4. $OnboardingFolder = "C:\Windows\Onboarding" #used to set lock screen image by GPO
  5.  
  6. function WriteToLogFile ($message)
  7. {
  8. $message +" - "+ (Get-Date).ToString() >> $logfilepath
  9. }
  10.  
  11. WriteToLogFile "Script started"
  12. #Set powerplan to Ultra Performance. Keeps the machine from going to sleep
  13. WriteToLogFile "Importing high Performance powerplan"
  14. powercfg /import C:\Windows\Temp\High.pow 24c8a99f-711f-424b-1234-790c4168ed60
  15. powercfg /s 24c8a99f-711f-424b-1234-790c4168ed60
  16.  
  17. WriteToLogFile "Waiting for Hybrid Join"
  18. do {
  19.     $AADInfo = Get-Item "HKLM:/SYSTEM/CurrentControlSet/Control/CloudDomainJoin/JoinInfo"
  20.  
  21.     $guids = $AADInfo.GetSubKeyNames()
  22.     foreach ($guid in $guids) {
  23.         $guidSubKey = $AADinfo.OpenSubKey($guid);
  24.         $DeviceDisplayName = ($Null -ne $guidSubKey.GetValue("DeviceDisplayName"))
  25.         Start-Sleep -Seconds 1
  26.     }
  27. } while ($DeviceDisplayName -ne "True")
  28.     WriteToLogFile "Hybrid Joined"
  29.  
  30. WriteToLogFile "Retrigger Co-Management task"
  31. Start-Sleep -Seconds 15
  32. $instance = Get-WmiObject -Namespace root\ccm\dcm -Query "Select * from SMS_DesiredConfiguration WHERE DisplayName = 'CoMgmtSettingsProd'"
  33. Invoke-CimMethod -Namespace root\ccm\dcm -ClassName SMS_DesiredConfiguration -MethodName TriggerEvaluation -Arguments @{“Name” = $instance.Name; “Version” = $instance.Version; “PolicyType” = $instance.PolicyType}
  34.  
  35. WriteToLogFile "Waiting for Intune enrollment"
  36. do {
  37.     $MDMEnrollment = $Null -ne (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Provisioning\OMADM\MDMDeviceID).DeviceClientID
  38.     Start-Sleep -Seconds 1
  39. } while ($MDMEnrollment -ne "True")
  40.     WriteToLogFile "Enrolled in MDM"
  41.  
  42. WriteToLogFile "Waiting for Defender onboarding"
  43. do {
  44.     $MDEState = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows Advanced Threat Protection\Status").onboardingstate -eq "1"
  45.     Start-Sleep -Seconds 1
  46. } while ($MDEState -ne "True")
  47.     WriteToLogFile "Onboarded to Defender for endpoint"
  48.  
  49. WriteToLogFile "Waiting for VPN device profile"
  50. do {
  51.     $VPN = Get-VpnConnection -AllUserConnection -Name "AlwaysON VPN Device" -ErrorAction SilentlyContinue
  52.     Start-Sleep -Seconds 10
  53. } while ($VPN -eq $null)
  54.     WriteToLogFile "VPN profile applied from Intune"
  55.    
  56. Remove-Item -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\Personalization -Force
  57.  
  58. WriteToLogFile "Setting compnay recommended powerplan"
  59. $powerplan = Get-WmiObject -Namespace root\cimv2\power -Class Win32_PowerPlan -Filter "ElementName = 'Company recommended'"
  60. powercfg /setactive ([string]$powerplan.InstanceID).Replace("Microsoft:PowerPlan\{","").Replace("}","")
  61.  
  62. #Delete Ultimate powerplan
  63. WriteToLogFile "Deleting imported performance powerplan"
  64. powercfg /d 24c8a99f-711f-424b-1234-790c4168ed60
  65.  
  66. Remove-Item $OnboardingFolder
  67. Unregister-ScheduledTask -TaskName waitforonboarding -Confirm:$false
  68. WriteToLogFile "Done. Rebooting.."
  69. Restart-Computer -Force
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement