Im trying to automate a whole bunch of AD group creation, and for some reason when i put in the variable in the middle "$BU" it inserts a white space before returning the variable.
Code:
$arrServerTypes = @("FileServers","DatabaseServers","TMGServers","SCCMServers","ArchivingServers","CitrixServers")
$BUPath = "OU=Business Unit 2,OU=Asia,dc=MyCo,dc=global"
$Path = "OU=China,OU=Business Unit 2,OU=Asia,dc=MyCo,dc=global"
$CountryCode = "CN"
$BU = "_BU2"
Foreach($ServerType in $arrServerTypes){
Write-Host "ADMA$BU_AdminOn$ServerType"
}
And the output i get is
Code:
ADMA _BU2_AdminOnFileServers
ADMA _BU2_AdminOnDatabaseServers
ADMA _BU2_AdminOnTMGServers
ADMA _BU2_AdminOnSCCMServers
ADMA _BU2_AdminOnArchivingServers
ADMA _BU2_AdminOnCitrixServers
The reason for the write-host was troubleshooting why groups where not being created with the correct names. So ive used write-host to output the string value.
The original script (or loop section) is as follow for anyone interested
Code:
New-ADGroup -Name "ADMA_$CountryCode_RdpTo$ServerType" -SamAccountName "" -GroupCategory Security -GroupScope Global -Path "OU=Restricted Security Groups,OU=Groups,$Path"
New-ADGroup -Name "ADMA_$CountryCode_AdminOn$ServerType" -SamAccountName "" -GroupCategory Security -GroupScope Global -Path "OU=Restricted Security Groups,OU=Groups,$Path"
Add-QADGroupMember -Identity "CN=ADMA_$CountryCode_RdpTo$ServerType,$Path" -Member "CN=ADMA_$BU_RdpTo$ServerType,$BUPath"
Add-QADGroupMember -Identity "CN=ADMA_$CountryCode_AdminOn$ServerType,$Path" -Member "CN=ADMA_$CountryCode_AdminOn$ServerType,$BUPath"
Im open for ideas!!! Thx!
Edit: Further update, the problem seems directly related to having an underscore anyway either side of the variable. Ive search google for ages but cant seem to find out why.