Not working as expected

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
Locked
User avatar
island_guy
Posts: 14
Joined: Wed Aug 14, 2013 11:07 am

Not working as expected

Post by island_guy » Fri Oct 24, 2014 1:18 pm

I have the following code in a form:

foreach ($item in $vara)
{
[String]$item = $item
if ($item -match 'User=212Z')
{

[array]$lookupmailaddress = Get-ADObject -LDAPFilter "(sAMAccountName=$tempsamconvert)" -SearchBase 'DC=212Z,DC=mgd2,DC=msft,DC=net' -Properties * -Server 212Z.msft.net | Select-Object mail
}

Else

#do something else.

When I set the $vara from the shell to the SAM account name ($NVP000-H8RNU4T06F1P) and then run the above code:

Get-ADObject -LDAPFilter "(sAMAccountName=$tempsamconvert)" -SearchBase 'DC=212Z,DC=mgd2,DC=msft,DC=net' -Properties * -Server 212Z.msft.net | Select-Object mail

it works? Just not in the application. Can someone help explain this? It's passing the same value in $tempsamconvert as I typed out and set in the shell. It's just being set in the form not on the shell.

Thank you.
Last edited by island_guy on Fri Oct 24, 2014 1:43 pm, edited 3 times in total.

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Not working as expected

Post by jvierra » Fri Oct 24, 2014 1:29 pm

It is not possible to understand what you are asking.

What is not working? What is set in the "shell"? What shell?

Are you trying to pass a value from one form to another?

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Not working as expected

Post by jvierra » Fri Oct 24, 2014 1:46 pm

Here is a little direction.

Use Get-AdUser with a SAmAccountName

$somevariable='somesamname'

Get-Aduser $somevariable

A SamAccountName is unique across a domain. It need no searchbase or any filters. It is a legitimate identity.

$mail=Get-AdUser usersSam | %{$_.Mail}

That is all you ever need.

"mail" is not an array. It is a single string value. Casting it to an array will not be of much uses.

Your loop is also not very useful:
PowerShell Code
Double-click the code block to select all.
foreach ($item in $vara) {
    [String]$item = $item
    if ($item -match 'User=006F'){
        [array]$lookupmailaddress=Get-ADObject -LDAPFilter "(sAMAccountName=$tempsamconvert)" -f -SearchBase 'DC=006f,DC=mgd2,DC=msft,DC=net' -Properties * -Server 212Z.msft.net | Select-Object mail
    }else{
        #do something else. 
	}
}
1. It serves not purpose to force $item to a string..
2. $item is never used in loop.
3. $vara is undefined
4. $tempsamconvert is never set to a value.

User avatar
island_guy
Posts: 14
Joined: Wed Aug 14, 2013 11:07 am

Re: Not working as expected

Post by island_guy » Fri Oct 24, 2014 2:17 pm

Let me try and provide some more information and try to be clear. It's been a long day.

I have a single form application. In this form , I have the following code:
## $vara is a collection of SamAccounts that looks something like this:
User=212Z\$Z43NSA-ASD424
User=212Z\$ASDFAS-ADFASD
User=212Z\$AD4324-S32W3A


foreach ($item in $vara)
{
[String]$item = $item
if ($item -match 'User=212Z')
{

[array]$lookupmailaddress = Get-ADObject -LDAPFilter "(sAMAccountName=$tempsamconvert)" -SearchBase 'DC=212Z,DC=mgd2,DC=msft,DC=net' -Properties * -Server 212Z.msft.net | Select-Object mail
}

Else

#do something else.



I expect if the value in $item matches "User=212Z" and equals true then process:
[array]$lookupmailaddress = Get-ADObject -LDAPFilter "(sAMAccountName=$tempsamconvert)" -SearchBase 'DC=212Z,DC=mgd2,DC=msft,DC=net' -Properties * -Server 212Z.msft.net | Select-Object mail

This should fetch the mail object using the Get-ADObject cmdlet. and return something like john.doe@acme.com; The purpose of the code is lookup the mail object by the SAM account name. All SaM account names start with $
Then store the mail object which is much easier to read (john.doe@acme.com) in the array $lookupmailaddress.

It runs in the form with no errors but the Get-ADObject cmdlet returns nothing to the [array]$lookupmailaddress

Now if I open a power shell session (shell) and run:
$tempsamconvert = $ANd4df43232-232



And then run the same cmdlet in the same powershell session
[array]$lookupmailaddress = Get-ADObject -LDAPFilter "(sAMAccountName=$tempsamconvert)" -SearchBase 'DC=212Z,DC=mgd2,DC=msft,DC=net' -Properties * -Server 212Z.msft.net | Select-Object mail


It stores the mail object (john.doe@acme.com) in the new variable. No issues. I can't seem to make sense of it. Hope this helps. The only thing, I can guess is tripping me up is that the Sam Accounts all start with a "$" in there name.

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Not working as expected

Post by jvierra » Fri Oct 24, 2014 2:56 pm

Wow! Go slow:

What is this?

I have a single form application. In this form , I have the following code:
## $vara is a collection of SamAccounts that looks something like this:

User=212Z\$Z43NSA-ASD424
User=212Z\$ASDFAS-ADFASD
User=212Z\$AD4324-S32W3A

Is it a hashtable? It can't be because there are no quotes. It cannot be a string array for the same reason.

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Not working as expected

Post by jvierra » Sat Oct 25, 2014 2:13 am

I just noticed this part.
Now if I open a power shell session (shell) and run:
$tempsamconvert = $ANd4df43232-232
SamAccountNames cannot begin with a "$". It is not allowed in AD.

The line above does nothing because the object "$ANd4df43232-232
" is not a string. It is a variable.

If you run this in a new session this is what happens:
PowerShell Code
Double-click the code block to select all.
PS C:\scripts> $tempsamconvert = $ANd4df43232-232
PS C:\scripts> $tempsamconvert
-232
PS C:\scripts>
Which makes no sense.

Locked