Vbscript 需要查看哪些打印机连接到x台机器。存储在名为Computers.txt的文本文件中的机器名称
我需要帮助从文件中读取机器名称并列出连接到这些机器的打印机。这里我列出了我的变量Vbscript 需要查看哪些打印机连接到x台机器。存储在名为Computers.txt的文本文件中的机器名称,vbscript,Vbscript,我需要帮助从文件中读取机器名称并列出连接到这些机器的打印机。这里我列出了我的变量 Const ForAppending = 8 Const ForReading = 1 Dim WshNetwork, objPrinter, intDrive, intNetLetter, fso 这里我定义了我的输入文件文本文件 Set fso = CreateObject("Scripting.FileSystemObject") Set InputFile = fso.OpenTextFile("C:\x
Const ForAppending = 8
Const ForReading = 1
Dim WshNetwork, objPrinter, intDrive, intNetLetter, fso
这里我定义了我的输入文件文本文件
Set fso = CreateObject("Scripting.FileSystemObject")
Set InputFile = fso.OpenTextFile("C:\xVBS Scripts\Computers.txt", 1)
strComputer = InputFile.ReadAll
Set WshNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objItem in colItems
UserName = objItem.UserName
arrUserName = Split(UserName, "\", -1, 1)
varUserName = arrUserName(1)
Next
在这里,我定义了我的输出文件,稍后将自动打开该文件
filOutput = varUserName & ".txt"
If objFSO.FileExists(filOutput) Then
objFSO.DeleteFile(filOutput)
End If
Set objOutputFile = objFSO.OpenTextFile (filOutput, ForAppending, True)
For Each objPrinter In colInstalledPrinters
strTest = Left(objPrinter.Name, 2)
objOutputFile.WriteLine(objPrinter.Name)
Next
Set objPrinter = WshNetwork.EnumPrinterConnections
If objPrinter.Count = 0 Then
WScript.Echo "No Printers Mapped "
Else
For intDrive = 0 To (objPrinter.Count -1) Step 2
intNetLetter = IntNetLetter + 1
printer = "UNC Path " & objPrinter.Item(intDrive) & " = " & objPrinter.Item(intDrive +1) & " Printer : " & intDrive
objOutputFile.WriteLine(printer)
Next
End If
objOutputFile.Close
Wscript.Sleep 1500
MsgBox "Printer mapping report is located" & vbNewLine & "in the following directory: " & filOutput , vbInformation, "Report Located At"
WshShell.Run "Notepad " & filOutput, 1, False
Wscript.Quit
您正在将整个文件读入变量
strComputer
:
Set InputFile = fso.OpenTextFile("C:\xVBS Scripts\Computers.txt", 1)
strComputer = InputFile.ReadAll
^^^^^^^
如果要逐行处理文件,则需要以下内容:
Set InputFile = fso.OpenTextFile("C:\xVBS Scripts\Computers.txt")
For Each strComputer In Split(InputFile.ReadAll, vbNewLine)
...
Next
InputFile.Close
Set InputFile = fso.OpenTextFile("C:\xVBS Scripts\Computers.txt")
Do Until InputFile.AtEndOfStream
strComputer = InputFile.ReadLine
...
Loop
InputFile.Close
或者(更好)像这样:
Set InputFile = fso.OpenTextFile("C:\xVBS Scripts\Computers.txt")
For Each strComputer In Split(InputFile.ReadAll, vbNewLine)
...
Next
InputFile.Close
Set InputFile = fso.OpenTextFile("C:\xVBS Scripts\Computers.txt")
Do Until InputFile.AtEndOfStream
strComputer = InputFile.ReadLine
...
Loop
InputFile.Close
这段代码到底出了什么问题?我不知道如何让它从文本文件中读取一行以上的内容。我知道我需要使用一个循环,但我不确定应该将哪种类型的代码封装到循环中。我是一名初级编剧,我在不断学习。