Cannot Add properties to Object in Powershell Studio, but works in Powershell ISE

Ask your Windows 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.
Locked
User avatar
rubberduck
Posts: 54
Joined: Wed Oct 30, 2013 9:34 am

Cannot Add properties to Object in Powershell Studio, but works in Powershell ISE

Post by rubberduck » Thu Nov 15, 2018 10:30 am

PowerShell Studio 2018 (64 Bit)
Build: v5.5.155
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.17134.0

Hello,

I have a simple task that works in the Powershell ISE, oddly enough not in Powershell Studio.

I have a click event that takes a name from a textbox,
searches for this name via Get-ADUser and then builds a new object with only two properties.
These two properties should be added to the 'PSCUSTOMOBJECT' '$Result'.

If I use the click event to query new names with Get-ADuser again and again, all new entries should added in $Result again.

Unfortunately in my Powershell Studio the $Result array seems to be deleted over and over again, there is always only one entry in it, the last one.
In Powershell ISE every entry is added correctly and the array logically gets bigger.
Maybe I also have a thinking error?

Here is the code of Powershell-Studio

Code: Select all

$Result = @()


$btnGo_Click = {
	
	$FindUsername=$txtUsername.Text
	$User = (Get-ADUser -Identity $FindUsername)
	
		
$data = @{
		
SamAccountName    = $User.SamAccountName
DistinguishedName = $User.DistinguishedName
		
	}
	
	$Result += New-Object -TypeName PSCUSTOMOBJECT -Property $data
	
	
	#debug
	Write-Host " : $($Result.SamAccountName)"
			
  	
}
As I say, the output of $Result.SamAccountName is always only the last name, I'd expect an addition of multiple names if I run it multiple times.

Output from Studio

Code: Select all

: Peter.Pan
: Bon.Jovi

*** PowerShell Script finished. ***
>> Execution time: 00:00:39
>> Script Ended
I was expecting an addition here, so peter.pan bon.jovi

Now the same in Powershell ISE

Code: Select all

$Result=@()

while ($exit -eq 'false' )
{
 
 
 $Input = Read-Host  'Input User Name to find or type "q" '
 
 
if ($Input -ne 'q'){

  $User = (Get-ADUser -Identity $Input)
  
  $data = @{

    SamAccountName    = $User.SamAccountName
    DistinguishedName = $User.DistinguishedName

  }

  $Result += New-Object -TypeName PSCUSTOMOBJECT -Property $data

  Write-host -ForegroundColor Yellow ": $($Result.SamAccountName)"

}else{

$exit = 'true'

}
 
 
}
Output of ISE

Code: Select all

Input User Name to find or type "q" : peter.pan
: peter.pan

Input User Name to find or type "q" : bon.jovi
[b]peter.pan bon.jovi[/b]
Here the variable $Result was extended by the second entry. In the example above there were two single entries


I hope I made it clear what I meant.
Do you have any idea what that could be, so why does the variable "$Result" seem to be reset?

many thanks in advance
Last edited by rubberduck on Thu Nov 15, 2018 1:26 pm, edited 1 time in total.

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

Re: Cannot Add properties to Object Studio, but works in Powershell ISE

Post by jvierra » Thu Nov 15, 2018 12:58 pm

The issue is that the code in the ISE and in the form are completely different. Each has a different fundamental scope which causes the "$result" array to not be saved in the form.

Here is how to remedy scope in a form:

Code: Select all

$script:result = @()
$buttonGo_Click={
    
	$User = Get-ADUser $txtUsername.Text
	
    $script:result += [pscustomobject]@{
        SamAccountName    = $User.SamAccountName
        DistinguishedName = $User.DistinguishedName
    }
    
	#debug
    
    Write-Host '---------New result list---------' -fore green
    for($i = 0;$i -lt $result.Count;$i++){
        Write-Host ('{0}:{1}' -f $i, $result[$i-1].SamAccountName)
    }
    
}

Locked