Guest User

Get Members

a guest
Mar 28th, 2021
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.75 KB | None | 0 0
  1. Write-host "
  2.  
  3. Distribution Group Member Report
  4. ----------------------------
  5.  
  6. 1.Display in Exchange Management Shell
  7.  
  8. 2.Export to CSV File
  9.  
  10. 3.Enter the Distribution Group name with Wild Card (Export)
  11.  
  12. 4.Enter the Distribution Group name with Wild Card (Display)
  13.  
  14. Dynamic Distribution Group Member Report
  15. ----------------------------
  16.  
  17. 5.Display in Exchange Management Shell
  18.  
  19. 6.Export to CSV File
  20.  
  21. 7.Enter the Dynamic Distribution Group name with Wild Card (Export)
  22.  
  23. 8.Enter the Dynamic Group name with Wild Card (Display)"-ForeGround "Cyan"
  24.  
  25. #----------------
  26. # Script
  27. #----------------
  28.  
  29. Write-Host " "
  30.  
  31. $number = Read-Host "Choose The Task"
  32. $output = @()
  33. switch ($number)
  34. {
  35.  
  36. 1 {
  37.  
  38. $AllDG = Get-DistributionGroup -resultsize unlimited
  39. Foreach($dg in $allDg)
  40. {
  41. $Members = Get-DistributionGroupMember $Dg.name -resultsize unlimited
  42.  
  43.  
  44. if($members.count -eq 0)
  45. {
  46. $userObj = New-Object PSObject
  47. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value EmtpyGroup
  48. $userObj | Add-Member NoteProperty -Name "Alias" -Value EmtpyGroup
  49. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value EmtpyGroup
  50. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  51. Write-Output $Userobj
  52. }
  53. else
  54. {
  55. Foreach($Member in $members)
  56. {
  57. $userObj = New-Object PSObject
  58. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value $member.Name
  59. $userObj | Add-Member NoteProperty -Name "Alias" -Value $member.Alias
  60. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value $member.PrimarySmtpAddress
  61. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  62. Write-Output $Userobj
  63. }
  64.  
  65. }
  66.  
  67. }
  68.  
  69. ;Break}
  70.  
  71. 2 {
  72.  
  73. $i = 0
  74.  
  75. $CSVfile = Read-Host "Enter the Path of CSV file (Eg. C:\DG.csv)"
  76.  
  77. $AllDG = Get-DistributionGroup -resultsize unlimited
  78.  
  79. Foreach($dg in $allDg)
  80. {
  81. $Members = Get-DistributionGroupMember $Dg.name -resultsize unlimited
  82.  
  83. if($members.count -eq 0)
  84. {
  85. $managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
  86.  
  87. $userObj = New-Object PSObject
  88.  
  89. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value EmptyGroup
  90. $userObj | Add-Member NoteProperty -Name "Alias" -Value EmptyGroup
  91. $userObj | Add-Member NoteProperty -Name "RecipientType" -Value EmptyGroup
  92. $userObj | Add-Member NoteProperty -Name "Recipient OU" -Value EmptyGroup
  93. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value EmptyGroup
  94. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  95. $userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP address" -Value $DG.PrimarySmtpAddress
  96. $userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
  97. $userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
  98. $userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.GroupType
  99. $userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType
  100.  
  101. $output += $UserObj
  102.  
  103. }
  104. else
  105. {
  106. Foreach($Member in $members)
  107. {
  108.  
  109. $managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
  110.  
  111. $userObj = New-Object PSObject
  112.  
  113. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value $Member.Name
  114. $userObj | Add-Member NoteProperty -Name "Alias" -Value $Member.Alias
  115. $userObj | Add-Member NoteProperty -Name "RecipientType" -Value $Member.RecipientType
  116. $userObj | Add-Member NoteProperty -Name "Recipient OU" -Value $Member.OrganizationalUnit
  117. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value $Member.PrimarySmtpAddress
  118. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  119. $userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP address" -Value $DG.PrimarySmtpAddress
  120. $userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
  121. $userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
  122. $userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.GroupType
  123. $userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType
  124.  
  125. $output += $UserObj
  126.  
  127. }
  128. }
  129. # update counters and write progress
  130. $i++
  131. Write-Progress -activity "Scanning Groups . . ." -status "Scanned: $i of $($allDg.Count)" -percentComplete (($i / $allDg.Count) * 100)
  132. $output | Export-csv -Path $CSVfile -NoTypeInformation
  133.  
  134. }
  135.  
  136. ;Break}
  137.  
  138. 3 {
  139.  
  140. $i = 0
  141.  
  142. $CSVfile = Read-Host "Enter the Path of CSV file (Eg. C:\DG.csv)"
  143.  
  144. $Dgname = Read-Host "Enter the DG name or Range (Eg. DGname , DG*,*DG)"
  145.  
  146. $AllDG = Get-DistributionGroup $Dgname -resultsize unlimited
  147.  
  148. Foreach($dg in $allDg)
  149.  
  150. {
  151.  
  152. $Members = Get-DistributionGroupMember $Dg.name -resultsize unlimited
  153.  
  154. if($members.count -eq 0)
  155. {
  156. $managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
  157.  
  158. $userObj = New-Object PSObject
  159.  
  160. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value EmptyGroup
  161. $userObj | Add-Member NoteProperty -Name "Alias" -Value EmptyGroup
  162. $userObj | Add-Member NoteProperty -Name "RecipientType" -Value EmptyGroup
  163. $userObj | Add-Member NoteProperty -Name "Recipient OU" -Value EmptyGroup
  164. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value EmptyGroup
  165. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  166. $userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP address" -Value $DG.PrimarySmtpAddress
  167. $userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
  168. $userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
  169. $userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.GroupType
  170. $userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType
  171.  
  172. $output += $UserObj
  173.  
  174. }
  175. else
  176. {
  177. Foreach($Member in $members)
  178. {
  179.  
  180. $managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
  181.  
  182. $userObj = New-Object PSObject
  183.  
  184. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value $Member.Name
  185. $userObj | Add-Member NoteProperty -Name "Alias" -Value $Member.Alias
  186. $userObj | Add-Member NoteProperty -Name "RecipientType" -Value $Member.RecipientType
  187. $userObj | Add-Member NoteProperty -Name "Recipient OU" -Value $Member.OrganizationalUnit
  188. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value $Member.PrimarySmtpAddress
  189. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  190. $userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP address" -Value $DG.PrimarySmtpAddress
  191. $userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
  192. $userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
  193. $userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.GroupType
  194. $userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType
  195.  
  196. $output += $UserObj
  197.  
  198. }
  199. }
  200. # update counters and write progress
  201. $i++
  202. Write-Progress -activity "Scanning Groups . . ." -status "Scanned: $i of $($allDg.Count)" -percentComplete (($i / $allDg.Count) * 100)
  203. $output | Export-csv -Path $CSVfile -NoTypeInformation
  204.  
  205. }
  206.  
  207. ;Break}
  208.  
  209. 4 {
  210.  
  211. $Dgname = Read-Host "Enter the DG name or Range (Eg. DGname , DG*,*DG)"
  212.  
  213. $AllDG = Get-DistributionGroup $Dgname -resultsize unlimited
  214.  
  215. Foreach($dg in $allDg)
  216.  
  217. {
  218.  
  219. $Members = Get-DistributionGroupMember $Dg.name -resultsize unlimited
  220.  
  221. if($members.count -eq 0)
  222. {
  223. $userObj = New-Object PSObject
  224. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value EmtpyGroup
  225. $userObj | Add-Member NoteProperty -Name "Alias" -Value EmtpyGroup
  226. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value EmtpyGroup
  227. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  228. Write-Output $Userobj
  229. }
  230. else
  231. {
  232. Foreach($Member in $members)
  233. {
  234. $userObj = New-Object PSObject
  235. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value $member.Name
  236. $userObj | Add-Member NoteProperty -Name "Alias" -Value $member.Alias
  237. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value $member.PrimarySmtpAddress
  238. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  239. Write-Output $Userobj
  240. }
  241.  
  242. }
  243.  
  244. }
  245.  
  246. ;Break}
  247.  
  248. 5 {
  249.  
  250. $AllDG = Get-DynamicDistributionGroup -resultsize unlimited
  251.  
  252. Foreach($dg in $allDg)
  253.  
  254. {
  255.  
  256. $Members = Get-Recipient -RecipientPreviewFilter $dg.RecipientFilter -resultsize unlimited
  257.  
  258. if($members.count -eq 0)
  259. {
  260. $userObj = New-Object PSObject
  261. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value EmtpyGroup
  262. $userObj | Add-Member NoteProperty -Name "Alias" -Value EmtpyGroup
  263. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value EmtpyGroup
  264. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  265. Write-Output $Userobj
  266. }
  267. else
  268. {
  269. Foreach($Member in $members)
  270. {
  271. $userObj = New-Object PSObject
  272. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value $member.Name
  273. $userObj | Add-Member NoteProperty -Name "Alias" -Value $member.Alias
  274. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value $member.PrimarySmtpAddress
  275. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  276. Write-Output $Userobj
  277. }
  278.  
  279. }
  280.  
  281. }
  282.  
  283. ;Break}
  284.  
  285. 6 {
  286. $i = 0
  287.  
  288. $CSVfile = Read-Host "Enter the Path of CSV file (Eg. C:\DYDG.csv)"
  289.  
  290. $AllDG = Get-DynamicDistributionGroup -resultsize unlimited
  291.  
  292. Foreach($dg in $allDg)
  293.  
  294. {
  295.  
  296. $Members = Get-Recipient -RecipientPreviewFilter $dg.RecipientFilter -resultsize unlimited
  297.  
  298. if($members.count -eq 0)
  299. {
  300. $managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
  301.  
  302. $userObj = New-Object PSObject
  303.  
  304. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value EmptyGroup
  305. $userObj | Add-Member NoteProperty -Name "Alias" -Value EmptyGroup
  306. $userObj | Add-Member NoteProperty -Name "RecipientType" -Value EmptyGroup
  307. $userObj | Add-Member NoteProperty -Name "Recipient OU" -Value EmptyGroup
  308. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value EmptyGroup
  309. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  310. $userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP address" -Value $DG.PrimarySmtpAddress
  311. $userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
  312. $userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
  313. $userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.RecipientType
  314. $userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType
  315.  
  316. $output += $UserObj
  317.  
  318. }
  319. else
  320. {
  321. Foreach($Member in $members)
  322. {
  323.  
  324. $managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
  325.  
  326. $userObj = New-Object PSObject
  327.  
  328. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value $Member.Name
  329. $userObj | Add-Member NoteProperty -Name "Alias" -Value $Member.Alias
  330. $userObj | Add-Member NoteProperty -Name "RecipientType" -Value $Member.RecipientType
  331. $userObj | Add-Member NoteProperty -Name "Recipient OU" -Value $Member.OrganizationalUnit
  332. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value $Member.PrimarySmtpAddress
  333. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  334. $userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP address" -Value $DG.PrimarySmtpAddress
  335. $userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
  336. $userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
  337. $userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.RecipientType
  338. $userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType
  339.  
  340. $output += $UserObj
  341.  
  342. }
  343. }
  344. # update counters and write progress
  345. $i++
  346. Write-Progress -activity "Scanning Groups . . ." -status "Scanned: $i of $($allDg.Count)" -percentComplete (($i / $allDg.Count) * 100)
  347. $output | Export-csv -Path $CSVfile -NoTypeInformation
  348.  
  349. }
  350.  
  351. ;Break}
  352.  
  353. 7 {
  354. $i = 0
  355.  
  356. $CSVfile = Read-Host "Enter the Path of CSV file (Eg. C:\DYDG.csv)"
  357.  
  358. $Dgname = Read-Host "Enter the DG name or Range (Eg. DynmicDGname , Dy*,*Dy)"
  359.  
  360. $AllDG = Get-DynamicDistributionGroup $Dgname -resultsize unlimited
  361.  
  362. Foreach($dg in $allDg)
  363.  
  364. {
  365.  
  366. $Members = Get-Recipient -RecipientPreviewFilter $dg.RecipientFilter -resultsize unlimited
  367.  
  368. if($members.count -eq 0)
  369. {
  370. $managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
  371.  
  372. $userObj = New-Object PSObject
  373.  
  374. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value EmptyGroup
  375. $userObj | Add-Member NoteProperty -Name "Alias" -Value EmptyGroup
  376. $userObj | Add-Member NoteProperty -Name "RecipientType" -Value EmptyGroup
  377. $userObj | Add-Member NoteProperty -Name "Recipient OU" -Value EmptyGroup
  378. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value EmptyGroup
  379. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  380. $userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP address" -Value $DG.PrimarySmtpAddress
  381. $userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
  382. $userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
  383. $userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.RecipientType
  384. $userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType
  385.  
  386. $output += $UserObj
  387.  
  388. }
  389. else
  390. {
  391. Foreach($Member in $members)
  392. {
  393.  
  394. $managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
  395.  
  396. $userObj = New-Object PSObject
  397.  
  398. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value $Member.Name
  399. $userObj | Add-Member NoteProperty -Name "Alias" -Value $Member.Alias
  400. $userObj | Add-Member NoteProperty -Name "RecipientType" -Value $Member.RecipientType
  401. $userObj | Add-Member NoteProperty -Name "Recipient OU" -Value $Member.OrganizationalUnit
  402. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value $Member.PrimarySmtpAddress
  403. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  404. $userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP address" -Value $DG.PrimarySmtpAddress
  405. $userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
  406. $userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
  407. $userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.RecipientType
  408. $userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType
  409.  
  410. $output += $UserObj
  411.  
  412. }
  413. }
  414. # update counters and write progress
  415. $i++
  416. Write-Progress -activity "Scanning Groups . . ." -status "Scanned: $i of $($allDg.Count)" -percentComplete (($i / $allDg.Count) * 100)
  417. $output | Export-csv -Path $CSVfile -NoTypeInformation
  418.  
  419. }
  420.  
  421. ;Break}
  422.  
  423. 8 {
  424.  
  425. $Dgname = Read-Host "Enter the Dynamic DG name or Range (Eg. DynamicDGname , DG*,*DG)"
  426.  
  427. $AllDG = Get-DynamicDistributionGroup $Dgname -resultsize unlimited
  428.  
  429. Foreach($dg in $allDg)
  430.  
  431. {
  432.  
  433. $Members = Get-Recipient -RecipientPreviewFilter $dg.RecipientFilter -resultsize unlimited
  434.  
  435. if($members.count -eq 0)
  436. {
  437. $userObj = New-Object PSObject
  438. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value EmtpyGroup
  439. $userObj | Add-Member NoteProperty -Name "Alias" -Value EmtpyGroup
  440. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value EmtpyGroup
  441. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  442. Write-Output $Userobj
  443. }
  444. else
  445. {
  446. Foreach($Member in $members)
  447. {
  448. $userObj = New-Object PSObject
  449. $userObj | Add-Member NoteProperty -Name "DisplayName" -Value $member.Name
  450. $userObj | Add-Member NoteProperty -Name "Alias" -Value $member.Alias
  451. $userObj | Add-Member NoteProperty -Name "Primary SMTP address" -Value $member.PrimarySmtpAddress
  452. $userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
  453. Write-Output $Userobj
  454. }
  455.  
  456. }
  457.  
  458. }
  459.  
  460. ;Break}
  461.  
  462. Default {Write-Host "No matches found , Enter Options 1 or 2" -ForeGround "red"}
  463.  
  464. }
Advertisement
Add Comment
Please, Sign In to add comment