跳转至

屏保 VBS 部署脚本

通过 VBS 脚本 + SFX 自解压包,批量给终端设置屏保。适用于企业终端批量管理场景。


实现思路

  1. .scr 屏保文件和 install.vbs 脚本打包成一个 SFX 自解压 EXE
  2. 用户双击 EXE → 自动解压 → 执行 VBS 脚本
  3. VBS 脚本通过 WMI 查找当前登录用户,修改其注册表屏保设置

VBS 脚本

脚本的核心逻辑:通过 explorer.exe 进程找到当前登录用户,再通过用户 SID 修改对应的注册表项。

Function ShowSID(strUser)
    On Error Resume Next
    Dim oWMI, oAs, oA, sSid
    Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
    If strUser <> "" Then
        Set oAs = oWMI.ExecQuery("Select SID From Win32_Account" & _
                  " WHERE Name='" & strUser & "'")
        For Each oA In oAs
            sSid = Mid(oA.SID, InStrRev(oA.SID, "-") + 1)
            Set oShell = CreateObject("WScript.Shell")

            ' 启用屏保
            oShell.RegWrite "HKEY_USERS\" + oA.SID + _
                "\Control Panel\Desktop\ScreenSaveActive", "1"
            ' 解锁时需要密码
            oShell.RegWrite "HKEY_USERS\" + oA.SID + _
                "\Control Panel\Desktop\ScreenSaverIsSecure", "1"
            ' 指定屏保程序路径
            oShell.RegWrite "HKEY_USERS\" + oA.SID + _
                "\Control Panel\Desktop\SCRNSAVE.EXE", _
                "C:\Windows\XYJZ20260316PB.scr"
        Next
    End If

    Set oA = Nothing
    Set oAs = Nothing
    Set oWMI = Nothing
    If Err.Number <> 0 Then
        WScript.Echo "Error occurred: " & Err.Description
        Err.Clear
    End If
End Function

' 通过 explorer.exe 进程获取当前登录用户
N = "explorer.exe"
Set wmi = GetObject("winmgmts:\\.\root\cimv2")
Set ps = wmi.ExecQuery("select * from win32_process where name=" & "'" & N & "'")
For Each p In ps
    p.GetOwner user
    ShowSID(user)
Next

打包成 64 位 SFX

方法一:7-Zip 命令行(推荐)

最轻量的方式,copy /b 拼接就行:

:: 第 1 步:创建压缩包
7z a archive.7z screensaver.scr install.vbs

:: 第 2 步:编写 SFX 配置

config.txt

;!@Install@!UTF-8!
Title="Screensaver Installer"
RunProgram="wscript.exe \"%%T\\install.vbs\""
;!@InstallEnd@!
:: 第 3 步:拼接生成 64 位 SFX
copy /b "C:\Program Files\7-Zip\7zS2.sfx" + config.txt + archive.7z setup_x64.exe

7zS2.sfx 是 7-Zip 自带的 64 位 SFX 模块,只有约 170KB。

方法二:WinRAR 64 位

:: 64 位 WinRAR 默认就用 Default64.SFX 模块
"C:\Program Files\WinRAR\WinRAR.exe" a -sfx -ep1 setup_x64.exe screensaver.scr install.vbs

GUI 方式:右键选文件 → 添加到压缩文件 → 勾选"创建自解压格式" → 高级 → SFX 选项中配置解压后执行的命令。

方法三:NSIS(复杂场景)

适合需要更多安装逻辑的场景:

Unicode true
Target amd64-unicode

Name "Screensaver Deploy"
OutFile "setup_x64.exe"
InstallDir "$TEMP\scr_deploy"

Section
  SetOutPath $INSTDIR
  File "screensaver.scr"
  File "install.vbs"
  nsExec::ExecToLog 'wscript.exe "$INSTDIR\install.vbs"'
SectionEnd

验证 EXE 是否为 64 位

$bytes = [System.IO.File]::ReadAllBytes("setup_x64.exe")
$peOffset = [BitConverter]::ToInt32($bytes, 0x3C)
$machine = [BitConverter]::ToUInt16($bytes, $peOffset + 4)
switch ($machine) {
    0x8664 { "x64 (64-bit)" }
    0x014c { "x86 (32-bit)" }
    0xAA64 { "ARM64" }
}