Feb 24 2012

Powershell line of code does not run when called through function but will run directly. What could be possible causes?

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

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?

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.

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.


Feb 20 2012

What is the best design practice when you have a class where properties are dependent on a web service call?

If I have a class with some read-only properties that are populated by a web service call, what is considered the best way to design this?

Is it considered proper for the property getters to make the web service call. It seems that the downside of this is that the getter is doing more than one thing and obscures the expense of the call. I realize that any of the property getters only needs to make the web service call once (by checking for nulls or flag before making the call). But that a single property getter could potentially be setting the private fields for other properties seems to smell to me.

On the other hand if I have have a public method (ie InitWebServiceVals) that calls the web service and updates the private fields I am creating a temporal dependency between the method and the property getters. So the API obscures the fact that you shouldn't read a property until the "InitWebServiceVals" is called.

Or is there some other method or pattern that addresses this? For example, making the webservice call in the constructor? Or is this generally indicative of a design issue?

I have run into this issue a number of times and I always ended up preferring the second method to the first.

Any thoughts?


Feb 03 2012

Comment by Seth Spearman on I need a regular expression that looks for <IPADDRESS> [x]created and excludes one particular IP address

I did not try anything. I have 0 knowledge of regex.

Feb 03 2012

I need a regular expression that looks for <IPADDRESS> [x]created and excludes one particular IP address

I want to open some log files one at a time and look for a certain string...

13:15:55 <AN IP ADDRESS> [2]created /myfile.zip 226 0

The key is that want to find files that have HAS an IP ADDRESS but EXCLUDES one particular IP address and that then has the "[2]created" part of the string. The number in brackets may vary.