Powershell script help - How to combine values

Long story short, I'm crafting a script to create a new domain user.. part of my script adds values like Description, and proxy address.. but to help us add the correct proxyaddress that will work with our O365 tenant I need to combine teh username with a custom string and I'm hitting a brick wall with the syntax.

Here's what I have:

$theOU = read-host "Enter the OU name"
$Surname = read-Host "Enter the surname"
$GivenName = read-host "Enter first name"
$DisplayName = "$Surname, $GivenName"
$Password = "December1"
$name = $GivenName.substring(0,1)+$Surname
$proxyaddress = read-host "Enter the proxy address in full"
#$txtBoxTelephone = read-host "Enter phone #"
$DomainProxyAddress = $GivenName.substring(0,1)+$Surname <------------------------------------
$txtBoxDescription = read-host "Enter persons description"
Import-Module activedirectory
import-module servermanager

	
	$myOU = Get-AdOrganizationalUnit -Filter "Name -eq '$theOU'" -Searchbase 'OU=People,DC=domain,DC=org'
	
	
Get-ADuser  -filter * -Properties ProxyAddresses|?{$_.proxyaddresses -contains $proxyaddress}
	$found=Get-ADuser  -filter * -Properties ProxyAddresses|
     Where-Object{
        $_.proxyaddresses | 
             Where-Object{ $_ -eq $ProxyAddress }}

if($found){
	write-host "ProxyAddress Exists, Change username to something unique!"
get-aduser -filter * -properties $proxyaddress
pause 5
}
	else {
	write-host "ProxyAddress Not Found!"
Pause 5

}


write-host	$myou
write-host $name
New-ADUser -path $myOU -samaccountname $name -name $displayname -DisplayName $DisplayName -Surname $Surname -givenname $givenname -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -force) -enabled:$false
set-aduser $name -properties proxyaddresses $proxyaddress
set-aduser $name -properties proxyaddresses 	
get-aduser $name

Open in new window



$DomainProxyAddress is the line I'm editing currently.  I basically need a secondary proxy address like:

Tester, Adam
atester
SMTP:atester@domain.com
smtp:atester-domain-com@domain.mail.onmicrosoft.com

I do not know the correct syntax to create this string... Help?
LVL 14
Ben HartAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ben HartAuthor Commented:
Ok I have to correct myself a little here..  The Correct syntax for adding the proxyaddress is:

set-aduser $name -add @{proxyaddresses = "$proxyaddress"}

Open in new window


So now I just need to determine how to format it in the way I explained above...
0
sirbountyCommented:
Try this:
$DomainProxyAddress = "$($GivenName.substring(0,1))$($Surname)"
0
Ben HartAuthor Commented:
Not sure this line would format an address like samaccountname-domain-com@domain.mail.onmicrosoft.com
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

sirbountyCommented:
Perhaps I misunderstood.
$givenname.substring(0,1) gives you the first bit,
$surname gives you the latter part.
Where are you obtaining domain-com and @domain.mail.onmicrosoft.com (presumably this is mostly hard-set?)
0
Ben HartAuthor Commented:
Getting the first initial and last name or in my case the SAM i can already do.  It's tying that into the @domain.mail.blah blah that's throwing me off.

Yeah It'd be a static variable so probably defined as another $proxyaddress
0
sirbountyCommented:
Sort of convaluted, but this retrieves the info from my domain.
Though I don't know that I quite fully understand your setup enough to know if it will work for you:

"$($givenname.substring(0,1))$surname-$($($(get-addomain).dnsroot).replace('.','-'))@$($($(get-addomain).dnsroot).replace('.com','')).mail.onmicrosoft.com"
0
sirbountyCommented:
Or, if you want to drop that into a variable,
[string]$domain = (get-addomain).dnsroot

"$($givenname.substring(0,1))$surname-$($domain.replace('.','-'))@$($domain.replace('.com','')).mail.onmicrosoft.com"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ben HartAuthor Commented:
That's not our domain per say, but a secondary proxy address for Office365.
0
sirbountyCommented:
Then I'm afraid I don't quite understand.
Where would you gather the domain information and would it be the same for both instances of 'domain' above?
0
Ben HartAuthor Commented:
So if my proxy domain is Widgets.mail.onmicrosoft.com then judging by your string above I could change it to:

"$($givenname.substring(0,1))$surname-$($domain.replace('.','-'))@widgets.mail.onmicrosoft.com"
0
Ben HartAuthor Commented:
The proxy domain is a static entry in the script.  It wouldnt be 'pulled' from anywhere like AD.
0
sirbountyCommented:
Yes, the above string should do it, if you can get your domain into the $domain variable.
0
Ben HartAuthor Commented:
BAM, got it!  With your help I modified your string to:

$DomainProxyAddress = "$($givenname.substring(0,1))$surname-$("mydomain")-$("net")@domain.mail.onmicrosoft.com"
0
sirbountyCommented:
Only the methods need to be delayed by enclosing them in $(), you shouldn't have to do that with the string - use this instead:

$DomainProxyAddress = "$($givenname.substring(0,1))$surname-mydomain-net@domain.mail.onmicrosoft.com"

(of course, that last 'domain' isn't accounted for yet?)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.