Issue after update to v3.1.12

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

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 11 years and 5 months 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.
User avatar
jamesearl
Posts: 11
Last visit: Tue May 30, 2023 10:40 am

Issue after update to v3.1.12

Post 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)
$timer1.Start()

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?
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Issue after update to v3.1.12

Post 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.
Change
$StartTime = $(get-Date).AddSeconds(30)
to
$Script:StartTime = $(get-Date).AddSeconds(30)

to force a different scope.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
jamesearl
Posts: 11
Last visit: Tue May 30, 2023 10:40 am

Issue after update to v3.1.12

Post by jamesearl »

Alexander,
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
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Issue after update to v3.1.12

Post 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.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 11 years and 5 months 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.