簡體   English   中英

從命令行在 Windows 上靜默設置屏幕保護程序?

[英]Silently set the screensaver on Windows from the command line?

我知道如果你運行:

rundll32.exe desk.cpl,InstallScreenSaver toasters.scr

您可以將屏幕保護程序設置為toasters.scr但它也會打開屏幕保護程序配置對話框。 有沒有辦法通過運行命令在不打開任何對話框的情況下在 Windows 上設置屏幕保護程序?

現代方式,使用 powershell

Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name ScreenSaveActive -Value 1
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name ScreenSaveTimeOut -Value 60
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name scrnsave.exe -Value "c:\windows\system32\mystify.scr"

您可以將這些放在您使用以下命令執行的ScrnInstaller.ps1腳本中:

$ powershell -WindowStyle hidden -f "ScrnInstaller.ps1"

注意:這些參數被組策略參數取代(例如,強制為企業中的用戶設置屏幕保護程序)。 您有多種方法可以在此處強制執行。

具有用戶/域/站點意識:組策略

使用 powershell 和組策略,您可以管理您影響更改的組織單位/域/站點,並且它優先於用戶設置。

在屏幕保護程序超時的情況下更改組策略:

Get-Command -Module GroupPolicy
New-GPO -Name "ScreenSaverTimeOut" -Comment "Sets the time to 900 seconds"
Set-GPRegistryValue -Name "ScreenSaverTimeOut" -Key "HKCU\Software\Policies\Microsoft\Windows\Control Panel\Desktop" -ValueName ScreenSaveTimeOut -Type DWord -Value 900
New-GPLink -Name "ScreenSaverTimeOut" -Target "ou=MyOU,dc=myenterprise,dc=com"
gpupdate /force /target:computer

為 myenterprise.com。 對於 New-GPLink 參數: msdn 參考

然后您可以查看您的全科醫生:

Get-GPO -Name "ScreenSaverTimeOut" | Get-GPOReport -ReportType HTML -Path $Home\report.html
Invoke-Item $Home\report.html

我找到了兩種方法:

1)在注冊表中添加,確保處於活動狀態並設置超時(僅幾分鍾)

CMD

reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v SCRNSAVE.EXE /t REG_SZ /d C:\Windows\System32\Mystify.scr /f
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaveActive /t REG_SZ /d 1 /f
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaveTimeOut /t REG_SZ /d 60 /f

爪哇

setScreenSaver(true, 1, "C:\\Windows\\System32\\Mystify.scr");

/**
 * set screen saver active, timeout and scr, only works in Windows
 * @param isActive
 * @param timeOutMin only minutes
 * @param pathToScr path to scr
 * @throws IOException
 */
public static void setScreenSaver(boolean isActive, int timeOutMin, String pathToScr) throws IOException{
    String _isActive = isActive ? "1" : "0";
    //only works with minutes, min. 1 min
    String _timeOut = timeOutMin > 1 ? timeOutMin*60+"" : "60";
    Runtime.getRuntime().exec(new String[] { "reg", "add", "HKEY_CURRENT_USER\\Control Panel\\Desktop", "/v", "SCRNSAVE.EXE", "/t", "REG_SZ", "/d", pathToScr,"/f" });
    Runtime.getRuntime().exec(new String[] { "reg", "add", "HKEY_CURRENT_USER\\Control Panel\\Desktop", "/v", "ScreenSaveActive", "/t", "REG_SZ", "/d", _isActive,"/f" });
    Runtime.getRuntime().exec(new String[] { "reg", "add", "HKEY_CURRENT_USER\\Control Panel\\Desktop", "/v", "ScreenSaveTimeOut", "/t", "REG_SZ", "/d", _timeOut,"/f" });
}

2)從注冊表中獲取路徑並重寫scr文件,但如果設置為null,則無法執行。

而不是運行該命令,你應該只運行命令

reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v SCRNSAVE.EXE /t REG_SZ /d C:\Windows\system32\toasters.scr /f

這將更新屏幕保護程序

c:\Windows\System32\scrnsave.scr -start

上面的批處理文件將啟動空白屏幕保護程序。 優點; 用 C:\\Windows\\system32\\ 中的其他 5 個屏幕保護程序之一替換 scrnsave.scr 也可以正常工作。 您無需重新啟動系統,它會立即啟動。 缺點; 我不知道如何設置超時。 我懷疑像c:\\Windows\\System32\\scrnsave.scr /ScreenSaveTimeOut = 150 -start這樣的行會有一個參數我認為需要一個單獨的批處理文件來停止屏幕保護程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM