none
Powershell - Timer to check registry value and auto-refresh form RRS feed

  • Question

  • Hi, I need some assistance with the script below.  I have a registry key that I need to monitor - if the value is not 0 then I want a button on a form to change color.  The script partly works - at the moment the button starts off color red - and if the registry key is changed at any time to not equal 0, the button then turns dark red, which is great however if the key is set back to 0 the key stays dark red.  Can anyone help me with this?

    Thanks in advance

    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
    [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null

    Function check{
    if ((Get-ItemProperty 'hklm:software\agere' -name modemtype | select -exp modemtype) -ne 0) {
       $button1.backcolor = "darkred" }

    $button1.refresh()
    $form.refresh()

    }
        $timer1 = New-Object System.Windows.Forms.Timer
        $timer1.Enabled = $true
        $timer1.Start()
        $timer1.Interval = 5000

    #form

    $timer1.add_tick({check})   
    $Form = New-Object System.Windows.Forms.Form
    #$Form.Location.X = -1000
    #$Form.Location.Y = -2200
    $Form.width = 550
    $Form.height = 250
    $Form.backcolor = "CornflowerBlue"
    $Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
    $Form.Text = "Form"
    $Form.Font = New-Object System.Drawing.Font("Verdana",10,[System.Drawing.FontStyle]::Bold)
    $Form.maximumsize = New-Object System.Drawing.Size(550,250)
    $Form.startposition = "centerscreen"

    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter") {}})
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
        {$Form.Close()}})
    #Draw buttons
    $Button1 = new-object System.Windows.Forms.Button
    $Button1.Location = new-object System.Drawing.Size(1,1)
    $Button1.Size = new-object System.Drawing.Size(80,74)
    $Button1.Text = "Registry"
    $Button1.backcolor = "Red"
    $Button1.Add_Click({check})

    $Form.Controls.Add($Button1)

    $Form.Add_Shown({$Form.Activate()})
    $Form.ShowDialog()
    #$timer1.enabled =$false



                       
    Thursday, February 13, 2014 8:29 PM

Answers

  • $p=(Get-ItemProperty hklm:software\agere -name modemtype).ModemType
    if($p -eq 0){
         $button1.backcolor='darkred'
    }else{
         $button1.backcolor='blue'
    }
    


    ¯\_(ツ)_/¯

    • Marked as answer by Thumper71 Thursday, February 13, 2014 9:34 PM
    Thursday, February 13, 2014 9:31 PM
  • Here is a complete demo on how to manage a timer and use the registry.  I set up a dummy registry key that you will have tp create:

    http://1drv.ms/1bvWnuK

    hkcu:Software\Test DWORD ModemType

    or change it to your key.


    ¯\_(ツ)_/¯

    • Marked as answer by Thumper71 Friday, February 14, 2014 8:54 PM
    Friday, February 14, 2014 8:17 PM

All replies

  • $p=(Get-ItemProperty hklm:software\agere -name modemtype).ModemType
    if($p -eq 0){
         $button1.backcolor='darkred'
    }else{
         $button1.backcolor='blue'
    }
    


    ¯\_(ツ)_/¯

    • Marked as answer by Thumper71 Thursday, February 13, 2014 9:34 PM
    Thursday, February 13, 2014 9:31 PM
  • Fantastic - Many Thanks for that!
    Thursday, February 13, 2014 9:34 PM
  • I just realised though that my timer must be wrong because when I change the registry value the color changes right away!  I wasn't too confident in what I'd written - any tips for wrapping the registry key check in a timer that loops every 30 seconds? 
    Thursday, February 13, 2014 11:17 PM
  • Set the timer to tick every 30 seconds.

    ¯\_(ツ)_/¯

    Thursday, February 13, 2014 11:54 PM
  • You cannot create the timer before you create the form and you must add the timer to the form.

    ¯\_(ツ)_/¯

    Thursday, February 13, 2014 11:57 PM
  • Not sure how to do that but below seemed to add a pause however not 30 seconds, which I though 300000 would equal.  Can you help with a code example?

    $Form = New-Object System.Windows.Forms.Form
    #$Form.Location.X = -1000
    #$Form.Location.Y = -2200
    $Form.width = 550
    $Form.height = 250
    $Form.backcolor = "CornflowerBlue"
    $Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
    $Form.Text = "Form"
    $Form.Font = New-Object System.Drawing.Font("Verdana",10,[System.Drawing.FontStyle]::Bold)
    $Form.maximumsize = New-Object System.Drawing.Size(550,250)
    $Form.startposition = "centerscreen"
    $timer1 = New-Object System.Windows.Forms.Timer
    $timer1.Enabled = $true
    $timer1.Start()
    $timer1.Interval = 300000
    $timer1.add_tick({check})

    Friday, February 14, 2014 7:00 PM
  • 1 second = 1000 milliseconds


    ¯\_(ツ)_/¯

    Friday, February 14, 2014 7:07 PM
  • My mistake - but the timer in my example if working would take longer to refresh the form, but it is happening in a few seconds.  Can you advise
    Friday, February 14, 2014 7:15 PM
  • You didn't make the timer part of the form.

    ¯\_(ツ)_/¯

    Friday, February 14, 2014 7:29 PM
  • I thought the $timer1 was the timer part.  I see if i add start-sleep -seconds 30 to end of the check function this pauses the refresh for the period I need
    Friday, February 14, 2014 7:37 PM
  • I thought the $timer1 was the timer part.  I see if i add start-sleep -seconds 30 to end of the check function this pauses the refresh for the period I need

    You will not get the timer to work as long as you have not added it to the form.  Currently only the button is part of the form.  Placing a sleep is just undermining the purpose of the timer.


    ¯\_(ツ)_/¯

    Friday, February 14, 2014 7:45 PM
  • Note also that the refresh will occur on the tick and not after the tick.  By loading the form with the timer detached the timer will tick once when it is created.  You need to create the form and all controls correctly or the results will be unpredictable.


    ¯\_(ツ)_/¯

    Friday, February 14, 2014 7:46 PM
  • Start with this tutorial and do all exercises.  Post back with questions if you do not understand the lessons.

    http://blogs.technet.com/b/stephap/archive/2012/04/23/building-forms-with-powershell-part-1-the-form.aspx


    ¯\_(ツ)_/¯

    Friday, February 14, 2014 7:48 PM
  • Here is a complete demo on how to manage a timer and use the registry.  I set up a dummy registry key that you will have tp create:

    http://1drv.ms/1bvWnuK

    hkcu:Software\Test DWORD ModemType

    or change it to your key.


    ¯\_(ツ)_/¯

    • Marked as answer by Thumper71 Friday, February 14, 2014 8:54 PM
    Friday, February 14, 2014 8:17 PM
  • Thanks for that Jim - there's some nice examples in there I can learn from
    • Edited by Thumper71 Friday, February 14, 2014 9:05 PM
    Friday, February 14, 2014 8:54 PM