Solved

Powershell Help needed

Posted on 2014-11-04
7
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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 70

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A brief introduction to what I consider to be the best editor for PowerShell.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

627 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