Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.
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 5 years and 9 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.
Locked
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am
Post
by localpct » Fri Jun 08, 2018 11:23 am
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
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times
Post
by davidc » Fri Jun 08, 2018 11:53 am
You have an if instead of an elseif on the "Power Saver" line:
if ( $PowerSetting -eq "Balanced" )
{
$radiobuttonHighPerformace .Checked = $true
}
elseif ( $PowerSetting -eq "Power Saver" )
{
$radiobuttonHighPerformace .Checked = $true
}
else
{
$radiobuttonBalanced .Checked = $true
}
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Fri Jun 08, 2018 12:02 pm
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.
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am
Post
by localpct » Fri Jun 08, 2018 12:14 pm
GWMI doesn't work on all our PCs or I would have went with that.
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am
Post
by localpct » Fri Jun 08, 2018 12:17 pm
davidc wrote: ↑ Fri Jun 08, 2018 11:53 am
You have an if instead of an elseif on the "Power Saver" line:
if ( $PowerSetting -eq "Balanced" )
{
$radiobuttonHighPerformace .Checked = $true
}
elseif ( $PowerSetting -eq "Power Saver" )
{
$radiobuttonHighPerformace .Checked = $true
}
else
{
$radiobuttonBalanced .Checked = $true
}
Looks like this did it... Thank you!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Fri Jun 08, 2018 12:19 pm
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.
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am
Post
by localpct » Fri Jun 08, 2018 12:58 pm
That's very cool.. I did not know you can use a switch statement like that.
Thanks!!!!
This topic is 5 years and 9 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.
Locked