Ben Hart
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:
$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@do main.mail. onmicrosof t.com
I do not know the correct syntax to create this string... Help?
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
$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@do
I do not know the correct syntax to create this string... Help?
Try this:
$DomainProxyAddress = "$($GivenName.substring(0, 1))$($Surn ame)"
$DomainProxyAddress = "$($GivenName.substring(0,
ASKER
Not sure this line would format an address like samaccountname-domain-com@ domain.mai l.onmicros oft.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.c om (presumably this is mostly hard-set?)
$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.c
ASKER
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
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))$surnam e-$($($(ge t-addomain ).dnsroot) .replace(' .','-'))@$ ($($(get-a ddomain).d nsroot).re place('.co m','')).ma il.onmicro soft.com"
Though I don't know that I quite fully understand your setup enough to know if it will work for you:
"$($givenname.substring(0,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
Where would you gather the domain information and would it be the same for both instances of 'domain' above?
ASKER
So if my proxy domain is Widgets.mail.onmicrosoft.c om then judging by your string above I could change it to:
"$($givenname.substring(0, 1))$surnam e-$($domai n.replace( '.','-'))@ widgets.ma il.onmicro soft.com"
"$($givenname.substring(0,
ASKER
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.
ASKER
BAM, got it! With your help I modified your string to:
$DomainProxyAddress = "$($givenname.substring(0, 1))$surnam e-$("mydom ain")-$("n et")@domai n.mail.onm icrosoft.c om"
$DomainProxyAddress = "$($givenname.substring(0,
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))$surnam e-mydomain -net@domai n.mail.onm icrosoft.c om"
(of course, that last 'domain' isn't accounted for yet?)
$DomainProxyAddress = "$($givenname.substring(0,
(of course, that last 'domain' isn't accounted for yet?)
ASKER
Open in new window
So now I just need to determine how to format it in the way I explained above...