Petri.com forums Home Forums Start Page Forums Frequently Asked Questions FAQ Member List Members List
Go Back   Petri IT Knowledgebase Forums > Windows Scripting > PowerShell
Petri.com is happy to award RicklesP the title of Most Valuable Member !!!
Register Calendar Calendar Search Petri IT Knowledgebase Forums Search Todays Posts Today's Posts Mark Forums Read
Notices

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

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

this thread has 1 replies and has been viewed 1662 times

Closed Thread
 
Thread Tools Search this Thread Display Modes
  #1  
Old 2nd December 2011, 08:29
Ryzz Ryzz is offline
Casual
Casual
 
 Join Date: Mar 2009
  6 month star 12 month star
 Posts: 31
 Reputation: Ryzz is on a distinguished road (19)
Default Help with Variables surrounded by underscore adding whitespace

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 at 08:44..
  #2  
Old 6th December 2011, 21:01
Rems's Avatar
Rems Rems is offline
Moderator
 
 Join Date: Mar 2005
  6 month star 12 month star
 Location: NL
 Posts: 2,434
 Reputation: Rems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to behold (965)
Default 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
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: input data to a string/remove the string leaving only the data using batch file Slow DOS Command Shell 1 23rd September 2011 14:55
Bulk Adding domains to address space tab for SMTP Connector sgershman Exchange 2000 / 2003 3 16th March 2010 07:57
Adding multiple bluetooth printers to multiple laptops taskhill Windows 2000 Pro, XP Pro 0 10th September 2008 01:59
Moving multiple mailboxes used all my transaction log space - workaround? HeroHari Exchange 2000 / 2003 3 7th August 2008 14:41
adding to path enviroment variables Maxwell Shivers Active Directory 17 15th August 2007 20:49


All times are GMT +3. The time now is 08:20.

Steel Blue 3.5.4 vBulletin Style ©2006 vBEnhanced
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
 

Valid XHTML 1.0!   Valid CSS!

Copyright 2005 Daniel Petri