Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. function AskJoinGroups { #Joins user to AD groups.
  2. $CanUseGPO = $true
  3. try {
  4. Import-Module GroupPolicy -ErrorAction Stop
  5. $GPO = Get-GPO -All
  6. }
  7. catch {
  8. Write-Host "Module GroupPolicy not Installed. Cannot match by drive letter." -ForegroundColor Yellow
  9. $CanUseGPO = $false
  10. }
  11. $GroupName = Read-Host "`nIf your user needs to be joined or removed from any additional groups, please name them one at a time.`nIf you know the drive letter the user needs access to, enter that instead (no colon).`nIf neither, press enter to proceed, or enter `'?`' for a list of all groups"
  12. if ($GroupName -eq "") {
  13. return
  14. }
  15. elseif ($GroupName -eq '?') {
  16. Get-ADGroup -Filter {Name -like "*"} | select -ExpandProperty Name | Out-Host
  17. }
  18. elseif (($GroupName.length -eq 1) -and ($GroupName -match "^[A-Za-z]+$") -and ($CanUseGPO -eq $true)) {
  19. $GPOResults = @{}
  20. $GPOPerm = @{}
  21. ForEach ($Policy in $GPO){
  22. $GPOID = $Policy.Id
  23. $GPODom = $Policy.DomainName
  24. $GPODisp = $Policy.DisplayName
  25. if ((Test-Path "\\$($GPODom)\SYSVOL\$($GPODom)\Policies\{$($GPOID)}\User\Preferences\Drives\Drives.xml") -and ($Policy.GpoStatus -eq "AllSettingsEnabled")) {
  26. [xml]$DriveXML = Get-Content "\\$($GPODom)\SYSVOL\$($GPODom)\Policies\{$($GPOID)}\User\Preferences\Drives\Drives.xml"
  27. ForEach ($drivemap in $DriveXML.Drives.Drive ) {
  28. if ($drivemap.Properties.Letter -eq $GroupName) {
  29. if ($drivemap.Filters.FilterGroup.Name -eq $null) {
  30. $PermArray = (Get-GPPermissions -Name $GPODisp -All | ?{$_.Permission -eq "GpoApply"}).Trustee.Name
  31. ForEach ($Perm in $PermArray) {
  32. $GPOPerm.$GPODisp += @("$Perm")
  33. }
  34. continue
  35. }
  36. else {
  37. $garbage, $GPOName = ($drivemap.Filters.FilterGroup.Name.split("`\"))
  38. $GPOResults.$GPODisp += @("$GPOName")
  39. }
  40. }
  41. }
  42. }
  43. }
  44. if ($GPOResults.Count -ne 0) {
  45. $ItemTargetResults = @($GPOResults.Values)
  46. }
  47. if (($GPOResults.Count -eq 0) -and ($GPOPerm.Count -ne 0)) {
  48. Write-Host "`nItem level targetting is not applied for this drive.`nThe user must be a member of any of the following groups/OU's for this drive to be mapped:"
  49. $GPOPerm.Keys | Select @{l='GPO Object';e={$_}},@{l='Scope OU/Group';e={$GPOPerm.$_}} | Out-Host
  50. }
  51. elseif ($GPOResults.Keys -contains "Domain Users") {
  52. Write-Host "`nThis drive is mapped to Domain Users and does not require explicit mapping." -ForegroundColor Yellow
  53. }
  54. elseif (($GPOResults.Count -ne 0) -and ($GPOPerm.Count -ne 0)) {
  55. Write-Host "`nMultiple item-level targetting and scope-targetting mappings found for the specified drive letter. Add one of the following if you dare. God help you."
  56. $GPOPerm.Keys | Select @{l='GPO Object';e={$_}},@{l='Scope OU/Group';e={$GPOPerm.$_}} | Out-Host
  57. $GPOResults.Keys | Select @{l='GPO Object';e={$_}},@{l='Group Name';e={$GPOResults.$_}} | Out-Host
  58. }
  59. elseif ($ItemTargetResults.Length -eq 1) {
  60. $SingleGPO = $ItemTargetResults[0]
  61. $Match = ProcessGroup ($SingleGPO)
  62. }
  63. elseif (($ItemTargetResults.Length -ge 2) -and ($GPOPerm.Count -eq 0)) {
  64. $GPOResults.Keys | Select @{l='GPO Object';e={$_}},@{l='Group Name';e={$GPOResults.$_}} | Out-Host
  65. $Match = $False
  66. $Choice = Read-Host -Prompt "`nMultiple Groups map to the specified drive letter. Please enter the appropriate group name"
  67. ForEach ($Listing in $GPOResults.Values) {
  68. if ($Choice -eq $Listing) {
  69. $Match = ProcessGroup ($Listing)
  70. }
  71. }
  72. if ($Match -eq $False) {
  73. Write-Host "`nInvalid selection. Please try again." -ForegroundColor Yellow
  74. }
  75. }
  76. else {
  77. $GroupName = $GroupName.ToUpper() + ":"
  78. Write-Host "`nNo groups found mapped to drive letter $GroupName.`nPlease try again, or enter the specific group name." -ForegroundColor Yellow
  79. }
  80. }
  81. elseif ((Get-ADGroup -Filter {Name -eq $GroupName}) -eq $null) {
  82. Write-Host "`nThat group does not exist. Please try again." -ForegroundColor Yellow
  83. }
  84. else {
  85. $Match = ProcessGroup ($GroupName)
  86. }
  87. AskJoinGroups
  88. }
  89.  
  90. function ProcessGroup ($SingleGPO) {
  91. $GroupDN = (Get-ADGroup -Filter {Name -eq $SingleGPO}).DistinguishedName
  92. $RealGroupName = (Get-ADGroup -Filter {Name -eq $SingleGPO}).Name
  93. if ((Get-ADGroupMember -Identity $GroupDN).Name -contains $FullName) {
  94. $choose = Read-Host -Prompt "`n$Firstname already belongs to this group. Would you like to remove them from this group? y/n"
  95. if ($choose -eq 'y') {
  96. Remove-ADGroupMember -Identity $GroupDN -Members $Sam -Confirm:$false
  97. $script:Properties['Groups'] = @($script:Properties['Groups'] | ?{$_ -ne "$RealGroupName"})
  98. Write-Host "`n$Firstname has been removed from $RealGroupName"
  99. }
  100. else {
  101. Write-Host "`nMembership has not been changed."
  102. }
  103. }
  104. else {
  105. Add-ADGroupMember -Identity $GroupDN -Members $Sam
  106. Write-Host "`nSuccess! $Sam was joined to $RealGroupName."
  107. $script:Properties.Groups += @($RealGroupName)
  108. }
  109. return $True
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement