Powershell Script Help request: If found, else

Script to create a new domain user.. I have a part that checks for the existence of an email address.

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

}

Open in new window


If I input a proxyaddress that does not already exist I get the write-host saying "ProxyAddress Not Found".  However if it does exist then I get an error:

get-aduser : One or more properties are invalid.
Parameter name: jmanwell@domain.net
At D:\Users\bhart.DIFC\Dropbox\Scripts\Create_New_AD_user_O365.ps1:36 char:1
+ get-aduser -filter * -properties $proxyaddress
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser


I'd obviously like for this part to say the address already exists and re-prompt me to input a correct one.  Back in the day that'd be a goto line whatever, but I doubt that works in PS lol.
LVL 14
Ben HartAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The first line and the following 4 are the same ;-).
You want to run an endless loop until the Proxy Address is unique. Your code is not telling that.
while (Get-ADuser  -filter * -Properties ProxyAddresses|?{$_.proxyaddresses -contains $proxyaddress})
{
  $proxyaddress = read-host "$proxyaddress is already in use, please try another one"
}
Write-Host "$proxyaddress is not used yet."

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ben HartAuthor Commented:
So this would replace the entire segment I listed originally?
Ben HartAuthor Commented:
Nevermind.. works like a champ by just replacing the part:

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

}

Open in new window

SD-WAN: Making It Work for You

As bandwidth requirements and Internet costs grow, businesses naturally want to manage budgets by reducing reliance on their most expensive connection types. Learn more about how to make SD-WAN work for your business in our on-demand webinar!

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Replace all posted content, please.
Ben HartAuthor Commented:
Really?  This is what I have now and it works like a champ:

Get-ADuser  -filter * -Properties ProxyAddresses|?{$_.proxyaddresses -contains $proxyaddress}
      $found=Get-ADuser  -filter * -Properties ProxyAddresses|
     Where-Object{
        $_.proxyaddresses |
             Where-Object{ $_ -eq $ProxyAddress }}

while (Get-ADuser  -filter * -Properties ProxyAddresses|?{$_.proxyaddresses -contains $proxyaddress})
{
  $proxyaddress = read-host "$proxyaddress is already in use, please try another one"
}
Write-Host "$proxyaddress is not used yet."

I will comment out the top part that was in my original and give it a test.
Ben HartAuthor Commented:
And it still works.  interesting but thanks for letting me know.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
With the partitially replaced code you are doing the AD query 3 times. Only once is sufficient :D.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.