troubleshooting Question

Compile multiple same line command to make a good script

Avatar of Steve Bona
Steve BonaFlag for Canada asked on
PowershellActive Directory
5 Comments2 Solutions79 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros