Storing Data in a Collection

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
tconley@semprautilities.com
Posts: 13
Joined: Fri Dec 11, 2009 9:09 am

Storing Data in a Collection

Post by tconley@semprautilities.com » Thu Jan 28, 2010 2:29 am

I have create a varible in my form load that is meant to hold the working data while the script runs but it seems to not get updated correctly what am i doing wrong? It is suppose to be update when the submit button is clicked but It seems to not be holding the data properly.

Code: Select all


$FormEvent_Load={
$ServerName.Focus()
$DataStore = @() 
}

$handler_Submit_Click={
SubmitData
ClearData
}

Function SubmitData {
$Temp ="" | Select-Object Server_Name, Asset_Tag, Serial_Number, Issue
$Temp.Server_Name = $ServerName.Text
$Temp.Asset_Tag = $AssetTag.Text
$Temp.Serial_Number = $SerialNumber.Text
$Temp.Issue = $Issue.Text
$DataStore = $DataStore + $Temp
$Display.Text = $DataStore | ft -auto |Out-String
return $DataStore
}

uploads/16537/ServerRoomDailyCheck.zip

User avatar
tconley@semprautilities.com
Posts: 13
Joined: Fri Dec 11, 2009 9:09 am

Storing Data in a Collection

Post by tconley@semprautilities.com » Thu Jan 28, 2010 2:29 am

I have create a varible in my form load that is meant to hold the working data while the script runs but it seems to not get updated correctly what am i doing wrong? It is suppose to be update when the submit button is clicked but It seems to not be holding the data properly.

Code: Select all


$FormEvent_Load={
$ServerName.Focus()
$DataStore = @() 
}

$handler_Submit_Click={
SubmitData
ClearData
}

Function SubmitData {
$Temp ="" | Select-Object Server_Name, Asset_Tag, Serial_Number, Issue
$Temp.Server_Name = $ServerName.Text
$Temp.Asset_Tag = $AssetTag.Text
$Temp.Serial_Number = $SerialNumber.Text
$Temp.Issue = $Issue.Text
$DataStore = $DataStore + $Temp
$Display.Text = $DataStore | ft -auto |Out-String
return $DataStore
}

uploads/16537/ServerRoomDailyCheck.zip

User avatar
tconley@semprautilities.com
Posts: 13
Joined: Fri Dec 11, 2009 9:09 am

Storing Data in a Collection

Post by tconley@semprautilities.com » Thu Jan 28, 2010 12:57 pm

no it actually very common in powershell. You can define all of the different element and then add them to a custom object. I use it all of the time I just haven't used it with a form my regular scripts have no problem.

$temp = "" | select-object element1, element2

then you can add the values to the element

$temp.element1="a"
$temp.element2="b"look at this link
http://get-powershell.com/2008/03/04/cr ... m-objects/

code excerpt

Code: Select all

 $votes = @()
 foreach ($v in Get-Content votes.txt)
     {    
         $vote = "" | Select-Object v1,v2,v3,v4;
         $vote.v1,$vote.v2,$vote.v3,$vote.v4 = $v.split(",")
         $votes = $votes + $vote
     } 

tconley@semprautilities.com2010-01-28 20:02:29

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

Storing Data in a Collection

Post by jvierra » Thu Jan 28, 2010 2:14 pm

Declare $datastore as $global:datastore

It looks like it is going out of scope.

Remember that the form and it's events happen on different threads and cannot see each others data. I thing using a global may fix this. Can't test it right now.
jvierra2010-01-28 21:14:43

Locked