Get-GlobalADGroups

  • 8 June 2017
  • harringg
Description: 

This script is used to query the server once and add the data to a variable upon which assorted reports can be run by placing the $GROUPS_Global in the pipeline $GROUPS_Global is declared as a global variable, so you only need to call this function once, at the beginning of your script.

Language: 
Powershell
function Get-GlobalADGroups {
	<#
	.SYNOPSIS
		This gathers the Groups in the ADUC OU
	.DESCRIPTION
		This script is used to query the server once and add the data to a variable
		upon which assorted reports can be run by placing the $GROUPS_Global in the pipeline
		$GROUPS_Global is declared as a global variable, so you only need to call this function once,
		at the begining of your script
	.PARAMETER PRODUCTION
        This will intialize the variable and populate the variable $GROUPS_Global
        If already populated, will gracefully exit
        Verb-Noun -PRODUCTION InitializeVariable
        This will export the data to an XML file for working with offline (disconnected from network)
        Verb-Noun -PRODUCTION ExportXML
 
	.EXAMPLE
				PS C:\> Get-GlobalADGroups -PRODUCTION InitializeVariable
	.NOTES
		    NAME: Get-GlobalADGroups
            AUTHOR: Grant Harrington
            EMAIL: grant.harrington@ars.usda.gov
            CREATED: 6/16/2016 1:45 PM
			LASTEDIT: 3/17/2017 7:00 PM
            KEYWORDS: EAD, SecurityGroups, ADUC
	.LINK
		EAD Scripts
#>
	[CmdletBinding(SupportsPaging = $true,
				   SupportsShouldProcess = $true)]
	[OutputType([array])]
	param
	(
		[Parameter(Mandatory = $TRUE)]
		[ValidateSet('InitializeVariable', 'ExportXML')]
		[string]$PRODUCTION = 'InitializeVariable'
 
	)
 
	BEGIN {
        #Modify for your environment
        #HINT: get-aduser first.last to find full OU via DistinguishedName
		$BaseOU = 'OU=myOU,DC=mydomain,DC=net'
		$GroupOU = 'OU=MyGlobalGroups,' <# Global #>
 
		$SearchBase = "{0}{1}" -f $GroupOU, $BaseOU
 
	} #end BEGIN
 
	PROCESS {
 
		SWITCH ($PRODUCTION) {
			InitializeVariable {
				IF ($GROUPS_Global) {
					Write-Verbose '$GROUPS_Global variable is already populated.'
				} #end IF
				ELSE {
					Write-Verbose 'Populating variable $GROUPS_Global...'
					$global:GROUPS_Global = Get-ADGroup -SearchBase $SearchBase -filter * -Properties *
				} #end ELSE
 
			} #end InitializeVariable
			ExportXML {
				IF ($GROUPS_Global) {
					Write-Verbose 'Exporting CliXml...'
					$GROUPS_Global | Export-Clixml "C:\Temp\Clixml-Groups_Global.xml"
				} #end IF
				ELSE {
					Get-GlobalADGroups -PRODUCTION InitializeVariable -Verbose
				} #end ELSE
			} #end ExportXML
		} #end SWITCH
 
	} #end PROCESS
 
	END {
 
	} #end END
 
} #end Get-GlobalADGroups
 
Get-GlobalADGroups -PRODUCTION InitializeVariable -Verbose

Comments

Note: In my environment, I have all by Global Security Groups in a specific folder (OU), I could modify the script to find GroupScope -eq Global too.

Note: that all applications posted here are posted for use, both commercial and non-commercial, free of charge, and as such are provided without warranty of any kind whatsoever. FMSCUG or any program's author are not responsible for any damages or shortcomings that result from usage of any of these applications.