Solved

Powershell Help needed

Posted on 2014-11-04
7
152 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 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

8 Experts available now in Live!

Get 1:1 Help Now