Link to home
Start Free TrialLog in
Avatar of Ben Hart
Ben HartFlag for United States of America

asked on

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?
Avatar of Ben Hart
Ben Hart
Flag of United States of America image

ASKER

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...
Avatar of sirbounty
Try this:
$DomainProxyAddress = "$($GivenName.substring(0,1))$($Surname)"
Not sure this line would format an address like samaccountname-domain-com@domain.mail.onmicrosoft.com
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?)
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
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"
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's not our domain per say, but a secondary proxy address for Office365.
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?
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"
The proxy domain is a static entry in the script.  It wouldnt be 'pulled' from anywhere like AD.
Yes, the above string should do it, if you can get your domain into the $domain variable.
BAM, got it!  With your help I modified your string to:

$DomainProxyAddress = "$($givenname.substring(0,1))$surname-$("mydomain")-$("net")@domain.mail.onmicrosoft.com"
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?)