Volevo automatizzare l'impostazione dell'azione eseguita da Windows 7 quando il coperchio è chiuso sul mio laptop di lavoro, poiché viene reimpostato tramite GPO ogni volta che effettuo l'accesso.Powershell Set Lid Close Azione
So che posso usare il comando powercfg in uno script batch per raggiungere questo obiettivo:
powercfg -setacvalueindex 5ca83367-6e45-459f-a27b-476b1d01c936 0
powercfg -setdcvalueindex 5ca83367-6e45-459f-a27b-476b1d01c936 0
Tuttavia, questa è stata una buona scusa per tentare imparare alcune PowerShell. Il mio primo tentativo richiede più di 10 secondi per essere eseguito.
Come posso migliorare sul sotto, sia in termini di runtime & in termini di pulizia del codice. Quale sarebbe il modo idiomatico in powerhell di approcciare il sotto?
$DO_NOTHING = 0
$activePowerPlan = Get-WmiObject -Namespace "root\cimv2\power" Win32_PowerPlan | where {$_.IsActive}
$rawPowerPlanID = $activePowerPlan | select -Property InstanceID
$rawPowerPlanID -match '\\({.*})}'
$powerPlanID = $matches[1]
# The .GetRelated() method is an inefficient approach, i'm looking for a needle and this haystack is too big. Can i go directly to the object instead of searching?
$lidCloseActionOnACPower = $activePowerPlan.GetRelated("win32_powersettingdataindex") | where {$_.InstanceID -eq "Microsoft:PowerSettingDataIndex\$powerPlanID\AC\{5ca83367-6e45-459f-a27b-476b1d01c936}"}
$lidCloseActionOnBattery = $activePowerPlan.GetRelated("win32_powersettingdataindex") | where {$_.InstanceID -eq "Microsoft:PowerSettingDataIndex\$powerPlanID\DC\{5ca83367-6e45-459f-a27b-476b1d01c936}"}
$lidCloseActionOnACPower | select -Property SettingIndexValue
$lidCloseActionOnACPower.SettingIndexValue = $DO_NOTHING
$lidCloseActionOnACPower.put()
$lidCloseActionOnBattery | select -Property SettingIndexValue
$lidCloseActionOnBattery.SettingIndexValue = $DO_NOTHING
$lidCloseActionOnBattery.put()
Sembra che non abbia nemmeno bisogno di eseguire una query per powerettingdataindex, solo powerplan. –