Jump to content

Recommended Posts

Posted

Good morning

 

I am just starting out on powershell and have a short script that outputs a countdown timer. The script runs perfectly in powershell_ise using F5, however when run from command line produces errors. After much searching I cannot find anything relevant to this error.

Script and error produced as follows:

 

SCRIPT

 

$delay = 5

$a=Get-Date -Format "dd/MM/yyyy"

[system.Media.SystemSounds]::Asterisk.Play()

$Counter_Form = New-Object System.Windows.Forms.Form

$Counter_Form.Text = "Archive logs"

$Counter_Form.Width = 600

$Counter_Form.Height = 200

$Counter_Form.StartPosition = "CenterScreen"

$Counter_Form.BackColor = "#ffffff"

$Counter_Label = New-Object System.Windows.Forms.Label

$Counter_Label.AutoSize = $true

$Counter_Label.ForeColor = "Black"

$normalfont = New-Object System.Drawing.Font("Segoe UI",10)

$Counter_Label.Font = $normalfont

$Counter_Label.Left = 50

$Counter_Label.Top = 30

$Counter_Form.Controls.Add($Counter_Label)

while ($delay -ge 1)

{

$Counter_Form.Show()

$Counter_Label.Text = " LOGS CANNOT BE ARCHIVED UNTIL FIRST DAY OF MONTH`n`nTodays date is: $a - Program will terminate in $($delay) seconds"

 

start-sleep 1

$delay -= 1

}

$Counter_Form.Close()

 

OUTPUT

 

Microsoft Windows [Version 6.0.6002]

Copyright © 2006 Microsoft Corporation. All rights reserved.

 

C:\Users\Richard>powershell c:\countdown.ps1

New-Object : Cannot find type [system.Windows.Forms.Form]: make sure the assembly containing this type is loaded.

At C:\countdown.ps1:4 char:27

+ $Counter_Form = New-Object <<<< System.Windows.Forms.Form

+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException

+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

 

Property 'Text' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:5 char:15

+ $Counter_Form. <<<< Text = "Archive logs"

+ CategoryInfo : InvalidOperation: (Text:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

Property 'Width' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:6 char:15

+ $Counter_Form. <<<< Width = 600

+ CategoryInfo : InvalidOperation: (Width:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

Property 'Height' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:7 char:15

+ $Counter_Form. <<<< Height = 200

+ CategoryInfo : InvalidOperation: (Height:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

Property 'StartPosition' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:8 char:15

+ $Counter_Form. <<<< StartPosition = "CenterScreen"

+ CategoryInfo : InvalidOperation: (StartPosition:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

Property 'BackColor' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:9 char:15

+ $Counter_Form. <<<< BackColor = "#ffffff"

+ CategoryInfo : InvalidOperation: (BackColor:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

New-Object : Cannot find type [system.Windows.Forms.Label]: make sure the assembly containing this type is loaded.

At C:\countdown.ps1:10 char:28

+ $Counter_Label = New-Object <<<< System.Windows.Forms.Label

+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException

+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

 

Property 'AutoSize' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:11 char:16

+ $Counter_Label. <<<< AutoSize = $true

+ CategoryInfo : InvalidOperation: (AutoSize:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

Property 'ForeColor' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:12 char:16

+ $Counter_Label. <<<< ForeColor = "Black"

+ CategoryInfo : InvalidOperation: (ForeColor:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

New-Object : Cannot find type [system.Drawing.Font]: make sure the assembly containing this type is loaded.

At C:\countdown.ps1:13 char:25

+ $normalfont = New-Object <<<< System.Drawing.Font("Segoe UI",10)

+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException

+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

 

Property 'Font' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:14 char:16

+ $Counter_Label. <<<< Font = $normalfont

+ CategoryInfo : InvalidOperation: (Font:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

Property 'Left' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:15 char:16

+ $Counter_Label. <<<< Left = 50

+ CategoryInfo : InvalidOperation: (Left:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

Property 'Top' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:16 char:16

+ $Counter_Label. <<<< Top = 30

+ CategoryInfo : InvalidOperation: (Top:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

You cannot call a method on a null-valued expression.

At C:\countdown.ps1:17 char:27

+ $Counter_Form.Controls.Add <<<< ($Counter_Label)

+ CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException

+ FullyQualifiedErrorId : InvokeMethodOnNull

 

You cannot call a method on a null-valued expression.

At C:\countdown.ps1:20 char:21

+ $Counter_Form.Show <<<< ()

+ CategoryInfo : InvalidOperation: (Show:String) [], RuntimeException

+ FullyQualifiedErrorId : InvokeMethodOnNull

 

Property 'Text' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:21 char:18

+ $Counter_Label. <<<< Text = " LOGS CANNOT BE ARCHIVED UNTIL FIRST DAY OF MONTH`n`nTodays date is: $a - Program

will terminate in $($delay) seconds"

+ CategoryInfo : InvalidOperation: (Text:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

You cannot call a method on a null-valued expression.

At C:\countdown.ps1:20 char:21

+ $Counter_Form.Show <<<< ()

+ CategoryInfo : InvalidOperation: (Show:String) [], RuntimeException

+ FullyQualifiedErrorId : InvokeMethodOnNull

 

Property 'Text' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:21 char:18

+ $Counter_Label. <<<< Text = " LOGS CANNOT BE ARCHIVED UNTIL FIRST DAY OF MONTH`n`nTodays date is: $a - Program

will terminate in $($delay) seconds"

+ CategoryInfo : InvalidOperation: (Text:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

You cannot call a method on a null-valued expression.

At C:\countdown.ps1:20 char:21

+ $Counter_Form.Show <<<< ()

+ CategoryInfo : InvalidOperation: (Show:String) [], RuntimeException

+ FullyQualifiedErrorId : InvokeMethodOnNull

 

Property 'Text' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:21 char:18

+ $Counter_Label. <<<< Text = " LOGS CANNOT BE ARCHIVED UNTIL FIRST DAY OF MONTH`n`nTodays date is: $a - Program

will terminate in $($delay) seconds"

+ CategoryInfo : InvalidOperation: (Text:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

You cannot call a method on a null-valued expression.

At C:\countdown.ps1:20 char:21

+ $Counter_Form.Show <<<< ()

+ CategoryInfo : InvalidOperation: (Show:String) [], RuntimeException

+ FullyQualifiedErrorId : InvokeMethodOnNull

 

Property 'Text' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:21 char:18

+ $Counter_Label. <<<< Text = " LOGS CANNOT BE ARCHIVED UNTIL FIRST DAY OF MONTH`n`nTodays date is: $a - Program

will terminate in $($delay) seconds"

+ CategoryInfo : InvalidOperation: (Text:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

You cannot call a method on a null-valued expression.

At C:\countdown.ps1:20 char:21

+ $Counter_Form.Show <<<< ()

+ CategoryInfo : InvalidOperation: (Show:String) [], RuntimeException

+ FullyQualifiedErrorId : InvokeMethodOnNull

 

Property 'Text' cannot be found on this object; make sure it exists and is settable.

At C:\countdown.ps1:21 char:18

+ $Counter_Label. <<<< Text = " LOGS CANNOT BE ARCHIVED UNTIL FIRST DAY OF MONTH`n`nTodays date is: $a - Program

will terminate in $($delay) seconds"

+ CategoryInfo : InvalidOperation: (Text:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

 

You cannot call a method on a null-valued expression.

At C:\countdown.ps1:26 char:20

+ $Counter_Form.Close <<<< ()

+ CategoryInfo : InvalidOperation: (Close:String) [], RuntimeException

+ FullyQualifiedErrorId : InvokeMethodOnNull

 

 

C:\Users\Richard>

 

I am guessing there is a simple explanation to this and I am misunderstanding the application somewhere, but as I said after much googling have been unable to find anything relevant so any assistance would be greatly received.

 

For your info OS is Vista SP2 and Powershell is v1.0.

 

Many thanks

Posted

Thank you .... but I'm afraid same error ... runs in powershell_ise F5 but fails running from command with exactly the same messages.

 

I like your wee quote ... I too used to work in IT back in the day, on the mighty IBM AS/400 ... where did it all go wrong ?

Posted

Many thanks.

I must have missed that link.

Adding that line has helped - I now get the GUI form plus title up but unfortunately no text !!

Posted

Apologies - I had to break off for a while before I could send you the updated script ... I have added a further line belt and braces, but still only get a blank form plus title and no text for some reason. The form stays on the screen for the five second duration !!

 

Add-Type -AssemblyName System.Windows.Forms

Add-Type -AssemblyName System.Drawing

[system.Windows.Forms.Application]::EnableVisualStyles();

$delay = 5

$a=Get-Date -Format "dd/MM/yyyy"

[system.Media.SystemSounds]::Asterisk.Play()

$Counter_Form = New-Object System.Windows.Forms.Form

$Counter_Form.Text = "Archive logs"

$Counter_Form.Width = 600

$Counter_Form.Height = 200

$Counter_Form.StartPosition = "CenterScreen"

$Counter_Form.BackColor = "#ffffff"

$Counter_Label = New-Object System.Windows.Forms.Label

$Counter_Label.AutoSize = $true

$Counter_Label.ForeColor = "Black"

$normalfont = New-Object System.Drawing.Font("Segoe UI",10)

$Counter_Label.Font = $normalfont

$Counter_Label.Left = 50

$Counter_Label.Top = 30

$Counter_Form.Controls.Add($Counter_Label)

while ($delay -ge 1)

{

$Counter_Form.Show()

$Counter_Label.Text = " LOGS CANNOT BE ARCHIVED UNTIL FIRST DAY OF MONTH`n`nTodays date is: $a - Program will terminate in $($delay) seconds"

 

start-sleep 1

$delay -= 1

}

$Counter_Form.Close()

 

I'll keep experimenting for now.

Posted

Many thanks for looking at this.

I have duplicated your command line and sadly I still only get the frame showing for the allotted time !!

Strangely, if I create a simple menu form (no countdown), all works fine from the command line.

Thoughts are a faulty Powershell.exe or incompatible while loop for Powershell v1.0.

Anyway it's not a deal breaker as the main project, GUI menu, appears to be fine .......... interesting though ?

 

Thanks again and have a good day.

Posted

A further update ....

 

I am pretty sure that there is a problem with the compatability between the tutorial examples I have been using to build the ps1 code and the Powershell version that I have on board (V1 on Vista) as my simple GUI menu that I have been working on also works in ISE but not correctly when "run with Powershell". I probably should look out for examples coded for V1 but who can tell !!! At least I can continue with good old VBS !!

Again many thanks for your assistance and have a good day.

Posted
Yeah - Vista (and PS V1) will be the issue.

 

I can confirm this running powershell with -version 2 (which is the lowest version the parameter will go down to) shows the errors you are seeing.

 

Running the above on powershell 5 or 7 works correctly.

 

There are examples of version 1 form timers but i would try and stay away from them and try and concentrate moving away from windows vista.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...