Solved

Powershell Help needed

Posted on 2014-11-04
7
147 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 14

Author Comment

by:Ben Hart
Comment Utility
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 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the help.. even though I am not able to continue towards a full completion.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
A procedure for exporting installed hotfix details of remote computers using powershell
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now