Solved

Powershell Help needed

Posted on 2014-11-04
7
154 Views
Last Modified: 2014-12-08
Long story short, I am trying to create a script that will perform the following:

accept an OU name
a surname
a givenname
formulate a proxy address in the following format $givenname.substring(0,1)+$surname +"domain.mail.com"
If the proxy address already exists teh script need to tell me so and allow me to enter a different $givenname.substring(0,1)+$surname portion
Then either after I enter a valid proxyaddress create a new ad user object in the OU specified with the proxyaddress specified
Then return that new user accounts info including the proxy address for verification


What I have so far works except for the proxy verification and redirection 'if exists'.. well also I cannot find the right parameter that recognizes proxyaddress.  I tried -properties but the cmdlet doesnt not like that with either set-aduser or new-aduser.

$theOU = read-host "enter OU name"
$Surname = read-Host "enter surname"
$GivenName = read-host "Enter first name"
$DisplayName = "$Surname, $GivenName"
$Password = "P@$$WORD1"
$name = $GivenName.substring(0,1)+$Surname
$proxyaddress = read-host "Enter the proxy address"
#$txtBoxTelephone = read-host "Enter phone #"

#$txtBoxDescription = read-host "Enter descr"
Import-Module activedirectory
import-module servermanager

	
	$myOU = Get-AdOrganizationalUnit -Filter "Name -eq '$theOU'" -Searchbase 'OU=People,DC=DIFC,DC=Root01,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	
get-aduser $name

Open in new window

0
Comment
Question by:Ben Hart
  • 4
  • 2
7 Comments
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40424140
If you are planning to add a single email address I recommend you to use emailaddress attribute as proxyaddresses attribute is multivalued.

set-aduser $name -properties emailaddress $proxyaddress

Also, if your objective is to create a Exchange mailbox then you may use New-Mailbox command.
0
 
LVL 14

Author Comment

by:Ben Hart
ID: 40424994
Nope.. we are not creating Exchange mailboxes here. The proxyaddress attribute is required for Office365 via a parent company.

The emailaddress value will be populated by mailbox creation in the cloud.. I specifically need an additional proxyaddress value, to append rather than replace.
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40425055
From the lines of script you are creating a new AD account and adding proxy address. I dont get how you can append something when you are creating brand new.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 14

Author Comment

by:Ben Hart
ID: 40426021
First off I'm sorry for not explaining correctly here.  You are right the proxyaddresses field is empty for a new user.  Basically the only help I need is what was in my original post.  I am unaware of the correct syntax for the If the proxy address already exists the script need to tell me so and allow me to enter a different $givenname.substring(0,1)+$surname portion part.  I know that in creating a domain user account if the name is already in use an error will occur but I don;t want to wait for the script to fail.  I want it to give me the option of going back to the name steps to change them as per our naming conventions and allowing me to add a 1 to the end. (or a 2, or 3 whatever the case may be)
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40432536
You've messed up the code snippet with different approaches, so we'll first have to perform some cleanup.
Be careful when using $ inside of strings - you should enclose literal strings (not needing variable or subexpression substitution) like your password in ticks instead of quotes.

ProxyAddresses contains the message type (SMTP:, SIP:, ...) as prefix, and you need the FQDN, that is why you can't find duplicates. You'll have to add that when comparing. Better, you can use the full address in a AD filter expression.

Note that I'm focussing on the proxy address only - do you require a check for the user name instead? (Sounds more logical.)
$theOU        = read-host "enter OU name"
$Surname      = read-Host "enter surname"
$GivenName    = read-host "Enter first name"
$DisplayName  = "$Surname, $GivenName"
$Password     = 'P@$$WORD1'
$name         = $GivenName.substring(0,1)+$Surname
$domain       = 'domain.mail.com'


$proxyaddress = read-host "Enter the proxy address"

#$txtBoxTelephone = read-host "Enter phone #"
#$txtBoxDescription = read-host "Enter descr"

Import-Module activedirectory
Import-Module servermanager

$myOU = Get-AdOrganizationalUnit -Filter "Name -eq '$theOU'" -Searchbase 'OU=People,DC=DIFC,DC=Root01,DC=org'

# get all inexact matches
$inUse = Get-ADUser -Filter " ProxyAddresses -like ""SMTP:$proxyaddress*@$domain"" " -Properties ProxyAddresses | select -Expand Proxy*

# check for exact match only, else don't bother!
while ($inUse -contains "SMTP:$proxaddress@$domain")
{
  Write-Host ("The following proxy addresses exist already:`n" + ($inUse -join "`n"))
  $proxyaddress = read-host "Enter new name prefix"
}

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 "SMTP:$proxyaddress@$domain"
get-aduser $name

Open in new window

0
 
LVL 14

Author Comment

by:Ben Hart
ID: 40486775
Thank you Qlemo... I thank you for your help towards this, really I do appreciate it.  Apparently we've decided to use a script provided by another entity so mine is not needed at this time. :(

My latency in completing this was compounded due to a vacation week and then a training week that I had forgotten about.
0
 
LVL 14

Author Closing Comment

by:Ben Hart
ID: 40486778
Thanks for the help.. even though I am not able to continue towards a full completion.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Synchronize a new Active Directory domain with an existing Office 365 tenant
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question