Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell Help needed

Posted on 2014-11-04
7
Medium Priority
?
170 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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 71

Accepted Solution

by:
Qlemo earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrā€¦

721 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