Announcement

Collapse
No announcement yet.

Help with Multiple Variables in String (PS adding in a space)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with Multiple Variables in String (PS adding in a space)

    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.
    Last edited by Ryzz; 2nd December 2011, 06:44.

  • #2
    Re: Help with Multiple Variables in String (PS adding in a space)

    String concatenation with a combination of text and variables you should use curly braces 'round names of the variables. Like,

    Code:
    Write-Host "ADMA${BU}_AdminOn${ServerType}"
    /Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment

    Working...
    X