I have a powershell script with the following code in it...
$appdir = Split-Path -Path $MyInvocation.MyCommand.Path
$xfrdir = $appdir + "\xfr\"
$cfgFile = "ofx_config.cfg"
$cfgPath = $appdir + "\" + $cfgFile
$configData = New-Object System.Collections.ArrayList
# --- some other code here...
function Load-Config ()
{
if (test-path ($cfgPath))
{
$configData = Import-Clixml -path "$cfgPath"
}
}
# ---some other code here
load-config
When I just RUN this script in the ps ISE, load-config runs because it is at the end of script (i verified this with a breakpoint) but the $configData variable remains empty.
But if I immediately copy and past the line $configData = Import-Clixml -path "$cfgPath" into the powershell command line and run it then $configData is loaded with data. Does anyone have any ideas what might be going on?
EDIT
I think that what you are saying is that $configData in $configData = Import-Clixml -path "$cfgPath" is being treated as a whole separate variable (and is local to the function) because of scoping rules. I thought it would be more like a c# class and so would assign to the script level variable of the same name.
I am LOVING powershell but dynamic typing does make things trickier.
EDIT 2
Both answers were insightful. In such a case I usual give the answer to the person with the fewest reputation. And I did in fact Andy's second example in any case.
Seth