Powershell Set Lid关闭动作

问题描述 投票:11回答:6

我希望自动设置Windows 7在我的工作笔记本电脑上关闭盖子时所采取的操作,因为每次登录时都会通过GPO重置。

我知道我可以在批处理脚本中使用powercfg命令来实现这个目的:

powercfg -setacvalueindex 5ca83367-6e45-459f-a27b-476b1d01c936 0
powercfg -setdcvalueindex 5ca83367-6e45-459f-a27b-476b1d01c936 0

然而,这是尝试学习一些PowerShell的一个很好的借口。我的第一次尝试需要10秒以上才能运行。

在运行时和代码清洁方面,我如何改进以下内容。接近下面的惯用PowerShell方法是什么?

$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()
windows powershell wmi
6个回答
4
投票

我想做同样的事情并得到完全相同的问题。最后,我发现您需要在命令行中插入优于您要修改的注册表项的注册表项:

powercfg -setacvalueindex 5ca83367-6e45-459f-a27b-476b1d01c936 0
powercfg -setdcvalueindex 5ca83367-6e45-459f-a27b-476b1d01c936 0

应成为:

powercfg -setacvalueindex 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 0
powercfg -setdcvalueindex 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 0

把它放在BAT文件中就可以了!


3
投票

试试WMI加速器:

$class = ([wmi] '\root\cimv2\power:Win32_PowerSettingDataIndex.InstanceID="Microsoft:PowerSettingDataIndex\\{8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c}\\DC\\{5ca83367-6e45-459f-a27b-476b1d01c936}"')
$class.SettingIndexValue = 0
$class.Put()

3
投票

老实说,我没有看到任何理由你不应该使用简单工作的工具...;)无论如何:当使用WMI时,通常最好过滤尽可能多的左边。这里不应该有太大的区别,但有时差异很大。这就是我用WMI做的方法:

$Name = @{
    Namespace = 'root\cimv2\power'
}
$ID = (Get-WmiObject @Name Win32_PowerPlan -Filter "IsActive = TRUE") -replace '.*(\{.*})"', '$1'
$Lid = '{5ca83367-6e45-459f-a27b-476b1d01c936}'
Get-WmiObject @Name Win32_PowerSettingDataIndex -Filter "InstanceId LIKE '%$Id\\%C\\$Lid'" | 
    Set-WmiInstance -Arguments @{ SettingIndexValue = 0 }

使用更高级的WQL查询可能有更好的方法,这与您所做的几乎相同,只是进行了一些修改。


3
投票

PowerShell的这一部分实际上确实改变了注册表设置,但它并没有改变我的笔记本电脑在关闭盖子时的行为方式。使用powercfg与此WMI对象的作用相同。

显然,注册表子组PowerButtons and Lid有两组不同的注册表项。

此脚本和powercfg中的相同命令将Power Options >> Advanced Settings中的此子组更改为Do Nothing(或Sleep,或Hibernate,或您设置的0 - 3中的任何选项编号),但在Change what the power buttons doChange what closing the lid does的实际控制面板设置中不受影响。控制面板中的设置实际上决定了操作,至少对于此子组而言。

如果我使用powercfg或类似的PS脚本来编写上面的内容,我实际上可以Change Plan Settings获得所需的行为调暗显示(或其他)。我找不到任何适用于Power Buttons and Lid Sub Group的东西。


3
投票

我在Microsoft页面上找到了这个。要求同意使用条款。适用于W10。

https://gallery.technet.microsoft.com/scriptcenter/Quickly-change-the-lid-b78eb77d

lid on (sets to 'do nothing')
lid off (sets to 'sleep')

今天早上发布的脚本包含两个对c:\ tools的硬编码引用。这些引用仅用于日志记录,因此您可以安全地将它们注释掉或将其修改为文件结构。


2
投票

我在Windows 8.1中看到的是,当为电源方案更改盖子动作时,该电源方案必须是活动和首选电源方案。可以通过PowerCfg设置有功功率方案,并且可以通过注册表设置首选功率方案。

这是一个Powershell脚本来更改它们和盖子动作:

#Enable High performance
$powerScheme = "High performance"

#Find selected power scheme guid
$guidRegex = "(\{){0,1}[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}(\}){0,1}"
[regex]$regex = $guidRegex
$guid = ($regex.Matches((PowerCfg /LIST | where {$_ -like "*$powerScheme*"}).ToString())).Value

#Change preferred scheme
$regGuid = "{025A5937-A6BE-4686-A844-36FE4BEC8B6D}"
$currentPreferredScheme = Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\ControlPanel\NameSpace\$regGuid -Name PreferredPlan 
if ($currentPreferredScheme.PreferredPlan -ne $guid) {
    Set-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\ControlPanel\NameSpace\$regGuid -Name PreferredPlan -Value $guid
    Write-Host -ForegroundColor Green "Preferred scheme successfully changed. Preferred scheme is now '$powerScheme'." 
} else {
    Write-Host -ForegroundColor Yellow "Preferred scheme does not need to be changed. Preferred scheme is '$powerScheme'." 
}

#Change active scheme
$currentActiveScheme = PowerCfg /GETACTIVESCHEME
if ($currentActiveScheme | where {$_ -notlike "*$guid*"}) {
    PowerCfg /SETACTIVE $guid
    Write-Host -ForegroundColor Green "Power scheme successfully changed. Current scheme is now '$powerScheme'." 
} else {
    Write-Host -ForegroundColor Yellow "Power scheme does not need to be changed. Current scheme is '$powerScheme'." 
}

#Do not sleep when closing lid on AC
PowerCfg /SETACVALUEINDEX $guid SUB_BUTTONS LIDACTION 000
Write-Host -ForegroundColor Green "No action when closing lid on AC."
© www.soinside.com 2019 - 2024. All rights reserved.