从文本文件VBS中读取内容

时间:2016-05-02 10:51:12

标签: vbscript

嗨大家这个脚本要求您手动输入计算机名称以提取库存。

如果我可以在文本文件中包含所有计算机的名称,那就太棒了。

我试过像

这样的东西
strComputer  = fso1.OpenTextFile("c:\Computers.txt",1) 

但似乎没有任何想法?

Dim strIP, strSubnet, strDescription, lnX, strcomputer, objwmiservice, colitems, objitems

strComputer  =  InputBox ("Enter the Computer name to get its Inventory:-")
lnX = 1

' ********************************************************************************************
'Section to change a filename using timestamps
strPath  =  "C:\"   
strMonth  =  DatePart("m", Now())
strDay  =  DatePart("d",Now())

if Len(strMonth) = 1 then
 strMonth  =  "0" & strMonth
else
 strMonth  =  strMonth
end if

if Len(strDay) = 1 then
 strDay  =  "0" & strDay
else
 strDay  =  strDay
end if

strFileName  =  DatePart("yyyy",Now()) & strMonth & strDay 
strFileName  =  Replace(strFileName,":","")
' ********************************************************************************************

'Variable Declarations
Const ForAppending  =  8

'Get CompName
Set objWMIService  =  GetObject("winmgmts:" & "{impersonationLevel = impersonate}!\\" & strComputer & "\root\cimv2")

' ********************************************************************************************
'Get Operation System & Processor Information
' ********************************************************************************************
Set colItems  =  objWMIService.ExecQuery("Select * from Win32_Processor")
For Each objItem in colItems
     CompName  =  objItem.SystemName
Next

Set objFSO  =  CreateObject("Scripting.FileSystemObject")
if objFSO.FileExists(strPath & CompName & "_" & "_Inventory.txt") then
 WScript.Quit
end if

'Set the file location 
Set objFSO  =  CreateObject("Scripting.FileSystemObject")
Set objTextFile  =  objFSO.OpenTextFile(strPath & CompName & "_" & "Inventory.txt", ForAppending, True)

' ********************************************************************************************
'Print HEADER
' ********************************************************************************************
objTextFile.Write "********************************************************************************************" & VBCRLF & VBCRLF
objTextFile.Write "                     COMPUTER INVENTORY                         " & VBCRLF
objTextFile.Write "                     DATE:  " &    FormatDateTime(Now(),1)   & "                   " & VBCRLF
objTextFile.Write "                     TIME:  " &    FormatDateTime(Now(),3)   & "                   " & VBCRLF & VBCRLF
objTextFile.Write "******************************************************************************************** " & VBCRLF & VBCRLF 

' ********************************************************************************************
'Get Processor Information
' ********************************************************************************************
objTextFile.Write "COMPUTER" & VBCRLF 
Set colItems  =  objWMIService.ExecQuery("Select * from Win32_Processor")
For Each objItem in colItems
    If lnX = 1 Then
     objTextFile.Write "          COMPUTER NAME: " &  objItem.SystemName & VBCRLF
     objTextFile.Write "          PROCESSOR: " &  objItem.Name & vbCrLf
     lnX = lnX + 1
    End If
Next

' ********************************************************************************************
'Get Computer Manufacturer and RAM details
' ********************************************************************************************
Set colSystems  =  objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each objSystems In colSystems
  objTextFile.WriteLine "          MANUFACTURER: " & objSystems.Manufacturer
  objTextFile.WriteLine "          MODEL: " & objSystems.Model
  objTextFile.WriteLine "          DOMAIN: " & objsystems.Domain
  objTextFile.Write "          RAM: " & Round (objSystems.TotalPhysicalMemory / 1048576, 0) & " MB "& VBCRLF 
Next

' ********************************************************************************************
' Get the Serial number/ Service Tag of the system
' ********************************************************************************************
Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure") 
For Each objSMBIOS in colSMBIOS 
    objTextFile.Write "          SERVICE TAG: " & objSMBIOS.SerialNumber & VBCRLF
Next 

' ********************************************************************************************
'Get BIOS Information
' ********************************************************************************************
objTextFile.Write VBCRLF & "BIOS INFO" & vbCrLf
Set colBIOS  =  objWMIService.ExecQuery("Select * from Win32_BIOS")
For each objItem in colBIOS
    objTextFile.WriteLine "          BIOS MANUFACTURER: " & objItem.Manufacturer
    objTextFile.WriteLine "          BIOS VERSION: " & objItem.Name
Next

' ********************************************************************************************
'Get OS Information
' ********************************************************************************************
objTextFile.Write VBCRLF & "OS INFO" & vbCrLf
Set colSettings  =  objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colSettings
    objTextFile.Write "          OPERATING SYSTEM: " & objOperatingSystem.Caption & " {Enter the product key}" & VBCRLF
    objTextFile.Write "          SERVICE PACK: " & objOperatingSystem.Caption & " Service Pack "& objOperatingSystem.ServicePackMajorVersion & "." & objOperatingSystem.ServicePackMinorVersion & VBCRLF 
Next



' ********************************************************************************************
'Get Logical Disk Size and Partition Information
' ********************************************************************************************
objTextFile.Write VBCRLF & "MEMORY" & VBCRLF 
Set colDisks  =  objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType  =  3")
For Each objDisk in colDisks
    intFreeSpace  =  objDisk.FreeSpace
    intTotalSpace  =  objDisk.Size
    pctFreeSpace  =  intFreeSpace / intTotalSpace
    objTextFile.Write objDisk.Name & "\ (" & objDisk.FileSystem & ") " &  Round((objDisk.Size/1000000000),4) & " GB ("& Round((intFreeSpace/1000000000)*1.024,4) & " GB Free Space)" & VBCRLF
Next

' ********************************************************************************************
'Get NETWORK ADAPTERS information
' ********************************************************************************************
objTextFile.Write VBCRLF & "NETWORK" & VBCRLF 
Set colNicConfigs  =  objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled  =  True")
For Each objNicConfig In colNicConfigs
    strDescription = objNicConfig.Caption
    strMACAddress = objNicConfig.MACAddress
    strDHCP = objNicConfig.DHCPEnabled

    For Each strIPAddress In objNicConfig.IPAddress
        strIP = strIPAddress        'Assign IP Address to variable
        For Each strIPSubnet In objNicConfig.IPSubnet
            strSubnet  =  strIPSubnet       'Assign Subnet to variable
        Next
        objTextFile.Write "          NETWORK ADAPTER: " & strDescription & VBCRLF
        objTextFile.Write "          IP ADDRESS: " & strIP & VBCRLF
        objTextFile.Write "          MAC ADDRESS: " & strMACAddress & vbCrLf
        objTextFile.Write "          DHCP ENABLED: " & strDHCP & vbCrLf & vbCrLf 
    Next

Next
Set colNicConfigs  = NOTHING

' ********************************************************************************************
'Get GRAPHICS ADAPTERS information
' ********************************************************************************************
objTextFile.Write VBCRLF & "GRAPHICS CARD" & VBCRLF 
Set colGraphics  =  objWMIService.ExecQuery ("Select * from Win32_DisplayControllerConfiguration")
For Each objGraphics in colGraphics
    objTextFile.WriteLine "          GRAPHICS CARD: " & objGraphics.Name & VBCRLF 
Next

' ********************************************************************************************
' Get the list of Installed software
' ********************************************************************************************
objTextFile.Write VBCRLF & "SOFTWARE INSTALLED" & VBCRLF 
Set colSoftware  =  objWMIService.ExecQuery("Select * from Win32_Product")
For Each objSoftware in colSoftware
    objTextFile.WriteLine objSoftware.Description 
Next

'Close text file after writing logs
objTextFile.Write VbCrLf
objTextFile.Close

'Clean Up
WScript.Echo "Inventory Complete "

1 个答案:

答案 0 :(得分:1)

中的.OpenTextFile()调用
strComputer = fso1.OpenTextFile("c:\Computers.txt",1)

只打开文件,但不会读取应分配给strComputer的文件内容。所以使用

strComputer = fso1.OpenTextFile("c:\Computers.txt").ReadLine()