使用 powershell 设置 Windows 屏幕保护程序需要密码

Tim*_*Lee 5 windows powershell

我知道我可以通过注册表设置屏幕保护程序值,但这些值在登录时应用。我认为正确的方法是使用SystemParametersInfo函数,该函数可以立即应用更改。

我可以使用以下命令获取和设置屏幕保护程序超时值:

$signature = @"
[DllImport("user32.dll")]
public static extern bool SystemParametersInfo(int uAction, int uParam, ref int lpvParam, int flags );
"@

$systemParamInfo = Add-Type -memberDefinition  $signature -Name ScreenSaver -passThru

Function Get-ScreenSaverTimeout
{
  [Int32]$value = 0
  $systemParamInfo::SystemParametersInfo(14, 0, [REF]$value, 0)
  $($value/60)
}

Function Set-ScreenSaverTimeout
{
  Param ([Int32]$value)
  $seconds = $value * 60
  [Int32]$nullVar = 0
  $systemParamInfo::SystemParametersInfo(15, $seconds, [REF]$nullVar, 2)
}
Run Code Online (Sandbox Code Playgroud)

来自https://powershellreflections.wordpress.com/2011/08/02/control-your-screensaver-with-powershell/

我正在尝试更改“恢复时显示登录屏幕”的标志。通过以下方式获取值就成功了:

function Get-ScreenSaverSecure 
{
  [Int32]$value = 0
  $systemParamInfo::SystemParametersInfo(118, 0, [REF]$value, 0)
  $value
}
Run Code Online (Sandbox Code Playgroud)

但是,通过以下方式设置该值:

Function Set-ScreenSaverSecure
{
  [Int32]$nullVar = 0
  $systemParamInfo::SystemParametersInfo(119, $true, [REF]$nullVar, 2)
}
Run Code Online (Sandbox Code Playgroud)

不设置标志。从 MSDN 链接,我认为我应该传递 SPI_SETSCREENSAVESECURE (即 uiParam 119) $true 或 $false,具体取决于模式,但似乎都不适用。

任何帮助表示赞赏。

小智 2

我使用了@Tim AtLee 的函数并进行了如下定制:

Function Set-OnResumeDisplayLogon
{
    Param ([Int32]$value)
    [Int32]$nullVar = 0
    $systemParamInfo::SystemParametersInfo(119, $value, [REF]$nullVar, 2)
}
Run Code Online (Sandbox Code Playgroud)

然后调用者:

Set-OnResumeDisplayLogon(0)
Run Code Online (Sandbox Code Playgroud)

将“恢复时显示登录”标志设置为未选中;或者

Set-OnResumeDisplayLogon(1)
Run Code Online (Sandbox Code Playgroud)

将“恢复时显示登录”标志设置为选中