Je voulais automatiser la configuration de l'action que Windows 7 prend lorsque le couvercle est fermé sur mon ordinateur portable de travail, car cela est réinitialisé via GPO chaque fois que je me connecte.Powershell Set Lid Close Action
Je sais que je peux utiliser la commande powercfg dans un script batch pour y parvenir:
powercfg -setacvalueindex 5ca83367-6e45-459f-a27b-476b1d01c936 0
powercfg -setdcvalueindex 5ca83367-6e45-459f-a27b-476b1d01c936 0
Cependant, ce fut une bonne excuse pour essayer d'apprendre un peu Powershell. Ma première tentative prend plus de 10 secondes à courir.
Comment puis-je améliorer sur le ci-dessous, à la fois en termes d'exécution & en termes de propreté du code. Quelle serait la façon idiomatique powershell d'aborder le dessous?
$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()
On dirait qu'il n'a même pas besoin d'interroger pour powersettingdataindex, juste powerplan. –