本帖最后由 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
复制代码
|