Board logo

标题: [文本处理] 求助VBS或BAT局域网电脑改名,硬件扫描,并上传至共享盘 [打印本页]

作者: hgleader    时间: 2021-2-2 08:28     标题: 求助VBS或BAT局域网电脑改名,硬件扫描,并上传至共享盘

具体求教要求:
1、运行VBS或BAT 弹出输入电脑名称框,更改电脑名称。(修改电脑名称最好可以不重启完成修改,不行的话,此功能可以单独分开)
2、网上找的可以获取电脑硬件的VBS,可以直接使用:
  1. On Error Resume Next
  2. Temp = 0
  3. Set WSHNetwork = WScript.CreateObject("WScript.Network")
  4. ComputerName = WSHNetwork.ComputerName
  5. TempFiles = desktop & ComputerName &".txt"
  6. Set FSO = CreateObject("Scripting.FileSystemObject")
  7. Set TempFile = FSO.CreateTextFile(TempFiles)
  8. strComputer = "."
  9. Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  10. Set CPU = objWMIService.ExecQuery("Select * From Win32_Processor")
  11. For Each Item In CPU
  12.     CPU2 = "CPU:" & Item.Name
  13. Next
  14. Set Board = objWMIService.ExecQuery("Select * From Win32_BaseBoard")
  15. For Each Item In Board
  16.     Board2 = "主板: " & Item.Product
  17. Next
  18. Set colItems = objWMIService.ExecQuery("Select * From Win32_PhysicalMemory",,48)
  19. For Each objItem In colItems
  20.     A = objItem.Capacity/1048576
  21.     Temp = temp+objItem.Capacity
  22.     N = N+1
  23. Next
  24. Memory = Temp/1048576
  25. If N = 1 Then
  26.     Memory2 = "内存: " & N & "条" & A & "M"
  27. Else
  28.     Memory2 = "内存: " & N & "条" & A & "M" &" 总计" & Memory & "M"
  29. End If
  30. Set Video = objWMIService.ExecQuery("Select * From Win32_VideoController",,48)
  31. For Each Item In Video
  32.     Video2 = "显卡: " & Item.Description
  33.     Video3 = "分辨率: " & Item.VideoModeDescription
  34. Next
  35. Set VideoA = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor",,48)
  36. For Each Item In VideoA
  37.     VideoA = "显示器名称: " & Item.Description
  38.     VideoB = "屏幕高度: " & Item.ScreenHeight & "   屏幕宽度: " & Item.ScreenWidth
  39. Next
  40. Set Disk = objWMIService.ExecQuery("Select * From Win32_DiskDrive")
  41. For Each Item In Disk
  42.     Disk2 = Disk2 & Item.Model
  43.     Disk4 = Int(Item.Size/1000/1000/1000) & "G"
  44. Next
  45. Disk3 = "硬盘: " & Trim(Disk2)
  46. Disk5 = "硬盘容量: " & Disk4
  47. Set Network = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
  48. For Each Item In Network
  49.     Lan = "网卡: " & Item.Description
  50.     IP = "IP地址: " & Item.IPAddress(0)
  51.     MacAdd = "MAC地址:" & Item.MACAddress
  52. Next
  53. TempFile.WriteLine(CPU2)
  54. TempFile.WriteLine(Board2)
  55. TempFile.WriteLine(Memory2)
  56. TempFile.WriteLine(Video2)
  57. TempFile.WriteLine(Video3)
  58. TempFile.WriteLine(VideoA)
  59. TempFile.WriteLine(VideoB)
  60. TempFile.WriteLine(Disk3)
  61. TempFile.WriteLine(Disk5)
  62. TempFile.WriteLine(Lan)
  63. TempFile.WriteLine(Ip)
  64. TempFile.WriteLine(MacAdd)
  65. TempFile.Close
复制代码
3、以电脑名称保存TXT配置文件在软件目录,并复制一份文件至共享盘\\192.168.8.30\XINXIBU     谢谢!
非常恳请哪位大神能帮忙处理,谢谢!
作者: hgleader    时间: 2021-2-2 08:29

非常恳请哪位大神能帮忙处理,谢谢!
作者: flashercs    时间: 2021-2-2 17:43

本帖最后由 flashercs 于 2021-2-5 10:10 编辑
  1. ' 重命名当前计算机,并获取当前系统硬件信息,保存到 %ComputerName%.txt
  2. On Error Resume Next
  3. RunasAdmin
  4. pcName = InputBox("请输入新计算机名:","重命名PC")
  5. ' WScript.Echo pcName
  6. ' WScript.Quit
  7. renameResult = RenamePC(pcName)
  8. Dim fso
  9. Set fso = CreateObject("Scripting.FileSystemObject")
  10. Temp = 0
  11. Set WSHNetwork = WScript.CreateObject("WScript.Network")
  12. If renameResult Then
  13.   ComputerName = pcName
  14. Else
  15.   ComputerName = WSHNetwork.ComputerName
  16. End If
  17. TempFiles = fso.GetParentFolderName(WScript.ScriptFullName) & "\" & ComputerName & ".txt"
  18. Set TempFile = fso.CreateTextFile(TempFiles)
  19. strComputer = "."
  20. Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  21. Set CPU = objWMIService.ExecQuery("Select * From Win32_Processor")
  22. For Each Item In CPU
  23.     CPU2 = "CPU:" & Item.Name
  24. Next
  25. Set Board = objWMIService.ExecQuery("Select * From Win32_BaseBoard")
  26. For Each Item In Board
  27.     Board2 = "主板: " & Item.Product
  28. Next
  29. Set colItems = objWMIService.ExecQuery("Select * From Win32_PhysicalMemory",,48)
  30. For Each objItem In colItems
  31.     A = objItem.Capacity/1048576
  32.     Temp = temp+objItem.Capacity
  33.     N = N+1
  34. Next
  35. Memory = Temp/1048576
  36. If N = 1 Then
  37.     Memory2 = "内存: " & N & "条" & A & "M"
  38. Else
  39.     Memory2 = "内存: " & N & "条" & A & "M" &" 总计" & Memory & "M"
  40. End If
  41. Set Video = objWMIService.ExecQuery("Select * From Win32_VideoController",,48)
  42. For Each Item In Video
  43.     Video2 = "显卡: " & Item.Description
  44.     Video3 = "分辨率: " & Item.VideoModeDescription
  45. Next
  46. Set VideoA = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor",,48)
  47. For Each Item In VideoA
  48.     VideoA = "显示器名称: " & Item.Description
  49.     VideoB = "屏幕高度: " & Item.ScreenHeight & "   屏幕宽度: " & Item.ScreenWidth
  50. Next
  51. Set Disk = objWMIService.ExecQuery("Select * From Win32_DiskDrive")
  52. For Each Item In Disk
  53.     Disk2 = Disk2 & Item.Model
  54.     Disk4 = Int(Item.Size/1000/1000/1000) & "G"
  55. Next
  56. Disk3 = "硬盘: " & Trim(Disk2)
  57. Disk5 = "硬盘容量: " & Disk4
  58. Set Network = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
  59. For Each Item In Network
  60.     Lan = "网卡: " & Item.Description
  61.     IP = "IP地址: " & Item.IPAddress(0)
  62.     MacAdd = "MAC地址:" & Item.MACAddress
  63. Next
  64. TempFile.WriteLine(CPU2)
  65. TempFile.WriteLine(Board2)
  66. TempFile.WriteLine(Memory2)
  67. TempFile.WriteLine(Video2)
  68. TempFile.WriteLine(Video3)
  69. TempFile.WriteLine(VideoA)
  70. TempFile.WriteLine(VideoB)
  71. TempFile.WriteLine(Disk3)
  72. TempFile.WriteLine(Disk5)
  73. TempFile.WriteLine(Lan)
  74. TempFile.WriteLine(Ip)
  75. TempFile.WriteLine(MacAdd)
  76. TempFile.Close
  77. fso.CopyFile TempFiles,"\\192.168.8.30\XINXIBU\",true
  78. Set fso = Nothing
  79. Set objWMIService = Nothing
  80. Set WSHNetwork = Nothing
  81. Sub RunasAdmin()
  82.   On Error Resume Next
  83.   If Not IsVista(".") Then Exit Sub
  84.   Dim wshell
  85.   Set wshell = CreateObject("WScript.Shell")
  86.   wshell.RegRead "HKEY_USERS\S-1-5-19\Environment\TEMP"
  87.   If Err.Number <> 0 Then
  88.     Dim str,arg,shell
  89.     str = ""
  90.     Set shell = CreateObject("Shell.Application")
  91.     For Each arg In WScript.Arguments
  92.       str = str & " """ & arg & """"
  93.     Next
  94.     shell.ShellExecute WScript.FullName,"//nologo """ & WScript.ScriptFullName & """ " & str, "", "runas", 1
  95.     Set shell = Nothing  
  96.     Set wshell = Nothing
  97.     WScript.Quit
  98.   Else
  99.     Set wshell = Nothing
  100.   End If
  101. End Sub
  102. Function IsVista(strComputer)
  103.   On Error Resume Next
  104.   IsVista = False
  105.   Dim objWMIService, colOperationSystems, objOperationSystem
  106.   Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  107.   Set colOperationSystems = objWMIService.ExecQuery("Select Version from Win32_OperatingSystem")
  108.   For Each objOperationSystem In colOperationSystems
  109.     If CInt(Left(objOperationSystem.Version, InStr(1,objOperationSystem.Version,".") - 1)) > 5 Then
  110.       IsVista = True
  111.       Exit For
  112.     End If
  113.   Next
  114.   Set colOperationSystems = Nothing
  115.   Set objWMIService = Nothing
  116. End Function
  117. Function RenamePC(pcnameNew)
  118.   ' 重命名当前计算机
  119.   RenamePC = False
  120.   On Error Resume Next
  121.   With New RegExp
  122.     .Global = True
  123.     .Ignorecase = True
  124.     .Multiline = False
  125.     .Pattern = "[{|}~\[\]\\^':;<=>?@!""#$%`()+/.,*&\s\uD800-\uDFFF]"
  126.     If .Test(pcnameNew) Then
  127.       WScript.Echo "计算机名含有非法字符"
  128.       Exit Function
  129.     End If
  130.   End With
  131.   If Len(pcnameNew) > 15 Or Len(pcnameNew) = 0 Then
  132.     WScript.Echo "计算机名长度非法"
  133.     Exit Function
  134.   End If
  135.   Dim wshell,strCmd,oExec
  136.   Set wshell = CreateObject("WScript.Shell")
  137.   strCmd = "cmd.exe /c reg add ""HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam"" /ve /t REG_SZ /d """ & pcnameNew & """ /f&" &_
  138.             "reg add ""HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName"" /v ""ComputerName"" /t REG_SZ /d """ & pcnameNew & """ /f&" &_
  139.             "reg add ""HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName"" /v ""ComputerName"" /t REG_SZ /d """ & pcnameNew & """ /f&" &_
  140.             "reg add ""HKLM\SYSTEM\CurrentControlSet\Services\Eventlog"" /v ""ComputerName"" /t REG_SZ /d """ & pcnameNew & """ /f&" &_
  141.             "reg add ""HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"" /v ""NV Hostname"" /t REG_SZ /d """ & pcnameNew & """ /f&" &_
  142.             "reg add ""HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"" /v ""Hostname"" /t REG_SZ /d """ & pcnameNew & """ /f"
  143.   Set oExec = wshell.Exec(strCmd)
  144.   ' If Not oExec.StdOut.AtEndOfStream Then
  145.   '   WScript.Echo oExec.StdOut.ReadAll
  146.   ' End If
  147.   Do While oExec.Status = 0
  148.      WScript.Sleep 100
  149.   Loop
  150.   If oExec.ExitCode <> 0 Then
  151.     WScript.Echo "计算机重命名失败: " & oExec.ExitCode
  152.   Else
  153.     RenamePC = True
  154.   End If
  155.   Set oExec = Nothing
  156.   Set wshell = Nothing
  157. End Function
复制代码

作者: hgleader    时间: 2021-2-5 09:10

您好,非常感谢您帮忙回复,存为vbs运行保存如下,恳请帮忙再看看,谢谢




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2