Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenat two variable with a space $x + " " + $y

Status
Not open for further replies.

woter324

Technical User
Jan 26, 2007
179
GB
Hi All,

Realy simple, I am sure, but it's so simple I can't find the answer.

I have two variable and need to add a space in the middle. How?

Everything I've read would suggest the following should work:
Code:
$x + " " + $y

The actual code looks like:
Code:
param($fn, $ln)

New-Mailbox -Name $fn + " " + $ln `....

I've even tried:

Code:
$fn + [char]32 + $ln

If anyone could point me in the right direction, I'd be very grateful.

I know I am going to kick myself when it works. Grrr!!

Thanks
 
`' is the powershell space

Concatenation is done via
$C = $A += $B
 
Hi theravager,

Thanks for your reply. I'm not quite sure what you mean. I have tried several combinatons of your answer, however, I cannot get it to work.

If tried:
$y + `' '` + $x
$y `' $x
$y + = $x

Any chance of a pointer to what I am doing wrong?

Many thanks
 
Most likely you can't do the concatenation directly to the -Name parameter as your code tries. Simplest thing would probably be:
Code:
param($fn, $ln)

$fullname = $fn + " " + $ln

New-Mailbox -Name $fullname `....

There may be other ways to do it, but it just depends on how the -Name parameter accepts its value. You could also try:
string substitution: -Name "$fn $ln"
subexpression -Name $($fn + " " + $ln)
script block: -Name {$fn + " " + $ln} (not likely to work)

I don't have Exchange 2007 to play around with...
 
Hi crobin1,

As you say Exchange doesn't like it.

I've done as you suggested in your code snippet and it works fine. Well from PowerGUI Script Editor. Thank you very much.

If I may ask another question on the back of this one, as I said, it works from PowerGui Script Editor (Free tool from Quest), however when I try it from the command line it fails:

Code:
The term 'param' is not recognized as a cmdlet, function, operable program, or
script file. Verify the term and try again.
At H:\Scripts\PowerShell\CreateUser.ps1:2 char:6
+ param( <<<< $alias, $ou, $fn, $ln, $db)

I'm running the command from my XP box with EMT 2K7 and PS installed.

I've added the following line to the script:
Code:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin


Here is the complete code:

Code:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
param($alias, $ou, $fn, $ln, $db)

$fullname = $fn + " " + $ln

$password = Read-Host "Enter password" -AsSecureString

New-Mailbox -Name $fullname `
			-Alias $alias `
			-OrganizationalUnit $ou `
			-UserPrincipalName $alias"@xxxxxxx.co.uk" `
			-SamAccountName $alias `
			-FirstName $fn `
			-LastName $ln `
			-Password $password `
			-ResetPasswordOnNextLogon $true `
			-Database $db

I guess I have to get this to work as I am going to execute it from a VBScript HTA.

Many thanks

 
param must be the first executable line in your script. Try moving the Add-PSSnapIn command just after the param statement.
 
Hi Crobin1,

Thanks again. Spot on.


Many thanks

Woter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top