Compile multiple same line command to make a good script

Steve Bona
Steve Bona used Ask the Experts™
on
Is it a better way to compile my script that have a lot of repetetives command to make it easier to use ?

# Creation of many Domain Global group to be nest in one Domain Local Group 

$LocalGroupName = "DL-ACL-M-OCTOPUS-Shares-DAL"
$GlobalGroupName = "GG-ACL-M-OCTOPUS-Shares-DAL"
$CCSMTLServer = "$CCSMTLServer.ccsmtl.local.lab"

#regionCreate Domain Local Group in Domain CCSMTL
$Pathccsmtl= "OU=ACL,OU=Groupes,DC=CCSMTL,DC=local,DC=lab"
New-ADGroup -Name $LocalGroupName -GroupScope DomainLocal -Path $Pathccsmtl -Server $CCSMTLServer
#endregion 

#region Create Domain Global Group in Domain CRLB
$pathcrlb="OU=CCSMTL,OU=CRLB - CIUSSS,DC=lucie-bruneau,DC=local,DC=lab"
$serveurcrlb="S-ADS039.lucie-bruneau.local.lab"
New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathcrlb -Server $serveurcrlb 
# Add Member to Domain Local Group in CCSMTL
$accountGroupcrlb = Get-ADGroup -Identity $GlobalGroupName -Server $serveurcrlb
Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupcrlb -Server $CCSMTLServer
#endregion

#region Create Domain Global Group in Domain IRD
$pathird="OU=CCSMTL,OU=IRD_CIUSSS,DC=ird,DC=local,DC=lab"
$serveurird="irdsvr0045.ird.local.lab"
New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathird -Server $serveurird 
# Add Member to Domain Local Group in CCSMTL
$accountGroupird = Get-ADGroup -Identity $GlobalGroupName -Server $serveurird
Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupird -Server $CCSMTLServer
#endregion

#region Create Domain Global Group in Domain SOV
$serveursov= "TCRSVR1773.cssssov.local.lab"
$pathSOV="OU=CCSMTL,OU=ENTITES,DC=cssssov,DC=local,DC=lab"
New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathSOV -Server $serveursov 
# Add Member to Domain Local Group in CCSMTL
$accountGroupsov = Get-ADGroup -Identity $GlobalGroupName -Server $serveursov
Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupsov -Server $CCSMTLServer
#endregion

#region Create Domain Global Group in Domain TCR
$pathtcr="OU=DRI,OU=Groupes,OU=Utilisateurs,DC=tcrmtl,DC=local,DC=lab"
$serveurtcr="tcrsvr1447.tcrmtl.local.lab"
New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathtcr -Server $serveurtcr 
$accountGrouptcr = Get-ADGroup -Identity $GlobalGroupName -Server $serveurtcr
Add-ADGroupMember -Identity $LocalGroupName -Members $accountGrouptcr -Server $CCSMTLServer
#endregion

#region Create Domain Global Group in Domain CCSMTL
$Pathccsmtl= "OU=ACL,OU=Groupes,DC=CCSMTL,DC=local,DC=lab"
New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathccsmtl -Server $CCSMTLServer 
# Add Member to Domain Local Group in CCSMTL
$accountGroupcjmtl = Get-ADGroup -Identity $GlobalGroupName -Server $serveurcjmtl
Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupcjmtl -Server $CCSMTLServer
#endregion

#region Create Domain Global Group in Domain CJMTL
$pathcjmtl="OU=CCSMTL,dc=cjmtl,DC=local,DC=lab"
$serveurcjmtl= "ss001w1.cjmtl.local.lab"
New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathcjmtl -Server $serveurcjmtl 
# Add Member to Domain Local Group in CCSMTL
$accountGroupcjmtl = Get-ADGroup -Identity $GlobalGroupName -Server $serveurcjmtl
Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupcjmtl -Server $CCSMTLServer
#endregion

#region Create Domain Global Group in Domain IRGLM
$pathirglm="ou=CCSMTL,ou=IRGLM - CIUSSS,dc=irglm,DC=local,DC=lab"
$serveurirglm="Tcrsvr0398.irglm.local.lab"
New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathirglm -Server $serveurirglm 
# Add Member to Domain Local Group in CCSMTL
$accountGroupirglm = Get-ADGroup -Identity $GlobalGroupName -Server $serveurirglm
Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupirglm -Server $CCSMTLServer
#endregion

#region Create Domain Global Group in Domain CDC
$pathcdc="OU=CCSMTL,OU=CIUSSS-CEMTL,dc=cdc,DC=local,DC=lab"
$serveurcdc= "S-ADS045.cdc.local.lab"
New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathcdc -Server $serveurcdc 
# Add Member to Domain Local Group  in CCSMTL
$accountGroupcdc = Get-ADGroup -Identity $GlobalGroupName -Server $serveurcdc
Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupcdc -Server $CCSMTLServer
#endregion

#region Create Domain Global Group in Domain CRDITEDMTL
$pathcrditedmtl="OU=CCSMTL,OU=Organisation,DC=crditedmtl,DC=local,DC=lab"
$serveurcrditedmtl="Tcrsvr1237.crditedmtl.local.lab"
New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathcrditedmtl -Server $serveurcrditedmtl 
# Add Member to Domain Local Group in CCSMTL
$accountGroupcrditedmtl = Get-ADGroup -Identity $GlobalGroupName -Server $serveurcrditedmtl
Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupcrditedmtl -Server $CCSMTLServer
#endregion

#region Create Domain Global Group in Domain DSPMTL
$pathdspmtl="OU=CCSMTL,OU=Groupes,DC=dspmtl,DC=local,DC=lab"
$serveurdspmtl="tcrsvr1528.dspmtl.local.lab"
New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathdspmtl -Server $serveurdspmtl 
# Add Member to Domain Local Group in CCSMTL
$accountGroupdspmtl = Get-ADGroup -Identity $GlobalGroupName -Server $serveurdspmtl
Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupdspmtl -Server $CCSMTLServer
#endregion

#region Create Domain Global Group in Domain JEANNE-MANCE
$pathjma="OU=Groupes DFS,OU=CIUSSS CSMTL,DC=jeanne-mance,DC=local,DC=lab"
$serveurjma="TCRSVR1527.jeanne-mance.local.lab"
New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathjma -Server $serveurjma 
# Add Member to Domain Local Group in CCSMTL
$accountGroupjma = Get-ADGroup -Identity $GlobalGroupName -Server $serveurjma
Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupjma -Server $CCSMTLServer
#endregion

#region Create Domain Global Group in Domain IUGM
$pathiugm="OU=CCSMTL,OU=GROUPES,OU=CIUSSS,DC=iugm-06,DC=local,DC=lab"
$serveuriugm="tcrsvr1814.iugm-06.local.lab"
New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathiugm -Server $serveuriugm 
# Add Member to Domain Local Group in CCSMTL
$accountGroupiugm = Get-ADGroup -Identity $GlobalGroupName -Server $serveuriugm
Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupiugm -Server $CCSMTLServer
#endregion

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Change everything you have hard coded into variables
There is a lot of duplicated code  use a function instead
please use the CODE  block for your code
use single quotes for non-expanding strings.

function Set-GlobalGroups
{
  <#
      .SYNOPSIS
      Short Description
      .DESCRIPTION
      Detailed Description
      .EXAMPLE
      Global-Groups
      explains how to use the command
      can be multiple lines
      .EXAMPLE
      Set-GlobalGroups
      another example
      can have as many examples as you like
  #>
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory=$false, Position=0)]
    [System.String]
    $LocalGroupName = 'DL-ACL-M-OCTOPUS-Shares-DAL',
    
    [Parameter(Mandatory=$false, Position=1)]
    [System.String]
    $GlobalGroupName = "GG-ACL-M-OCTOPUS-Shares-DAL",
    
    [Parameter(Mandatory=$false, Position=2)]
    [Object]
    $CCSMTLServer = ('{0}.ccsmtl.local.lab' -f $CCSMTLServer),
    
    [Parameter(Mandatory=$false, Position=3)]
    [System.String]
    $Pathccsmtl = "OU=ACL,OU=Groupes,DC=CCSMTL,DC=local,DC=lab"
  )
  
  # Creation of many Domain Global group to be nest in one Domain Local Group
  
  #regionCreate Domain Local Group in Domain CCSMTL
    
    New-ADGroup -Name $LocalGroupName -GroupScope DomainLocal -Path $Pathccsmtl -Server $CCSMTLServer
  #endregion
  
  #region Create Domain Global Group in Domain CRLB
    $pathcrlb="OU=CCSMTL,OU=CRLB - CIUSSS,DC=lucie-bruneau,DC=local,DC=lab"
    $serveurcrlb="S-ADS039.lucie-bruneau.local.lab"
    New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathcrlb -Server $serveurcrlb
    # Add Member to Domain Local Group in CCSMTL
    $accountGroupcrlb = Get-ADGroup -Identity $GlobalGroupName -Server $serveurcrlb
    Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupcrlb -Server $CCSMTLServer
  #endregion
  
  #region Create Domain Global Group in Domain IRD
    $pathird="OU=CCSMTL,OU=IRD_CIUSSS,DC=ird,DC=local,DC=lab"
    $serveurird="irdsvr0045.ird.local.lab"
    New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathird -Server $serveurird
    # Add Member to Domain Local Group in CCSMTL
    $accountGroupird = Get-ADGroup -Identity $GlobalGroupName -Server $serveurird
    Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupird -Server $CCSMTLServer
  #endregion
  
  #region Create Domain Global Group in Domain SOV
    $serveursov= "TCRSVR1773.cssssov.local.lab"
    $pathSOV="OU=CCSMTL,OU=ENTITES,DC=cssssov,DC=local,DC=lab"
    New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathSOV -Server $serveursov
    # Add Member to Domain Local Group in CCSMTL
    $accountGroupsov = Get-ADGroup -Identity $GlobalGroupName -Server $serveursov
    Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupsov -Server $CCSMTLServer
  #endregion
  
  #region Create Domain Global Group in Domain TCR
    $pathtcr="OU=DRI,OU=Groupes,OU=Utilisateurs,DC=tcrmtl,DC=local,DC=lab"
    $serveurtcr="tcrsvr1447.tcrmtl.local.lab"
    New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathtcr -Server $serveurtcr
    $accountGrouptcr = Get-ADGroup -Identity $GlobalGroupName -Server $serveurtcr
    Add-ADGroupMember -Identity $LocalGroupName -Members $accountGrouptcr -Server $CCSMTLServer
  #endregion
  
  #region Create Domain Global Group in Domain CCSMTL
    $Pathccsmtl= "OU=ACL,OU=Groupes,DC=CCSMTL,DC=local,DC=lab"
    New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathccsmtl -Server $CCSMTLServer
    # Add Member to Domain Local Group in CCSMTL
    #
    #variable server $serveurcjmt is possibly undefined
    #
    $accountGroupcjmtl = Get-ADGroup -Identity $GlobalGroupName -Server $serveurcjmtl
    #
    Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupcjmtl -Server $CCSMTLServer
  #endregion
  
  #region Create Domain Global Group in Domain CJMTL
    $pathcjmtl="OU=CCSMTL,dc=cjmtl,DC=local,DC=lab"
    $serveurcjmtl= "ss001w1.cjmtl.local.lab"
    New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathcjmtl -Server $serveurcjmtl
    # Add Member to Domain Local Group in CCSMTL
    $accountGroupcjmtl = Get-ADGroup -Identity $GlobalGroupName -Server $serveurcjmtl
    Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupcjmtl -Server $CCSMTLServer
  #endregion
  
  #region Create Domain Global Group in Domain IRGLM
    $pathirglm="ou=CCSMTL,ou=IRGLM - CIUSSS,dc=irglm,DC=local,DC=lab"
    $serveurirglm="Tcrsvr0398.irglm.local.lab"
    New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathirglm -Server $serveurirglm
    # Add Member to Domain Local Group in CCSMTL
    $accountGroupirglm = Get-ADGroup -Identity $GlobalGroupName -Server $serveurirglm
    Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupirglm -Server $CCSMTLServer
  #endregion
  
  #region Create Domain Global Group in Domain CDC
    $pathcdc="OU=CCSMTL,OU=CIUSSS-CEMTL,dc=cdc,DC=local,DC=lab"
    $serveurcdc= "S-ADS045.cdc.local.lab"
    New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathcdc -Server $serveurcdc
    # Add Member to Domain Local Group  in CCSMTL
    $accountGroupcdc = Get-ADGroup -Identity $GlobalGroupName -Server $serveurcdc
    Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupcdc -Server $CCSMTLServer
  #endregion
  
  #region Create Domain Global Group in Domain CRDITEDMTL
    $pathcrditedmtl="OU=CCSMTL,OU=Organisation,DC=crditedmtl,DC=local,DC=lab"
    $serveurcrditedmtl="Tcrsvr1237.crditedmtl.local.lab"
    New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathcrditedmtl -Server $serveurcrditedmtl
    # Add Member to Domain Local Group in CCSMTL
    $accountGroupcrditedmtl = Get-ADGroup -Identity $GlobalGroupName -Server $serveurcrditedmtl
    Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupcrditedmtl -Server $CCSMTLServer
  #endregion
  
  #region Create Domain Global Group in Domain DSPMTL
    $pathdspmtl="OU=CCSMTL,OU=Groupes,DC=dspmtl,DC=local,DC=lab"
    $serveurdspmtl="tcrsvr1528.dspmtl.local.lab"
    New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathdspmtl -Server $serveurdspmtl
    # Add Member to Domain Local Group in CCSMTL
    $accountGroupdspmtl = Get-ADGroup -Identity $GlobalGroupName -Server $serveurdspmtl
    Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupdspmtl -Server $CCSMTLServer
  #endregion
  
  #region Create Domain Global Group in Domain JEANNE-MANCE
    $pathjma="OU=Groupes DFS,OU=CIUSSS CSMTL,DC=jeanne-mance,DC=local,DC=lab"
    $serveurjma="TCRSVR1527.jeanne-mance.local.lab"
    New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathjma -Server $serveurjma
    # Add Member to Domain Local Group in CCSMTL
    $accountGroupjma = Get-ADGroup -Identity $GlobalGroupName -Server $serveurjma
    Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupjma -Server $CCSMTLServer
  #endregion
  
  #region Create Domain Global Group in Domain IUGM
    $pathiugm="OU=CCSMTL,OU=GROUPES,OU=CIUSSS,DC=iugm-06,DC=local,DC=lab"
    $serveuriugm="tcrsvr1814.iugm-06.local.lab"
    New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathiugm -Server $serveuriugm
    # Add Member to Domain Local Group in CCSMTL
    $accountGroupiugm = Get-ADGroup -Identity $GlobalGroupName -Server $serveuriugm
    Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupiugm -Server $CCSMTLServer
  #endregion
}

Open in new window



BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	"OU=ACL,OU=Groupes,DC=CCSMTL,DC=local,DC=lab"	
BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	"OU=ACL,OU=Groupes,DC=CCSMTL,DC=local,DC=lab"	
BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	Global	
BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	Global	
BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	Global	
BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	Global	
BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	Global	
BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	Global	
BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	Global	
BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	Global	
BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	Global	
BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	Global	
BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	Global	
BestPracticeViolation	1	Redundant String Content (case-sensitive)	3272049c-90a3-44a8-9489-0c28174d80ff	String has been used multiple times in scriptblock and should be replaced by a variable.	Global	

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018
Classic case for the variables coming from an input like csv.
In this case, the csv is embedded into the script as here-string (with a tab indent of 4, which is why it looks kind of ragged here). You could as well save it in an external csv, see the comment at line 64.
Function Set-ADTrustedGroup {
[CmdletBinding()]
Param(
	[String]$LocalGroup,
	[String]$LocalGroupPath,
	[String]$LocalServer,
	[String]$GlobalGroup,
	[PSObject[]]$TrustedGroups
)
	#region Create Domain Local Group in Domain CCSMTL
	Write-Verbose "Using local resource group '$($LocalGroup)' ..."
	$resourceGroup = Get-ADGroup -Filter "Name -eq '$($LocalGroup)'" -Server $LocalServer -ErrorAction Stop
	If (-not $resourceGroup) {
		Write-Verbose "    ... not found, creating ..."
		$resourceGroup = New-ADGroup -Name $LocalGroup -GroupScope DomainLocal -Path $LocalGroupPath -Server $LocalServer -PassThru -ErrorAction Stop
		Write-Verbose "... OK, created"
	}
	#endregion
	
	#region Create Domain Global Groups
	ForEach ($trustedGroup in $TrustedGroups) {
		If (-not $trustedGroup.Server) {
			$trustedGroup.Server = $LocalServer
		}
		Try {
			Write-Verbose "Adding global role group in domain '$($trustedGroup.Domain)' ..."
			$accountGroup = Get-ADGroup -Filter "Name -eq '$($GlobalGroup)'" -Server $trustedGroup.Server
			If (-not $accountGroup) {
				Write-Verbose "    ... not found, creating ..."
				$accountGroup = New-ADGroup -Name $GlobalGroup -GroupScope Global -Path $trustedGroup.Path -Server $trustedGroup.Server -PassThru
				Write-Verbose "... OK, account group created"
			}
			# Add Member to Domain Local Group in CCSMTL
			Add-ADGroupMember -Identity $resourceGroup -Members $accountGroup -Server $LocalServer
			Write-Verbose "Account group successfully added to resource group."
		} Catch {
			$PSCmdlet.WriteError($_)
		}
	}
	#endregion
}

$trustedGroups = @"
	"Domain",		"Server",								"Path"
	"CRLB",			"S-ADS039.lucie-bruneau.local.lab",		"OU=CCSMTL,OU=CRLB - CIUSSS,DC=lucie-bruneau,DC=local,DC=lab"
	"IRD",			"irdsvr0045.ird.local.lab",				"OU=CCSMTL,OU=IRD_CIUSSS,DC=ird,DC=local,DC=lab"
	"SOV",			"TCRSVR1773.cssssov.local.lab",			"OU=CCSMTL,OU=ENTITES,DC=cssssov,DC=local,DC=lab"
	"TCR",			"tcrsvr1447.tcrmtl.local.lab",			"OU=DRI,OU=Groupes,OU=Utilisateurs,DC=tcrmtl,DC=local,DC=lab"
	"CCSMTL",		"",										"OU=ACL,OU=Groupes,DC=CCSMTL,DC=local,DC=lab"
	"CJMTL",		"ss001w1.cjmtl.local.lab",				"OU=CCSMTL,dc=cjmtl,DC=local,DC=lab"
	"IRGLM",		"Tcrsvr0398.irglm.local.lab",			"ou=CCSMTL,ou=IRGLM - CIUSSS,dc=irglm,DC=local,DC=lab"
	"CDC",			"S-ADS045.cdc.local.lab",				"OU=CCSMTL,OU=CIUSSS-CEMTL,dc=cdc,DC=local,DC=lab"
	"CRDITEDMTL",	"Tcrsvr1237.crditedmtl.local.lab",		"OU=CCSMTL,OU=Organisation,DC=crditedmtl,DC=local,DC=lab"
	"DSPMTL",		"tcrsvr1528.dspmtl.local.lab",			"OU=CCSMTL,OU=Groupes,DC=dspmtl,DC=local,DC=lab"
	"JEANNE-MANCE",	"TCRSVR1527.jeanne-mance.local.lab",	"OU=Groupes DFS,OU=CIUSSS CSMTL,DC=jeanne-mance,DC=local,DC=lab"
	"IUGM",			"tcrsvr1814.iugm-06.local.lab",			"OU=CCSMTL,OU=GROUPES,OU=CIUSSS,DC=iugm-06,DC=local,DC=lab"		
"@ | ConvertFrom-Csv

$splat = @{
	LocalGroup = 'DL-ACL-M-OCTOPUS-Shares-DAL'
	LocalGroupPath = 'OU=ACL,OU=Groupes,DC=CCSMTL,DC=local,DC=lab'
	LocalServer = "$($CCSMTLServer).ccsmtl.local.lab"
	GlobalGroup = 'GG-ACL-M-OCTOPUS-Shares-DAL'
	## When using an external csv, the next line would need to be: TrustedGroups = Import-Csv -Path C:\Temp\TrustedGroup.csv
	TrustedGroups = $trustedGroups
}
Set-ADTrustedGroup @splat -Verbose

Open in new window

Note that with all that copy and pasting, you had an error with the CCSMTL domain (lines 51 and 52), where you actually created the group in the CJMTL domain.
Steve BonaInformation Technology Specialist

Author

Commented:
I check lines 51 and 52 all informations are correct, so the global group will be create in IRGLM and CDC domain.
But i have error with the script

Set-ADTrustedGroup @splat -Verbose
VERBOSE: Using local resource group 'DL-APP-L-GCMI' ...
VERBOSE:     ... not found, creating ...
New-ADGroup : Directory object not found
At line:15 char:20
+ ... urceGroup = New-ADGroup -Name $LocalGroup -GroupScope DomainLocal -Pa ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (CN=DL-APP-L-GCM...tss,DC=qc,DC=ca:String) [New-ADGroup], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.NewADGroup
 
VERBOSE: ... OK, created
VERBOSE: Adding global role group in domain 'CRLB' ...
Set-ADTrustedGroup : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At line:67 char:1
+ Set-ADTrustedGroup @splat -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADTrustedGroup], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Set-ADTrustedGroup
Most Valuable Expert 2018
Distinguished Expert 2018
The "Directory object not found" means that the LocalGroupPath (with the OU where the group should be created) is incorrect or does not exist.
The other one is resulting from the first one.

I check lines 51 and 52 all informations are correct, so the global group will be create in IRGLM and CDC domain.
And that (among other obvious reasons) is why you should post code in [code]...[/code] tags. You looked up the lines in your version of the script, which differs from what you posted here (which is why I explicitly mentioned CCSMTL to give you a context).
Anyway, this is the full region in question. The marked server variable is not defined at this point (only in the next region for CJMTL), and is taken from another domain, not CCSMTL.

#region Create Domain Global Group in Domain CCSMTL
$Pathccsmtl= "OU=ACL,OU=Groupes,DC=CCSMTL,DC=local,DC=lab"
New-ADGroup -Name $GlobalGroupName -GroupScope Global -Path $pathccsmtl -Server $CCSMTLServer
# Add Member to Domain Local Group in CCSMTL
$accountGroupcjmtl = Get-ADGroup -Identity $GlobalGroupName -Server $serveurcjmtl
Add-ADGroupMember -Identity $LocalGroupName -Members $accountGroupcjmtl -Server $CCSMTLServer
#endregion
Steve BonaInformation Technology Specialist

Author

Commented:
Your were right I did not look at my original script for the error.
I correct the OU for the script and now, it works fine !!!!
Thanks you for help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial