注册表值设置更改后未生效

时间:2019-03-07 11:09:12

标签: powershell sysadmin dsc powershell-dsc

我正在使用以下Powershell DSC配置来禁用视觉效果。

Configuration OSConfig 
{
    param
    (
        [parameter()]
        [string]
        $NodeName = 'localhost'
    )

    # It is best practice to always directly import resources, even if the resource is a built-in resource.
    Import-DscResource -Name Service
    Import-DSCResource -Name WindowsClient
    Import-DscResource -Name Registry
    Import-DscResource -Name WindowsFeature
    Import-DscResource -Name WindowsOptionalFeature

    Node $NodeName
    {
        # The name of this resource block, can be anything you choose, as long as it is of type [String] as indicated by the schema.
        Registry VisualEffects
        {
        Ensure = "Present"

        Key = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VisualEffects"

        ValueName = "VisualFXSetting"

        ValueData = "2"

        ValueType = "Dword"

        }
    }

  }

运行Start-DSCConfiguration命令后,我可以看到visualfxsetting值已更新为2。但是在GUI(高级系统属性->视觉效果)下,它仍然显示为“让计算机选择最适合您的设置”,而不是“调整为最佳性能”。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在注册表中设置新值后,需要让Windows资源管理器知道某些更改。

重新启动计算机可以做到这一点,但是您可以尝试简单地重新启动资源管理器进程:

Stop-Process -ProcessName explorer

另一种方法可能是像这样使用Win32 API:

function Refresh-Explorer {
    $code = @'
private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);   //  http://www.pinvoke.net/default.aspx/Constants/HWND.html
private const uint WM_SETTINGCHANGE   = (uint)0x1a;                   //  http://www.pinvoke.net/default.aspx/Constants/WM.html
private const uint SMTO_ABORTIFHUNG   = (uint)0x0002;                 //  http://www.pinvoke.net/default.aspx/Enums/SendMessageTimeoutFlags.html
private const uint SHCNE_ASSOCCHANGED = (uint)0x08000000L;            //  http://www.pinvoke.net/default.aspx/Enums/SHChangeNotifyEventID.html
private const uint SHCNF_FLUSH        = (uint)0x1000;                 //  http://www.pinvoke.net/default.aspx/Enums/SHChangeNotifyFlags.html

[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SendMessageTimeout (IntPtr hWnd, uint Msg, IntPtr wParam, string lParam, uint fuFlags, uint uTimeout, IntPtr lpdwResult);

[System.Runtime.InteropServices.DllImport("Shell32.dll")]
private static extern int SHChangeNotify(uint eventId, uint flags, IntPtr item1, IntPtr item2);

public static void Refresh()  {
    SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_FLUSH, IntPtr.Zero, IntPtr.Zero);
    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero);
}
'@

    Add-Type -MemberDefinition $code -Namespace Win32Refresh -Name Explorer
    [Win32Refresh.Explorer]::Refresh()
}

# call the above function to tell explorer something has changed
Refresh-Explorer

希望有帮助