Support Forums

Problem with filling out textbox

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
User avatar
alwo23@hotmail.com
Posts: 27
Joined: Sat Sep 28, 2013 1:56 am

Problem with filling out textbox

Postby alwo23@hotmail.com » Thu Jan 07, 2016 6:54 am

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
User avatar
jvierra
Posts: 9699
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Problem with filling out textbox

Postby jvierra » Thu Jan 07, 2016 7:47 am

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: 27
Joined: Sat Sep 28, 2013 1:56 am

Re: Problem with filling out textbox

Postby alwo23@hotmail.com » Thu Jan 07, 2016 7:55 am

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.
User avatar
jvierra
Posts: 9699
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Problem with filling out textbox

Postby jvierra » Thu Jan 07, 2016 7:57 am

If I try to guess at what you are trying to do this is what I see:

function Load-SMTPAdresses {
 
$user=Get-ADUser -Filter { DisplayName -eq $txtbox_search.Text } -properties mail
if($user.mail){
$chbox_mailena.Checked = $true
$txtbox_exmail.Text = $user.mail
$rtxtbox_status.Text = "User has mailbox"
}else{
$rtxtbox_status.Text = "User has no mailbox Nope"
}
}


The above shows how to detect a mail address,
User avatar
alwo23@hotmail.com
Posts: 27
Joined: Sat Sep 28, 2013 1:56 am

Re: Problem with filling out textbox

Postby alwo23@hotmail.com » Thu Jan 07, 2016 8:08 am

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 }
User avatar
jvierra
Posts: 9699
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Problem with filling out textbox

Postby jvierra » Thu Jan 07, 2016 8:23 am

Why? Just use the mail in the user object. It is the primary address.
User avatar
alwo23@hotmail.com
Posts: 27
Joined: Sat Sep 28, 2013 1:56 am

Re: Problem with filling out textbox

Postby alwo23@hotmail.com » Thu Jan 07, 2016 8:27 am

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

Sorry but this is was the customer need.
User avatar
jvierra
Posts: 9699
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Problem with filling out textbox

Postby jvierra » Thu Jan 07, 2016 8:48 am

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: 27
Joined: Sat Sep 28, 2013 1:56 am

Re: Problem with filling out textbox

Postby alwo23@hotmail.com » Thu Jan 07, 2016 9:02 am

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.
User avatar
jvierra
Posts: 9699
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Problem with filling out textbox

Postby jvierra » Thu Jan 07, 2016 9:05 am

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.

Return to “Windows PowerShell”

Who is online

Users browsing this forum: No registered users and 1 guest