繁体   English   中英

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

[英]Batch script to get all printers on computer from all users

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

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

@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

该脚本的问题在于,它将仅显示当前登录用户的打印机。我要处理计算机上的所有用户。 我该怎么做呢?

Windows将网络打印机存储在键HKEY_USERS\\{SID}\\Printers\\Connections\\ 这是一个好的开始,因为我们可以为所有登录的用户处理这些。但是,当用户注销时,活动用户的配置单元将关闭并保存到其各自用户目录下的NTUSER.DAT ie 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文件加载到注册表中


附加说明:经过数小时的研究并且没有在线找到答案后,我回答了自己的问题。 我决定创建此脚本并重新措辞我的原始问题,以便遇到此问题的任何人都可以从中找到一些用处。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM