Problem with filling out textbox

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
alwo23@hotmail.com
Posts: 28
Last visit: Tue Jun 06, 2023 2:43 am

Problem with filling out textbox

Post by alwo23@hotmail.com »

Hello everybody,
i wrote a script that read the smtp address from a user and take it in a richtextbox. In a test script all is running fine, when i doit in my main script the textbox is not filled out.

Here is the function to read the smtp address:

function Load-SMTPAdresses
{
$strname = (Get-ADUser -Filter { DisplayName -eq $txtbox_search.Text } -properties sAmAccountName)."sAmAccountName"
$strFilter = "(&(objectCategory=User)(samAccountName=$strName))"
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.Filter = $strFilter
$objPath = $objSearcher.FindOne()
$objUser = $objPath.GetDirectoryEntry()

$a = $objUser.msExchMailboxGuid
if ($a.count -eq 1)
{
$chbox_mailena.Checked = $true
$smtp1 = Get-Mailbox $strName | select -ExpandProperty EmailAddresses | ?{ $_.IsPrimaryAddress -eq $true }
$smtp2 = @(Get-Mailbox $strName | select -ExpandProperty EmailAddresses | ?{ $_.IsPrimaryAddress -eq $False })

foreach ($item in $smtp2.SmtpAddress)
{
$item = $item -split ' '
$txtbox_2ndmail.AppendText("$($item) `n")
Write-Host $item
}
$txtbox_exmail.Text = $smtp1.SmtpAddress
$rtxtbox_status.Text = "User has mailbox"
}
else
{
$rtxtbox_status.Text = "User has no mailbox Nope"
}
}

The textbox $txtbox_2ndmail is a multiline textbox and the $txtbox_exmail is a normal textbox.

Thx for help

Greets
Alexander
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem with filling out textbox

Post by jvierra »

Cannot tell what you are trying to do. What mail address are you trying to get? Most of the code you have posted does nothing. I suspect you copied it from somewhere but what it is supposed to do us rather vague.
User avatar
alwo23@hotmail.com
Posts: 28
Last visit: Tue Jun 06, 2023 2:43 am

Re: Problem with filling out textbox

Post by alwo23@hotmail.com »

Ok, i wrote a program for an customer to edit ad users. This ad users are in exchange 2013 enabled. This user has the primary email address:
$smtp1 = Get-Mailbox $strName | select -ExpandProperty EmailAddresses | ?{ $_.IsPrimaryAddress -eq $true }

and multiple other smtp email addresses. Now i will get this smtp address from the user object i do this with:

$smtp2 = @(Get-Mailbox $strName | select -ExpandProperty EmailAddresses | ?{ $_.IsPrimaryAddress -eq $False })

Now i will show this email addresses in an richtextbox for edit. Nothing more.

The query $smtp1 and $smtp2 is working fine in the exchange powershell but not working in the form.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem with filling out textbox

Post by jvierra »

If I try to guess at what you are trying to do this is what I see:
  1. function Load-SMTPAdresses {
  2.    
  3.     $user=Get-ADUser -Filter { DisplayName -eq $txtbox_search.Text } -properties mail
  4.     if($user.mail){
  5.         $chbox_mailena.Checked = $true
  6.         $txtbox_exmail.Text = $user.mail
  7.         $rtxtbox_status.Text = "User has mailbox"
  8.     }else{
  9.         $rtxtbox_status.Text = "User has no mailbox Nope"    
  10.     }
  11. }
The above shows how to detect a mail address,
User avatar
alwo23@hotmail.com
Posts: 28
Last visit: Tue Jun 06, 2023 2:43 am

Re: Problem with filling out textbox

Post by alwo23@hotmail.com »

The problem is a user without an Exchange Mailbox has also an mail Attribute. Only the attribute mailboxguid is on the user which has an mailbox. And the other problem is the other emailaddresses are only in the exchange attribute Emailaddresses and this one is an multivalue field.

I take this with: Get-Mailbox $strName | select -ExpandProperty EmailAddresses | ?{ $_.IsPrimaryAddress -eq $true }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem with filling out textbox

Post by jvierra »

Why? Just use the mail in the user object. It is the primary address.
User avatar
alwo23@hotmail.com
Posts: 28
Last visit: Tue Jun 06, 2023 2:43 am

Re: Problem with filling out textbox

Post by alwo23@hotmail.com »

I need all addresses the primary and the second, third and so on.

Sorry but this is was the customer need.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem with filling out textbox

Post by jvierra »

That makes no sense. If the customer ask for the wrong thing are you going to give it to them.

The first smtp address in the array is NOT the primary address. It is not any specific address. There is no way to predict which address is first.

The primary address is either the one with a capital SMTP which is the one set as WindowsEmailAddress.

We can get this like this:
(Get-Mailbox userid).WindowsEmailAddress

Or
(Get-Mailbox userid().PrimaryEmailAddress

Using the "DisplayName" to get a user is probably not a good idea because it is not unique and may not match what is typed.

We can do that like this:
Get-Mailbox -Anr $displayname

So you can see that most of your code is redundant and will not likely work correctly.

Even the array can be accessed easily:

((get-mailbox -Anr '$displayname).Emailaddresses|?{$_.IsPrimaryAddress}).SmtpAddress

Which wil; always be the same as this:
(Get-Mailbox userid).PrimaryEmailAddress


Take some time to analyze the objects or look them up in the documentation.
User avatar
alwo23@hotmail.com
Posts: 28
Last visit: Tue Jun 06, 2023 2:43 am

Re: Problem with filling out textbox

Post by alwo23@hotmail.com »

Yes thats correct i ask for the attribute IsPrimarySMTPAddress.

SmtpAddress : group22@contoso.com
AddressString : group22@contoso.com
ProxyAddressString : smtp:group22@contoso.com
Prefix : SMTP
IsPrimaryAddress : False
PrefixString : smtp

Is that $True than i read it out and put it in the textbox. But this is the problem i can not see the informationen in the textbox.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem with filling out textbox

Post by jvierra »

Since you seem to be skeptical about how Exchange works then look at the following:


[PS] C:\scripts>(get-mailbox -Anr 'James Vierra').Emailaddresses


SmtpAddress : jjsmith@testnet.local
AddressString : jjsmith@testnet.local
ProxyAddressString : smtp:jjsmith@testnet.local
Prefix : SMTP
IsPrimaryAddress : False
PrefixString : smtp

SmtpAddress : jjsmith@testnet.local
AddressString : jjsmith@testnet.local
ProxyAddressString : smtp:jjsmith@testnet.local
Prefix : SMTP
IsPrimaryAddress : False
PrefixString : smtp

SmtpAddress : jjsmith@goobers.com
AddressString : jjsmith@goobers.com
ProxyAddressString : SMTP:jjsmith@goobers.com
Prefix : SMTP
IsPrimaryAddress : True
PrefixString : SMTP


Notice that it is an array of objects which are [Microsoft.Exchange.Data.SmtpProxyAddress] types and not strings.

Also note that the "primary" address in this collection is the last element and not the first. It also is tagged as "IsPrimaryAddress" as True.

So no matter what your customer is asking for you cannot give it to them. What is asked for does not really exits.

I think you are thinking of the proxyaddresses array of AD.

PS C:\scripts> get-aduser jvierra -prop proxyaddresses|select -exp proxyaddresses
smtp:jeff@testnet.local
smtp:jsmith@testnet.local
SMTP:jsmith@goobers.com
PS C:\scripts>


Notice that the fist address is not the primary and the primary is marked by capital SMTP:.

I know all of this is difficult to understand but you can get it over time. I learned by a certification course and about 20 years of working with Exchange, Much is not intuitive and most of the threads in forums are either wrong or misleading.
This topic is 8 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked