标题: [技术讨论] vbs如何查询服务器上指定txt文本里是否有本机的计算机名和系统产品识别码,没有则添加 [打印本页]
作者: qqqkoko123 时间: 2017-5-1 17:28 标题: vbs如何查询服务器上指定txt文本里是否有本机的计算机名和系统产品识别码,没有则添加
本帖最后由 pcl_test 于 2017-5-1 18:43 编辑
求大神一起讨论vbs如何才能实现如下功能
vbs要实现如下功能:
1,获取本机序列号
2,查询服务器上的findbyname.txt文本,输出计算机名
3,如果findbyname.txt文本中不包含此计算机名和序列号就按“计算机名 序列号”的格式写入findbyname.txt文本中
本人写了如下代码,但是第2个功能无法正常实现,请大神看看究竟哪里出了问题,万分感谢!- Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
- Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_computersystemproduct")
-
- For Each objItem In colItems
- serialnumber = objItem.identifyingnumber
- Next
- strComputer = "."
- Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
- Set colComputers = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
- For Each objComputer in colComputers
- strComputerName = objComputer.Name
- Exit For
- Next
- MsgBox (strComputerName)
- MsgBox (serialnumber)
- Dim strFile
-
- strFile = "findbyname.txt"
- Dim fso, objFile, arr, strContents, blnFound
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set objFile = fso.OpenTextFile(strFile, 1)
- strContents = objFile.ReadAll
- blfind = False
- If InStr(strContents, serialnumber) Then
- blfind = True
- MsgBox ("找到主机名!")
- While Not objFile.AtEndOfStream
- arr = Split(objFile.ReadLine, " ")
- If UBound(arr) > 0 Then
- If arr(1) = serialnumber Then MsgBox arr(0)
- End If
- wend
- else
- MsgBox ("未找到主机名!")
- Set f = fso.OpenTextFile(strFile, 8)
- f.WriteLine (strComputerName & " " & serialnumber)
- End If
- objFile.Close()
- Set objFile = Nothing : Set fso = Nothing
复制代码
作者: pcl_test 时间: 2017-5-2 09:52
本帖最后由 pcl_test 于 2017-5-2 09:54 编辑
- Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
- strFile = "findbyname.txt"
-
- Set colItems = objWMIService.ExecQuery("Select * From Win32_ComputerSystemProduct")
- For Each objItem In colItems
- serialnumber = objItem.IdentifyingNumber
- Next
-
- Set colItems = objWMIService.ExecQuery("Select * From Win32_ComputerSystem")
- For Each objItem In colItems
- strComputerName = objItem.Name
- Next
-
- Set fso = CreateObject("Scripting.FileSystemObject")
- If Not fso.FileExists(strFile) Then
- fso.CreateTextFile(strFile).WriteLine strComputerName & " " & serialnumber
- Else
- If fso.GetFile(strFile).Size =0 Then
- fso.OpenTextFile(strFile, 2).WriteLine strComputerName & " " & serialnumber
- Else
- Set objFile = fso.OpenTextFile(strFile, 1)
- Do While objFile.AtEndOfStream<>true
- strLine = Split(Trim(objFile.ReadLine)," ")
- If LCase(strLine(0)) = LCase(strComputerName) Then
- flag = true
- Exit Do
- End If
- Loop
- objFile.Close
- If flag Then
- Msgbox "Exists!"
- Else
- fso.OpenTextFile(strFile, 8).WriteLine strComputerName & " " & serialnumber
- End If
- End If
- End If
复制代码
作者: qqqkoko123 时间: 2017-5-2 11:09
多谢大神指导,另外,经大神指导,用bat更加简单:- @echo off
- for /f %%a in ('wmic path Win32_computersystemproduct get identifyingnumber /value^|find "="') do set %%a
- find /i "%computername%" "findbyname.txt">nul && (echo 计算机名已找到!
- echo 计算机名是%computername%)||(echo 计算机名未找到!
- >>"findbyname.txt" echo %computername% %identifyingnumber%)
- pause
复制代码
作者: qqqkoko123 时间: 2017-5-2 11:19
另外,自己写的代码问题出在第27行,把第27行内容改为:- While objFile.AtEndOfStream<>true
复制代码
这样就可以了!
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |