14

我想自动设置 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()
4

8 回答 8

5

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

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 文件中,您就可以开始了!

于 2014-02-07T18:11:55.923 回答
5

老实说,我看不出有什么理由不应该使用简单工作的工具...... ;) 不管怎样:使用 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 查询可能有更好的方法,这与您所做的几乎相同,只是稍作修改。

于 2013-03-17T17:24:12.937 回答
5

试试 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()
于 2013-03-17T08:27:08.873 回答
4

我在快速更改笔记本电脑上的“盖子”电源设置上找到了这个脚本。要求同意使用条款。适用于 W10。

@echo off 
set debug=1 
::************************* 
:: Script Name: lid.cmd 
:: author: Stephen D Arsenault 
:: Creation Date: 2013-september-07 
:: Modified Date: 2013-september-07 
:: Description:    Changes the lid action to sleep or do nothing 
:: parameters:     - on: sets lid action to do nothing 
::        - off: sets lid action to sleep 
::************************* 
 
echo Getting current scheme GUID 
::store the output of powercfg /getactivescheme in %cfg% 
for /f "tokens=* USEBACKQ" %%a in (`powercfg /getactivescheme`) do @set cfg=%%a 
if %debug%==1 echo Current %cfg% 
 
::trim power config output to get GUID 
set trimcfg=%cfg:~19,36% 
if %debug%==1 echo %trimcfg% 
 
::accepts arguments 
if %1==off set newVal=001 
if %1==OFF set newVal=001 
if %1==on set newVal=000 
if %1==ON set newVal=000 
 
::make power scheme change 
powercfg /setdcvalueindex %trimcfg% 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b- 
 
476b1d01c936 %newVal% >nul 2>&1 
 
powercfg /setacvalueindex %trimcfg% 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b- 
 
476b1d01c936 %newVal% >nul 2>&1 
 
if %errorlevel%==1 echo "Invalid Parameters" 
if %errorlevel%==1 pause 
if %errorlevel%==1 echo %date% %time% Invalid Parameters: %1 >>C:\tools\lid.log 
echo %date% %time% %1 >>C:\tools\lid.log 
 
::apply changes 
powercfg /s %trimcfg%

注意:该脚本包含对c:\tools. 这些引用仅用于记录,因此您可以安全地将它们注释掉或将它们修改为您的文件结构。

于 2017-12-31T13:35:32.180 回答
3

这一点 PowerShell 确实会更改注册表设置,但不会改变我的笔记本电脑在合上盖子时的行为方式。Usingpowercfg与此 WMI 对象执行相同的操作。

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

此脚本和 中的相同命令将其中的powercfg此子组更改Power Options >> Advanced SettingsDo 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对子组有用的东西。

于 2013-12-17T14:01:59.007 回答
3

我在 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."
于 2015-02-22T23:54:01.507 回答
1

带有所有别名的 Powercfg:

powercfg -setacvalueindex scheme_current sub_buttons pbuttonaction 0
于 2021-02-20T21:42:46.277 回答
1

我只是想在当前的 Windows 上复制它,而旧的解决方案将不再起作用(CIM 中不提供“激活”方法,并且尝试使用激活方法应用 WMI 的更改会引发错误,方法未定义)

我最终用来检查当前 PowerPlan (CIM) 上的设置是否正确的代码,并且应用更改的最简单方法似乎是powercfg.exe直接的。

未注释的powercfg行是别名,因为此设置有别名,但并非所有设置都有别名。

如果您不知道您想要的子组或设置的 GUID,您应该能够检查它们,powercfg /Qh这很长,您可能希望在文本文件中查看。

这将运行报告并在文本文件中打开它 powercfg /Qh > %temp%\CurrentPower Settings.txt && %temp%\CurrentPowerSettings.txt

下面的这个脚本只是将设置应用于当前的、活动的、电源计划——但如果你直接知道哪个

$DesiredValue = 0
$SettingSubGroupID = '4f971e89-eebd-4455-a8de-9e59040e7347'
$SettingGUID       = '5ca83367-6e45-459f-a27b-476b1d01c936'
    
# Select Current Power Plan
$currentPLan = Get-CimInstance -namespace "root\cimv2\power" -class Win32_powerplan | where {$_.IsActive} 
$schemeID= $currentPLan.InstanceID -replace "^Microsoft:PowerPlan\\{(.*?)}$",'$1'
    
# Apply Settings to Specific Power Plan by GUID
# $specificPowerPlanID = '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c'
# $currentPlan = Get-CimInstance -namespace "root\cimv2\power" -class Win32_powerplan | where {$_.InstanceID -match $specificPowerPlanID} 
# $schemeID= $currentPLan.InstanceID -replace "^Microsoft:PowerPlan\\{(.*?)}$",'$1'

# Optionally Activate this specific Power Plan
# powercfg -SetActive $specificPowerPlanID

$currPLanLidCLoseSettings = Get-CimAssociatedInstance -InputObject $currentPLan -ResultClassName 'win32_powersettingdataindex' | where {$_.InstanceID -match $SettingGUID} 
$improperSettings = $currPLanLidCLoseSettings | where {$_.settingIndexValue -ne $DesiredValue}

If ($improperSettings) {
    Write-Verbose -Verbose "Found $(@($improperSettings).Count) settings in current power plan that do not match. Fixing"
    # Aliases are taken from 'powercfg /Aliases'
    # SubGroup GUID Alias SUB_BUTTONS = 4f971e89-eebd-4455-a8de-9e59040e7347 
    # SubGroup GUID Alias LIDACTION   = 5ca83367-6e45-459f-a27b-476b1d01c936 
    
    powercfg -SETACVALUEINDEX $schemeID SUB_BUTTONS LIDACTION $DesiredValue
    powercfg -SETDCVALUEINDEX $schemeID SUB_BUTTONS LIDACTION $DesiredValue
    # powercfg -SETACVALUEINDEX $schemeID $SettingSubGroupID $SettingGUID$SettingGUID $DesiredValue
    # powercfg -SETDCVALUEINDEX $schemeID $SettingSubGroupID $SettingGUID $DesiredValue
    
    Write-Verbose -Verbose "New Values are below" 
    Get-CimAssociatedInstance -InputObject $currentPLan -ResultClassName 'win32_powersettingdataindex' | where {$_.InstanceID -match $SettingGUID} 
    
} 
else {
    Write-Verbose -Verbose "All settings are already correct" 
}
于 2021-11-16T17:35:10.897 回答