Page 1 of 1

$radiobutton1.Checked =$true not working

Posted: Fri Jun 08, 2018 11:23 am
by localpct
I'm trying to detect the powersettings for a PC

Code: Select all

$PowerSetting = Invoke-Command -ComputerName $computer { (powercfg /l | ? { $_.Contains('*') -and $_.Contains('GUID') }).Split()[3] }
		$statusbar1.Text = "Finding Power Setting"
		$PowerSetting = switch ($PowerSetting.trim())
		{
 "381b4222-f694-41f0-9685-ff5bb260df2e" { "Balanced" }
 "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c" { "High Performace" }
 "a1841308-3541-4fab-bc81-f71556f20b4a" { "Power Saver" }
		}
		$statusbar1.Text = "$($textbox1.text) power setting is $PowerSetting"
		if ($PowerSetting -eq "Balanced")
		{
			$radiobuttonHighPerformace.Checked =$true
		}
		if ($PowerSetting -eq "Power Saver")
		{
			$radiobuttonHighPerformace.Checked =$true
		}
		else
		{
			$radiobuttonBalanced.Checked =$true
		}
The current computer I'm working on, it's currently set to Balanced, but the radiobutton doesn't change to High Performace

Re: $radiobutton1.Checked =$true not working

Posted: Fri Jun 08, 2018 11:53 am
by jvierra
I suggest that stepping through with the debugger would give you the answer.

This article will show you how to use WMI to easily get the power settings:

https://blogs.technet.microsoft.com/hey ... owershell/

Re: $radiobutton1.Checked =$true not working

Posted: Fri Jun 08, 2018 11:53 am
by davidc
You have an if instead of an elseif on the "Power Saver" line:
  1. if ($PowerSetting -eq "Balanced")
  2. {
  3.     $radiobuttonHighPerformace.Checked = $true
  4. }
  5. elseif ($PowerSetting -eq "Power Saver")
  6. {  
  7.     $radiobuttonHighPerformace.Checked = $true 
  8. }
  9. else
  10. {
  11.     $radiobuttonBalanced.Checked = $true
  12. }

Re: $radiobutton1.Checked =$true not working

Posted: Fri Jun 08, 2018 12:02 pm
by jvierra
A quick look says you have numerous spelling and naming issues. Review the code line-by-line and word-by-word to find all.

Be sure your code names match the GUI designer names.

Re: $radiobutton1.Checked =$true not working

Posted: Fri Jun 08, 2018 12:14 pm
by localpct
jvierra wrote: Fri Jun 08, 2018 11:53 am I suggest that stepping through with the debugger would give you the answer.

This article will show you how to use WMI to easily get the power settings:

https://blogs.technet.microsoft.com/hey ... owershell/
GWMI doesn't work on all our PCs or I would have went with that.

Re: $radiobutton1.Checked =$true not working

Posted: Fri Jun 08, 2018 12:17 pm
by localpct
davidc wrote: Fri Jun 08, 2018 11:53 am You have an if instead of an elseif on the "Power Saver" line:
  1. if ($PowerSetting -eq "Balanced")
  2. {
  3.     $radiobuttonHighPerformace.Checked = $true
  4. }
  5. elseif ($PowerSetting -eq "Power Saver")
  6. {  
  7.     $radiobuttonHighPerformace.Checked = $true 
  8. }
  9. else
  10. {
  11.     $radiobuttonBalanced.Checked = $true
  12. }
Looks like this did it... Thank you!

Re: $radiobutton1.Checked =$true not working

Posted: Fri Jun 08, 2018 12:19 pm
by jvierra
This would be the better way to use the code:

Code: Select all

    
$sb = { (powercfg /l | ? { $_.Contains('*') -and $_.Contains('GUID') }).Split()[3] }
$powerSetting = Invoke-Command -ScriptBlock $sb -ComputerName  $computer 
$statusbar1.Text = 'Finding Power Setting'
switch ($powerSetting){
    '381b4222-f694-41f0-9685-ff5bb260df2e' {
            $statusbar1.Text = $setting = 'Balanced' 
            $radiobuttonBalanced.Checked =$true
        }
    '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' {
            $statusbar1.Text = $setting = 'High Performace' 
            $radiobuttonHighPerformance.Checked =$true
        }
    'a1841308-3541-4fab-bc81-f71556f20b4a' {
            $statusbar1.Text = $setting = 'Power Saver' 
            $radiobuttonPowerSaver.Checked =$true
        }
    default { $statusbar1.Text = 'Settings not found' }
}
$statusbar1.Text = "$($textbox1.text) power setting is $setting"

This avoids most issues of using an "if" construct and is more explicit and easier to manage.

Re: $radiobutton1.Checked =$true not working

Posted: Fri Jun 08, 2018 12:58 pm
by localpct
That's very cool.. I did not know you can use a switch statement like that.

Thanks!!!!

Re: $radiobutton1.Checked =$true not working

Posted: Fri Jun 08, 2018 1:03 pm
by jvierra
One of the first thing we learn in any PowerShell book or training is how to use the switch statement. It is one of the most useful control structures in all programming languages and is on steroids in PowerShell


https://kevinmarquette.github.io/2018-0 ... statement/