Page 1 of 1

Issue after update to v3.1.12

Posted: Thu Oct 25, 2012 3:26 pm
by jamesearl
I have an issue after I upgraded v3.1.12. I don't know if it's a bug or I have to set an option in the newer version. Basically I have a timer event on the form I created that worked in v3.0.8 but does not work in v3.1.12

In the frm_Load section i have the following:
$StartTime = $(get-Date).AddSeconds(30)

In the timer1_Tick section I have:
$TimeDiff = New-TimeSpan $(Get-Date) $StartTime
....and performing other stuff.

In v3.1.12 the following errors returns:

ERROR: New-TimeSpan : Cannot bind parameter 'End' to the target. Exception setting "End": "Object reference not set to an instance of an object."
PostInstall_Info.pff (159): ERROR: At Line: 159 char: 39
ERROR: + $TimeDiff = New-TimeSpan $(Get-Date) $StartTime
ERROR: + ~~~~~~~~~~
ERROR: + CategoryInfo : WriteError: (:) [New-TimeSpan], ParameterBindingException
ERROR: + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.NewTimeSpanCommand

What I noticed after I step through the program is that as soon as I leave the frm_Load section, $StartTime is immediately reset to $null. I guess the errors stem from $StartTime being set to $null. I am still new to this programming so I'm sorry if the solution is something simple. This happens on a physical system and on a virtual machine (VMWare) and I must note that I'm still in my trial period for PowerStudio. However, as I stated earlier, the code works flawless in v3.0.8

Is it something I'm doing wrong?

Issue after update to v3.1.12

Posted: Thu Oct 25, 2012 4:48 pm
by Alexander Riedel
Not seeing the entire code makes it a bit difficult, but...

From 3.0.8 to 3.1.12 you may now default to PowerShell V3. I don't know what you have installed and if that would make a difference. It is possible that V3 reacts to this differently than V2.

If $StartTime is reset right after you leave the function you most likely have only a local scope definition of the variable.
$StartTime = $(get-Date).AddSeconds(30)
$Script:StartTime = $(get-Date).AddSeconds(30)

to force a different scope.

Issue after update to v3.1.12

Posted: Fri Oct 26, 2012 5:24 am
by jamesearl
Your solution worked but I'm a little confused. In that same section (frm_Load) I set about 7 other variables (some wmi objects, some string values) along with $StartTime and they retain their values across the other functions in my script. Why do I have to set $StartTime as you have above?

btw, I've only been using Powershell V3

Issue after update to v3.1.12

Posted: Fri Oct 26, 2012 5:54 am
by Alexander Riedel
If you don't specifically declare your variables they are implicitely declared on first use. So PowerShell must have encountered these variables someplace else before it got to that code.
Now, PowerShell's parsing is not very transparent, nor are the scoping rules, so it is a good idea to explicitely declare variables that you want to use throughout the script
at a global level.
You can also use the "New-Variable" cmdlet to do that, which has a -scope parameter so you can specifically define the scope of that thing.