VBScript-多个问题
所以我被扔进了鲨鱼池的最深处,连手臂都没有,我不知道怎么游泳(翻译-我不知道VBS) 所以我发现我自己在这里,因为我一直用我的两根棍子敲击我的脚本,它仍然不起作用。当我解决一个问题时,会出现另一个问题,而当我解决这个问题时,另一个问题会返回(感觉就像我在追逐自己的尾巴) 下面是我的代码的最新迭代(我一直在移动垃圾,认为它可能神奇地工作) 如果我疯狂的管道录音脚本毫无意义,请不要感到震惊。我被困在一个山洞里,用两根棍子敲击键盘,这就是我想到的结果。对穴居人来说不算太坏,但它仍然不起作用 任何帮助、帮助、建议、评论、笑话、挖苦、咆哮都将不胜感激。任何拖拉都会很快被一根大木槌打到头上 谢谢,VBScript-多个问题,vbscript,Vbscript,所以我被扔进了鲨鱼池的最深处,连手臂都没有,我不知道怎么游泳(翻译-我不知道VBS) 所以我发现我自己在这里,因为我一直用我的两根棍子敲击我的脚本,它仍然不起作用。当我解决一个问题时,会出现另一个问题,而当我解决这个问题时,另一个问题会返回(感觉就像我在追逐自己的尾巴) 下面是我的代码的最新迭代(我一直在移动垃圾,认为它可能神奇地工作) 如果我疯狂的管道录音脚本毫无意义,请不要感到震惊。我被困在一个山洞里,用两根棍子敲击键盘,这就是我想到的结果。对穴居人来说不算太坏,但它仍然不起作用 任何帮助、
埃德·麦地那(Ed Medina)瞧,密码之神已经照亮了我的道路,并为我提供了答案 无论如何,感谢大克里斯和罗亚普先生的提问。我还要感谢学院,我所有的同事,我的妈妈,咖啡,邮递员,我的妻子,我的猫和所有的小人物 下面是一段代码,它将从临时文件夹中的文件(文件名为Computers.txt)中读取,然后针对该文件运行并测试,以查找域(网络)中该计算机中的所有打印机端口 输出是一个简单的回声输出,在窗口中提供给您。我只是在黑暗、臭气熏天的山洞里用两根棍子不停地敲键盘,然后代码就出来了 唯一需要注意的是,如果您有一个错误的计算机名或一台计算机关闭,脚本将在这一点上失败,并且不会继续(是的,是的,正在处理它) 多谢各位。
顺便说一句,如果有人知道如何抛出一个错误,并继续工作,请随时发布。再次感谢各位。您的脚本打算实现什么目标?这可能会帮助我们得到一个有效的答案…嘿,大克里斯-谢谢你的问题。所以脚本应该从一个文本文件读取到一个数组中,然后从数组中读取(计算机名),然后运行命令列出所有打印机端口(正在使用和未使用)。您好。那么什么不起作用呢?它做错了什么?嘿Roryap-这取决于我做了什么以及错误是什么。我得到以下错误。类型不匹配,应为语句(符合错误)。显然,我不知道我在做什么。我试图做的是从文件(文本)中读取并运行命令列出未使用的端口。我可以运行脚本从文件中读取数据,也可以运行脚本列出单个计算机的端口(在字符串中输入计算机名称)。但是当我尝试将两者结合起来时,我失败得很惨。我认为
objDictionary.RemoveAll
应该在每个objPrinters循环体的之外,否则将只存储最后打印的端口名
'---- Set Constant for Reading
Const ForReading = 1
'----- Define at the Variables for the scripts
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'------ Path for File below is Explicit (meaning you need to enter the complete path)
Set objTextFile = objFSO.OpenTextFile("c:\users\me\documents\Small- ComputerList.txt", ForReading)
'---- Begin Loop for reading the Array
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")
' ------- strComputer = "usms-w-ksd68598" Commented out from original script
' ------- Reading from the Array
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & arrServiceList(0) & "\root\cimv2")
' ------- Running the Command to find all the printers
Set colPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colPrinters
objDictionary.RemoveAll
objDictionary.Add objPrinter.PortName, objPrinter.PortName
Next
' ------ Running the Command to find all the TCP/IP Printer Ports
Set colPorts = objWMIService.ExecQuery _
("Select * from Win32_TCPIPPrinterPort")
For Each objPort in colPorts
If objDictionary.Exists(objPort.Name) Then
strUsedPorts = strUsedPorts & _
objDictionary.Item(objPort.Name) & VbCrLf
Else
strFreePorts = strFreePorts & objPort.Name & vbCrLf
End If
Next
'----- Printing out the Results to the screen
For i = 1 to Ubound(arrServiceList)
& arrServiceList(i)
Next
Loop
Wscript.Echo "System Name: " & arrServiceList(0)
Wscript.Echo "The following ports are in use for: " & VbCrLf & strUsedPorts
Wscript.Echo "The following ports are not used for: " & VbCrLf & strFreePorts
'---- Set Constant for Reading
Const ForReading = 1
'----- Define at the Variables for the scripts
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'------ Path for File below is Explicit (meaning you need to enter the complete path)
Set objFile = objFSO.OpenTextFile("c:\Temp\Computers.txt", ForReading)
'---- Begin Loop for reading the Array
Do Until objFile.AtEndOfStream
strComputer = objFile.ReadLine
' ------- Reading from the Array
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' ------- Running the Command to find all the printers
Set colPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colPrinters
objDictionary.RemoveAll
objDictionary.Add objPrinter.PortName, objPrinter.PortName
Next
' ------ Running the Command to find all the TCP/IP Printer Ports
Set colPorts = objWMIService.ExecQuery _
("Select * from Win32_TCPIPPrinterPort")
For Each objPort in colPorts
If objDictionary.Exists(objPort.Name) Then
strUsedPorts = strUsedPorts & _
objDictionary.Item(objPort.Name) & VbCrLf
Else
strFreePorts = strFreePorts & objPort.Name & vbCrLf
End If
Next
'---- Output to Screen
Wscript.Echo "System Name: " & strComputer
Wscript.Echo "The following ports are in use for: " & VbCrLf & strUsedPorts
Wscript.Echo "The following ports are not used for: " & VbCrLf & strFreePorts
Loop