Storing Data in a Collection

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 14 years and 1 month 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
tconley@semprautilities.com
Posts: 18
Last visit: Thu May 27, 2021 11:28 am

Storing Data in a Collection

Post by tconley@semprautilities.com »

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: 18
Last visit: Thu May 27, 2021 11:28 am

Storing Data in a Collection

Post by tconley@semprautilities.com »

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: 18
Last visit: Thu May 27, 2021 11:28 am

Storing Data in a Collection

Post by tconley@semprautilities.com »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Storing Data in a Collection

Post by jvierra »

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
This topic is 14 years and 1 month 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