Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

powershell - Why is try {} on my ps1 script wont work?

I have a code like this:

$powerSchemes = powercfg /l | ForEach-Object { 
    if ($_ -match 'Power Scheme GUID:s*([-0-9a-f]+)s*(([^)]+))s*(*)?') { 
        [PsCustomObject]@{ 
            GUID       = $matches[1] 
            SchemeName = $matches[2] -eq 'Ultimate Performance' 
            Active     = $matches[3] 
        } 
    } 
} 
 
$customScheme = $powerSchemes | Where-Object { $_.SchemeName -eq 'Ultimate Performance' } 
 
try { 
    if (!$customScheme.Active) {
        powercfg /s $($customScheme.GUID) 
    }
} catch { 
    powercfg -duplicatescheme e9a42b02-d5df-448d-aa00-03f14749eb61 
    if (!$customScheme.Active) {
        powercfg /s $($customScheme.GUID) 
    }
}

I make a try{} cause I know not everyone going to have Ultimate Performance powerplan. But it give me error

powercfg : Invalid Parameters -- try "/?" for help
At C:UsersMyWinDesktopPowerPlan.ps1:15 char:3
+         powercfg /s $($customScheme.GUID)
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Invalid Parameters -- try "/?" for help:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

isn't try{} going ignore

try { 
    if (!$customScheme.Active) {
        powercfg /s $($customScheme.GUID) 
    }

and just jump to the catch{}??

Sorry I'm a newbie though, if you need more information about the code, just tell me

reason I didnt put

powercfg -duplicatescheme e9a42b02-d5df-448d-aa00-03f14749eb61 
if (!$customScheme.Active) {
   powercfg /s $($customScheme.GUID) 

on try{} so it didn't keep making new powerplan, if theres already Ultimate Performance powerplan, then just active it

question from:https://stackoverflow.com/questions/66056539/why-is-try-on-my-ps1-script-wont-work

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

*** UPDATED *** Actually you don't even need a try/catch block:

Clear-Host

$TestScheme = 'Ultimate Performance'

$powerSchemes = powercfg /l | ForEach-Object { 
    if ($_ -match 'Power Scheme GUID:s*([-0-9a-f]+)s*(([^)]+))s*(*)?') { 
        [PsCustomObject]@{ 
            GUID       = $matches[1] 
            SchemeName = $matches[2]  
            Active     = $matches[3] -eq '*'
        } 
    } 
} 
 
$powerschemes   #*** For Debugging Only ***
 
$customScheme = $powerSchemes | Where-Object { $_.SchemeName -eq "$TestScheme" } 

$ErrorActionPreference = "SilentlyContinue"        #*** For Debugging Only ***
"`nCustom Scheme: $($customScheme.SchemeName) `n"  #*** For Debugging Only ***
$ErrorActionPreference = "Continue"                #*** For Debugging Only ***

If ($Null -eq $CustomScheme) {
  "Power scheme named: $TestScheme does NOT Exist!"
}

Else {

      If ($customScheme.Active) {
        "$TestScheme scheme ACTIVE!" 
      } 
      Else { 
       "$TestScheme Not Active" 
      }

} #End Else

Test this code on my Dell XPS8920 which has an Ultimate Performance scheme.

I've completely rewritten the code and I think it covers the bases. Note: that I changed your Custom PSObject logic to keep the name and indicate the status in the Active property.

Here are the test results with debugging information included, you can delete the code marked as such for production.

GUID                                 SchemeName               Active
----                                 ----------               ------
381b4222-f694-41f0-9685-ff5bb260df2e Balanced                  False
49ef8fc0-bb7f-488e-b6a0-f1fc77ec649b Dell                      False
831878fb-3408-4f4b-a88c-72fdbe2ced67 Ultimate Performance       True
8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c High performance          False
a1841308-3541-4fab-bc81-f71556f20b4a Power saver               False
a7573aee-d201-4451-bf29-7165c6858f5c winword                   False
f92ea615-6000-45ae-96b2-cb6c364f0ae4 MRBackup                  False
fce6c371-318d-4bc2-b28e-6425737e2eef Samsung High Performance  False

Custom Scheme: Ultimate Performance 

Ultimate Performance scheme ACTIVE!


GUID                                 SchemeName               Active
----                                 ----------               ------
381b4222-f694-41f0-9685-ff5bb260df2e Balanced                  False
49ef8fc0-bb7f-488e-b6a0-f1fc77ec649b Dell                      False
831878fb-3408-4f4b-a88c-72fdbe2ced67 Ultimate Performance       True
8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c High performance          False
a1841308-3541-4fab-bc81-f71556f20b4a Power saver               False
a7573aee-d201-4451-bf29-7165c6858f5c winword                   False
f92ea615-6000-45ae-96b2-cb6c364f0ae4 MRBackup                  False
fce6c371-318d-4bc2-b28e-6425737e2eef Samsung High Performance  False

Custom Scheme: Ultimate Performance 

Ultimate Performance scheme ACTIVE!

GUID                                 SchemeName               Active
----                                 ----------               ------
381b4222-f694-41f0-9685-ff5bb260df2e Balanced                  False
49ef8fc0-bb7f-488e-b6a0-f1fc77ec649b Dell                      False
831878fb-3408-4f4b-a88c-72fdbe2ced67 Ultimate Performance      False
8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c High performance           True
a1841308-3541-4fab-bc81-f71556f20b4a Power saver               False
a7573aee-d201-4451-bf29-7165c6858f5c winword                   False
f92ea615-6000-45ae-96b2-cb6c364f0ae4 MRBackup                  False
fce6c371-318d-4bc2-b28e-6425737e2eef Samsung High Performance  False

Custom Scheme:  

Power scheme named: Test does NOT Exist!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...