Ben Hart
asked on
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.
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\Dropbo x\Scripts\ Create_New _AD_user_O 365.ps1:36 char:1
+ get-aduser -filter * -properties $proxyaddress
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Syst em.Argumen tException ,Microsoft .ActiveDir ectory.Man agement.Co mmands.Get ADUser
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.
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
}
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\Dropbo
+ get-aduser -filter * -properties $proxyaddress
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Syst
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
}
Replace all posted content, please.
ASKER
Really? This is what I have now and it works like a champ:
Get-ADuser -filter * -Properties ProxyAddresses|?{$_.proxya ddresses -contains $proxyaddress}
$found=Get-ADuser -filter * -Properties ProxyAddresses|
Where-Object{
$_.proxyaddresses |
Where-Object{ $_ -eq $ProxyAddress }}
while (Get-ADuser -filter * -Properties ProxyAddresses|?{$_.proxya ddresses -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.
Get-ADuser -filter * -Properties ProxyAddresses|?{$_.proxya
$found=Get-ADuser -filter * -Properties ProxyAddresses|
Where-Object{
$_.proxyaddresses |
Where-Object{ $_ -eq $ProxyAddress }}
while (Get-ADuser -filter * -Properties ProxyAddresses|?{$_.proxya
{
$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.
ASKER
And it still works. interesting but thanks for letting me know.
With the partitially replaced code you are doing the AD query 3 times. Only once is sufficient :D.
ASKER