批处理脚本从所有用户获取计算机上的所有打印机

时间:2018-02-22 14:07:48

标签: windows batch-file printing registry

我正在尝试从批处理脚本中获取所有用户安装到计算机的所有打印机(联网和本地)的列表。

我知道如何使用批处理脚本来获取本地打印机:

@ECHO OFF
CLS
setLocal EnableDelayedExpansion
REM The textfile to store the printers
SET textFile="C:\printers.txt"
REM Clear the text file and start new
COPY /Y NUL !textFile! >nul 2>&1
ECHO ==============================================================
ECHO Processing locally installed printers
ECHO ==============================================================
ECHO.
ECHO Local Printers:>>!textFile!
FOR /F "tokens=*" %%a in ('WMIC PRINTER GET NAME') do (
    SET printer=%%a
    IF NOT "!printer:~0,4!" == "Name" (
        ECHO.!printer! >> !textFile!
    )
)
ENDLOCAL

此脚本的问题在于它只会显示当前登录用户的打印机。我想处理计算机上的所有用户。我该怎么做?

1 个答案:

答案 0 :(得分:0)

Windows将联网打印机存储在密钥HKEY_USERS\{SID}\Printers\Connections\下。这是一个良好的开端,因为我们可以为所有登录的用户处理这些。但是,当用户注销时,活动用户的配置单元将关闭并保存到其各自用户下的NTUSER.DAT&# 39; s目录。 i.e. C:\Users\someuser\NTUSER.DAT

因此,对于已注销的用户,我们需要将他们的NTUSER.DAT文件加载到注册表中,阅读打印机,然后断开他们的NTUSER.DAT文件。

最后,因为我们可以轻松抓取本地打印机。最终的脚本应如下所示:

@ECHO OFF
CLS
setLocal EnableDelayedExpansion
REM The textfile to store the printers
SET textFile="C:\printers.txt"
REM Clear the text file and start new
COPY /Y NUL !textFile! >nul 2>&1

REM =================================================================================================================
REM Get all networked printers for every user who is currently logged in
REM =================================================================================================================
ECHO ==============================================================
ECHO Processing users who are currently logged in!
ECHO ==============================================================
ECHO.
FOR /F "tokens=*" %%G IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"') DO (
    SET line=%%G
    FOR /F "tokens=3" %%X IN ('REG QUERY "HKLM\!line:~19!" /v "profileImagePath" 2^>nul') DO (
        SET userPath=%%X
        SET userPath=!userPath:*C:\Users\=!

        SET isUser=true

        REM Specify users to filter out
        IF "!userPath!" == "Administrator" SET isUser=false
        IF "!userPath!" == "defaultuser0" SET isUser=false
        IF "!userPath!" == "Public" SET isUser=false
        IF "!isUser!" == "true" (
            IF EXIST "C:\users\!userPath!\" (
                REM Make sure the key actually exists
                REG QUERY "HKU\!line:~76!" >nul 2>&1
                IF !ERRORLEVEL! EQU 0 (
                    ECHO Processing printers for !userPath!
                    ECHO !userPath!: >> !textFile!
                    REM Get all network printers
                    FOR /F "tokens=*" %%F IN ('REG QUERY "HKU\!line:~76!\Printers\Connections" 2^>nul') DO (

                        REM Format the output to only contain the printer name. Then print it to the text file.
                        SET newLine=%%F
                        SET output=!newLine:*Connections\=!
                        ECHO !output:,=\! >> !textFile!
                    )
                    ECHO.>>!textFile!
                )
            )
        )
    )
)
ECHO Logged in users are now processed.
ECHO.
REM =================================================================================================================
REM Get all networked printers for users who are logged off
REM =================================================================================================================
ECHO ==============================================================
ECHO Processing users who are logged off.
ECHO ==============================================================
ECHO.
FOR /F "tokens=*" %%D IN ('DIR C:\Users\ /B') DO (
    SET line=%%D
    SET isUser=true

    REM Specify users to filter out
    IF "!line!" == "Administrator" SET isUser=false
    IF "!line!" == "defaultuser0" SET isUser=false
    IF "!line!" == "Public" SET isUser=false
    IF "!isUser!" == "true" (
        XCOPY "C:\Users\!line!\NTUSER.DAT" "C:\Users\!line!\NTUSER_TEMP.DAT*" /H /Q >nul 2>&1
        IF !ERRORLEVEL! EQU 0 (
            REG LOAD "HKU\TempHive" "C:\Users\!line!\NTUSER_TEMP.DAT" >nul 2>&1

            REM Make sure the key actually exists
            REG QUERY "HKU\TempHive\Printers\Connections" >nul 2>&1
            IF !ERRORLEVEL! EQU 0 (

                REM Get all network printers
                ECHO Processing printers for !userPath!
                ECHO !line!: >> !textFile!
                FOR /F "tokens=*" %%F IN ('REG QUERY "HKU\TempHive\Printers\Connections" 2^>nul') DO (

                    REM Format the output to only contain the printer name. Then print it to the text file.
                    SET newLine=%%F
                    SET output=!newLine:*Connections\=!
                    ECHO - !output:,=\! >> !textFile!
                )
                ECHO.>>!textFile!
            )

            REG UNLOAD "HKU\TempHive" >nul 2>&1
            DEL /Q /A:H "C:\Users\!line!\NTUSER_TEMP.DAT"
        )
    )
)

REM =================================================================================================================
REM Get the locally installed printers
REM =================================================================================================================
ECHO ==============================================================
ECHO Processing locally installed printers
ECHO ==============================================================
ECHO.
ECHO Local Printers:>>!textFile!
FOR /F "tokens=*" %%a in ('WMIC PRINTER GET NAME') do (
    SET printer=%%a
    IF NOT "!printer:~0,2!" == "\\" (
        IF NOT "!printer:~0,4!" == "Name" (
            ECHO.!printer! >> !textFile!
        )
    )
)
ENDLOCAL

注意:请确保以管理员身份运行此脚本,以便将NTUSER.DAT文件加载到注册表中

附加说明:在我对此进行了数小时的研究并且没有在线找到答案后,我回答了我自己的问题。我决定创建这个脚本并重新编写我的原始问题,这样如果遇到这个问题的其他人可能会发现它有用处。