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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…