Problems migrating from 5.1 to 7.4.1

A couple of weeks ago, I was low on ideas for new snippets and scripts. I then decided to look ahead and prepare for a new era.
The preparations went smooth and it looked like a walkover; but so far it hasn’t been.
I have collaborated with AI on many tricky things in Powershell, but here at last “he” was running out of suggestions and adviced me to a Forum like this one. So here I am.
The last thing I made my friend AI do, was making a 10 point plan for testing out my Win10 installation running Powershell ISE and it went fine. I also made “him” make a function that ran through my approx. 200 snippets and scripts, that tested for 7.4.1 acceptance, only 2 passed, that’s when he adviced me to join you.
I have collected all my things so they are menu driven, with System.Windows.Forms.ListBox as driver. Now hold on tight:
Below is my listbox and it works fine in 5.1; but doesn’t show in 7.4.1. I know you will all say, we can’t reproduce that; but if I’m the only one, how can I then get it fixed?

#load the appropriate assemblies
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")

#create chart
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart 
$Chart.Width = 500
$Chart.Height = 400
$Chart.Left = 40
$Chart.Top = 30

#create a chartarea to draw on and add to chart
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea. 
$Chart.ChartAreas.Add($ChartArea)

#listbox form
$form = New-Object System.Windows.Forms.Form
$form.Text = 'HeatingChart 2020'
$form.Size = New-Object System.Drawing.Size(300,280)
$form.StartPosition = 'CenterScreen'

#legend
$legend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend
$legend.name = "Legend1"
$Chart.Legends.Add($Legend)
$Legend.Docking = 'bottom'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,190)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,190)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = '                        Choose a 3 year period'
$form.Controls.Add($label)

#listbox
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 135         

#[void]$listBox.Items.AddRange($Xyear)
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()

Hey Henning!

Welcome! When you have a moment, please go back and format your code: Formatting your code: How to format code on PowerShell.org

Sometimes, funky things can happen if all the code isn’t formatted as code. for example the code you provided starts and ends with ‘’’. I don’t t hink that’s intended. Much appreciated!

The code for me throws errors but does pop up a GUI at the end in both 5 and 7. You’ll need to verify and step through those lines of code and fix them. For example, I don’t think this line is right:

$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea.

i don’t think there should be an ending period.

Next we don’t know what you’re expecting. You spent a ton of time talking about AI and telling a story instead of telling us exactly what the issue is. What is the issue, what is the expected result, and what errors are you hitting? You need to help us help you.

This outputs the same thing for me in both 5 and 7:

#load the appropriate assemblies
[void][Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[void][Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms.DataVisualization')

#create chart
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Width = 500
$Chart.Height = 400
$Chart.Left = 40
$Chart.Top = 30

#create a chartarea to draw on and add to chart
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$Chart.ChartAreas.Add($ChartArea)

#listbox form
$form = New-Object System.Windows.Forms.Form
$form.Text = 'HeatingChart 2020'
$form.Size = New-Object System.Drawing.Size(300,280)
$form.StartPosition = 'CenterScreen'

#legend
$legend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend
$legend.name = 'Legend1'
$Chart.Legends.Add($Legend)
$Legend.Docking = 'bottom'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,190)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,190)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Choose a 3 year period'
$form.Controls.Add($label)

#listbox
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 135

#[void]$listBox.Items.AddRange($Xyear)
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()

I fixed that period issue and all the single/double quote issues, which may have been from copy/pastinng into the forum. This makes VS code happy, with the exception of course, $result isn’t used but I trhink that’s ok. AI is great, but I’d suggest learning what it’s doing so you can better fix issues. AI, in my experience, doesn’t always do the job correctly, and can make up things out of thin air with 100% confidence. Learning what things are doing and simply stepping through your code a line at a time and see what’s failing and what isn’t is a great way to start learning PS.

1 Like

Hi there Nick and thank you.
What is my story telling all about. I tried to give my Powershell background. I tried to tell, that now is the time
to shift from 5.1 to 7.4.1. I tried to tell that it gives errors and that is why I’m here now.
I tried to tell that a big part of success is that the underlying system is a 100% in place. I tried to tell that I can’t know
if it is; but I have tried to get AI to help test it, so that’s not the main problem for errors.

Now, what do I expect of all this. I hope to start a heading saying: Problems with migrating from 5.1 to 7.4.1
and tell about all the things I was met with, either in one Heading or in as many as it takes.

Finally, I thought I had seen, that you wanted code to be surrounded by 3 ticks and the period you said you have changed,
is not in my the original draft. I have added a major thing to the story and that is, I’m running Powershell ISE

Why is that? Windows PowerShell will not go away any time soon? If there is no particular function you need from 7.x you don’t have to migrate. :man_shrugging:t3:

I’d recommend using VSCode instead.

Hey Henning,

Thank you for updating the original code. It is very much appreciated!

I think there might be some misunderstanding, I apologize. The questions I asked are really geared towards your specific issue and not from a general 5 → 7 migration. From a general perspective, when you create an issue on the forums, we really just need to focus on that specific issue to resolve. we need to focus on the specific task you are asking us about. Given that, each specific issue you write about on these forums, generally includes a few things that will help us help you (some of which you’ve done, thank you!):

  1. Problem Statement (what problem are you trying to overcome?)
  2. Desired Result (What is the output suppose to look like?)
  3. Share your formatted code
  4. Share any thing you have tried, as well as the error messages you are running into.

The more details the better, and in most cases, it’s best to assume we know nothing about your environment, how your running the code, etc. To that end:

In my previous response, I took a quick stab at a small refactor of your code. It fixes one small bug I found. However, that bug wasn’t preventing the GUI from working. The code I provided displays the following GUI and runs without errors in both 7 and 5.1:
image

Is this the expected output? It seems incomplete, like i’m missing data in the main area but that’s the code you provided, and it performs, on my end, identically in both powershell 5 and 7, so from my perspective, there’s not a 5 vs 7 issue here. However, if you’ve modified the code for 7 because of AI, and hte original PS5 code works differently, you need to clarify this and share your original PS5 code. I’m just not sure how to help if I don’t know what the expected result is.

Hi Olaf thank you for your post. The lines above made me take the decision. But you are right there is absolutely nothing to complain about (everything works in 5.1), except I am looking for new adventures :slight_smile: As for ISE vs. VsCode, I can say I run both but take ISE as first choise

This is how it invites to a graph for a 3 year period, when chosen the graph shows; but not in 7.4.1

#load the appropriate assemblies
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")

#create chart
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart 
$Chart.Width = 500
$Chart.Height = 400
$Chart.Left = 40
$Chart.Top = 30

#create a chartarea to draw on and add to chart
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$Chart.ChartAreas.Add($ChartArea)

#listbox form
$form = New-Object System.Windows.Forms.Form
$form.Text = 'HeatingChart 2020'
$form.Size = New-Object System.Drawing.Size(300,280)
$form.StartPosition = 'CenterScreen'

#legend
$legend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend
$legend.name = "Legend1"
$Chart.Legends.Add($Legend)
$Legend.Docking = 'bottom'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,190)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,190)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = '                          Choose 3 year period'
$form.Controls.Add($label)

#listbox
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 135

                 #set years in listbox
                 [void] $listBox.Items.Add('                                  👀')
                 $y=2020
                 $x=2021
                 $z=2022
                 $count = 1
                 $Xyear = @()
                 $antyear = (Get-Date).year - 2019
                 While ($count -lt $antyear)
                 {
                      $Xyear += @("$y $x $z")
                      $y++
                      $x++
                      $z++
                      $count ++
                 }

                 [void]$listBox.Items.AddRange($Xyear)
                 $form.Controls.Add($listBox)
                 $form.Topmost = $true
                 $result = $form.ShowDialog()

I have been testing and have made this observation:

Version 5.1

seen in command pane:

C:\Users\Henning\OneDrive\Personlige\WindowsPowerShell\LookUpCmd.ps1

Result: Listbox

It works like using Run script button PF5


Change to 7.4.1

seen in command pane:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$SendKeys = [System.Windows.Forms.SendKeys]

$form = New-Object System.Windows.Forms.Form
$form.Text = ‘Commandshell’
#$form.Size = New-Object System.Drawing.Size(300,280)
$form.Size = New-Object System.Drawing.Size(300,480)
$form.StartPosition = ‘CenterScreen’

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,404)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = ‘OK’
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

#$cancelButton = New-Object System.Windows.Forms.Button
#$cancelButton.Location = New-Object System.Drawing.Point(150,404)
#$cancelButton.Size = New-Object System.Drawing.Size(75,23)
#$cancelButton.Text = ‘Cancel’
#$cancelButton.Text = ‘Sort’
#$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
#$form.CancelButton = $cancelButton
#$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = ’ ’
$form.Controls.Add($label)

$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
#$listBox.Height = 135
$listBox.Height = 360

################################################### Uden sort #################################################

(SS64 = 1) og (PDQ = 2)

$A = @(
‘add-appxpackage’

And so on, the rest of the script; but no indication of exec. It works like using Run selection button PF8


Question: why the difference from version 5.1 and to 7.4.1 (this is probably what makes the error)

A break through. I have tested in VSCode version 7.4.1 and the listbox is showing. But we still need to see it work in Powershell ISE

PowerShell_ISE does not support PowerShell core (6.x and up). :point_up:t3:

Hi Olaf. In that case, case is closed, and you are right, there is no need to change if no severe need. Thank you for the information !

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.