批处理脚本从所有用户获取计算机上的所有打印机
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批处理脚本从所有用户获取计算机上的所有打印机相关的知识,希望对你有一定的参考价值。
我试图从批处理脚本中获取所有用户安装到计算机的所有打印机(联网和本地)的列表。
我知道如何使用批处理脚本来获取本地打印机:
@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}PrintersConnections
下。这是一个良好的开端,因为我们可以为所有登录的用户处理这些。但是,当用户注销时,活动用户的配置单元将关闭并保存到各自用户目录下的NTUSER.DAT
。 i.e. C:UserssomeuserNTUSER.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 "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionProfileList"') 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!PrintersConnections" 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 "HKUTempHive" "C:Users!line!NTUSER_TEMP.DAT" >nul 2>&1
REM Make sure the key actually exists
REG QUERY "HKUTempHivePrintersConnections" >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 "HKUTempHivePrintersConnections" 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 "HKUTempHive" >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
文件加载到注册表中
附加说明:在我对此进行了数小时的研究并且没有在线查找答案后,我回答了我自己的问题。我决定创建这个脚本并重新编写我原来的问题,这样如果遇到这个问题的其他人可能会发现它有用处。
以上是关于批处理脚本从所有用户获取计算机上的所有打印机的主要内容,如果未能解决你的问题,请参考以下文章
bat批处理脚本获取window系统所有用户名并设置密码,禁用Guest账户