Tôi muốn tự động thiết lập hành động mà Windows 7 thực hiện khi nắp được đóng trên máy tính xách tay của tôi, vì điều này được đặt lại qua GPO mỗi lần tôi đăng nhập.Powershell Đặt nắp Đóng Hành động
Tôi biết rằng tôi có thể sử dụng lệnh powercfg trong một kịch bản hàng loạt để đạt được điều này:
powercfg -setacvalueindex 5ca83367-6e45-459f-a27b-476b1d01c936 0
powercfg -setdcvalueindex 5ca83367-6e45-459f-a27b-476b1d01c936 0
Tuy nhiên, đây là một lý do tốt để cố gắng học tập một số PowerShell. Nỗ lực đầu tiên của tôi mất hơn 10 giây để chạy.
Làm cách nào tôi có thể cải thiện ở bên dưới, cả về thời gian chạy & về độ sạch của mã. Điều gì sẽ là cách mạnh mẽ thành ngữ để tiếp cận bên dưới?
$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()
Có vẻ như anh ấy thậm chí không cần truy vấn powerettingdataindex, chỉ cần powerplan. –