Advertisement
v1ni

Create Install&Uninstall Collections based on AD Queries

Sep 16th, 2019
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #***************************************************
  2. #      THE FOLLOWING SCRIPT WILL CREATE A
  3. #      COLLECTION BASED ON AN ACTIVE
  4. #      DIRECTORY SECURITY GROUP
  5. #***************************************************
  6. #####################PARAMETERS#####################
  7. #
  8. #ENTER NAME OF THE AD SECURITY GROUP #
  9. $ADSecurityGroup = "SG_APP_Notepad++"
  10. #
  11. #ENTER NAME OF APPLICATION/PACKAGE
  12. $PackageName = "Notepad++"
  13. #
  14. #ENTER NAME OF THE AD NETBIOS NAME #
  15. $DomainName = "JVTEST"
  16. ####################################################
  17. #
  18. #*********** DO NOT EDIT BELOW THIS LINE ***********
  19. #
  20. ####################################################
  21. #
  22. #
  23. #
  24. #
  25. #
  26. #
  27. #
  28. #
  29. #
  30. #
  31. #
  32. #
  33. #
  34. #
  35. #
  36. #
  37. #
  38. #
  39. #
  40. #
  41. #
  42. #
  43. #
  44. #
  45. #
  46. #
  47. #
  48. #
  49. #
  50. #
  51. #
  52. #
  53. #
  54. #
  55. #
  56. #
  57. #
  58. #
  59. #
  60. #Load Configuration Manager PowerShell Module
  61. Import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5)+ '\ConfigurationManager.psd1')
  62. #
  63. #Get SiteCode
  64. $SiteCode = Get-PSDrive -PSProvider CMSITE
  65. Set-location $SiteCode":"
  66. #
  67. #Error Handling and output
  68. Clear-Host
  69. $ErrorActionPreference= 'SilentlyContinue'
  70. #
  71. #
  72. $InstCollectionName = 'Install '+$PackageName
  73. $UninstCollectionName = 'Uninstall '+$PackageName
  74. #
  75. #ENTER NAME OF ACTIVE DIRECTORY SECURITY GROUP#
  76. $SecurityGroupName = $DomainName+'\\'+$ADSecurityGroup
  77. #
  78. #
  79. #Limiting COLLECTION
  80. $LimitingCollection = 'All Systems'
  81. #
  82. #
  83. #ENTER COLLECTION SCHEDULE DATE#
  84. $ScheduleDate = "09/09/2019 9:00 PM"
  85. $ScheduleDay = "Sunday"
  86. #
  87. #DEFINE DEPLOYMENT SCHEDULE
  88. $Schedule1 = New-CMSchedule -Start $ScheduleDate -DayOfWeek $ScheduleDay -RecurCount 1
  89. #
  90. #
  91. $QueryRuleName = 'Q_AD_'+$PackageName
  92. #
  93. $SecurityGroupQuery = "select *  from  SMS_R_System where SMS_R_System.SystemGroupName = '$SecurityGroupName' "
  94. #
  95. $AppDetectionQuery = "select distinct SMS_R_System.ResourceId, SMS_R_System.ResourceType, SMS_R_System.Name, SMS_R_System.SMSUniqueIdentifier, SMS_R_System.ResourceDomainORWorkgroup, SMS_R_System.Client from  SMS_R_System inner join SMS_G_System_ADD_REMOVE_PROGRAMS_64 on SMS_G_System_ADD_REMOVE_PROGRAMS_64.ResourceID = SMS_R_System.ResourceId inner join SMS_G_System_ADD_REMOVE_PROGRAMS on SMS_G_System_ADD_REMOVE_PROGRAMS.ResourceID = SMS_R_System.ResourceId where SMS_G_System_ADD_REMOVE_PROGRAMS_64.DisplayName like '%$PackageName%' or SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName like '%$PackageName%'"
  96. #
  97. #Perform the SCCM Magic
  98. #Create Installation Groups
  99. New-CMDeviceCollection -Name $InstCollectionName -Comment "This collection checks for AD Group Membership and performs an automated install, or allows users to perform an install of $PackageName via Software Center" -LimitingCollectionName $LimitingCollection -RefreshSchedule $Schedule1 -RefreshType Both
  100. #
  101. Add-CMDeviceCollectionQueryMembershipRule -CollectionName $InstCollectionName -QueryExpression $SecurityGroupQuery -RuleName $QueryRuleName
  102. #
  103. New-CMApplicationDeployment -CollectionName $InstCollectionName -Name $PackageName -DeployAction Install -DeployPurpose Required -UserNotification DisplaySoftwareCenterOnly -AvailableDateTime (get-date) -OverrideServiceWindow $True -TimeBaseOn LocalTime
  104. #
  105. #Create Uninstallation Groups
  106. New-CMDeviceCollection -Name $UninstCollectionName -Comment "This collection checks for AD Group Membership and performs an automated uninstall, or allows users to perform an uninstall of $PackageName via Software Center if the Device is not a member of the AD Group" -LimitingCollectionName $LimitingCollection -RefreshSchedule $Schedule1 -RefreshType Both
  107. #
  108. Add-CMDeviceCollectionQueryMembershipRule -CollectionName $UninstCollectionName -QueryExpression $AppDetectionQuery -RuleName "Q_Check_For_$PackageName"
  109. Add-CMDeviceCollectionExcludeMembershipRule -CollectionName $UninstCollectionName -ExcludeCollectionName $InstCollectionName
  110. #
  111. New-CMApplicationDeployment -CollectionName $UninstCollectionName -Name $PackageName -DeployAction Uninstall -DeployPurpose Required -UserNotification DisplaySoftwareCenterOnly -AvailableDateTime (get-date) -OverrideServiceWindow $True -TimeBaseOn LocalTime
  112. #
  113. #Uncomment if item is a Package (Not used/tested)
  114. #New-CMPackageDeployment -PackageName "$PackageName" -ProgramName "$PackageName" -CollectionName "$CollectionName" -StandardProgram -FastNetworkOption DownloadContentFromDistributionPointAndRunLocally -SlowNetworkOption DownloadContentFromDistributionPointAndLocally -Comment $PackageName -DeployPurpose Available
  115. #
  116. CD ([Environment]::GetFolderPath("Desktop"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement