Reputation: 2655
I am calling a PS function while passing it a string and a datatable variable. I want to extract all items in the datatable
and store them in an object.
Here is my code from my script:
Function myFunc
{
param ($id, $dt)
$data = $dt | where-object ($_.ID -eq $ID)
}
$myVar = myFunc -id "stringID" -dt myDataTable
When this runs my data
variable stays empty.
I have a breakpoint placed at the end of my project so I can try and play with the values. When I try and re-create the issue it works:
[DBG]: PS C:\WINDOWS\system32>> $abc = $myDataTable | where-object {$_.ID -eq $ID}
[DBG]: PS C:\WINDOWS\system32>> $abc
ID : //info here
Location : //info here
Managedr : //info here
It just will not work in my actual script.
Upvotes: 0
Views: 2116
Reputation: 68341
The function runs in it's own scope, and the scope and all variables created in the scope are disposed when the function exits. Functions should return data, which you assign to variables in the local scope, like this:
Function myFunc
{
param ($id, $dt)
$myDataTable | where-object ($_.ID -eq $ID)
}
$myVar = myFunc -id "stringID" -dt myDataTable
Upvotes: 4