尝试运行多个脚本,将它们全部输出到一个文件,并且所有脚本都使用相同的IP地址

时间:2013-04-09 18:38:34

标签: vbscript

我有几个问题,希望这是正确的地方。

基本上我想做的是能够远程获取有关域计算机的信息。

我有3个单独的脚本给我1(IP配置,comp名称...),2(已安装的软件)和3(映射驱动器)。

前两个请求IP /计算机名称,第三个我必须将其输入脚本...我只想输入一次IP地址并使其适用于所有3

其次我希望输入此信息的输出文件被命名为已安装的软件脚本,然后只需要另外两个脚本添加(修改)已经创建的输出。

我是vbs的新手,所以任何帮助都会很棒

SCRIPT 1(获取IP配置)

dim strComputer 'for computer name or IP
dim colAdapters 'collection of adapters


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("output.txt", True)


strComputer = ""

'open a dialog box asking for the computer name/IP
do
strComputer = inputbox( "Please enter a computername/IP, or . for local computer", "Input" )
Loop until strComputer <> "" 'run until a name/IP is entered


Set objWMIService = GetObject ("winmgmts:" & "!\\" & strComputer & "\root\cimv2") 'open the WMI service on the remote PC
Set colAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True") 

'go through the list of adapters and gather data
For Each objAdapter in colAdapters 


objFile.Writeline "Host name: " & objAdapter.DNSHostName 
objFile.Writeline "DNS domain: " & objAdapter.DNSDomain 
objFile.Writeline "DNS suffix search list: " & objAdapter.DNSDomainSuffixSearchOrder 
objFile.Writeline "Description: " & objAdapter.Description 
objFile.Writeline "Physical address: " & objAdapter.MACAddress 
objFile.Writeline "DHCP enabled: " & objAdapter.DHCPEnabled 
If Not IsNull(objAdapter.IPAddress) Then 
For i = LBound(objAdapter.IPAddress) To UBound(objAdapter.IPAddress) 
objFile.Writeline "IP address: " & objAdapter.IPAddress(i) 
Next 
End If 
If Not IsNull(objAdapter.IPSubnet) Then 
For i = LBound(objAdapter.IPSubnet) To UBound(objAdapter.IPSubnet) 
objFile.Writeline "Subnet: " & objAdapter.IPSubnet(i) 
Next 
End If 
If Not IsNull(objAdapter.DefaultIPGateway) Then 
For i = LBound(objAdapter.DefaultIPGateway) To UBound(objAdapter.DefaultIPGateway) 
objFile.Writeline "Default gateway: " & objAdapter.DefaultIPGateway(i) 
Next 
End If 
objFile.Writeline "DHCP server: " & objAdapter.DHCPServer 
If Not IsNull(objAdapter.DNSServerSearchOrder) Then 
For i = LBound(objAdapter.DNSServerSearchOrder) To UBound(objAdapter.DNSServerSearchOrder) 
objFile.Writeline "DNS server: " & objAdapter.DNSServerSearchOrder(i) 
Next 
End If 
objFile.Writeline "Primary WINS server: " & objAdapter.WINSPrimaryServer 
objFile.Writeline "Secondary WINS server: " & objAdapter.WINSSecondaryServer 
objFile.Writeline "Lease obtained: " & objAdapter.DHCPLeaseObtained 
objFile.Writeline "Lease expires: " & objAdapter.DHCPLeaseExpires 
Next 

SCRIPT 2(获得已安装的软件)

Option Explicit

Dim sTitle
sTitle = "InstalledPrograms.vbs by Bill James"
Dim StrComputer
strComputer = InputBox("Enter I.P. or name of computer to check for " & _
                       "installed software (leave blank to check " & _
                       "local system)." & vbcrlf & vbcrlf & "Remote " & _
                       "checking only from NT type OS to NT type OS " & _
                       "with same Admin level UID & PW", sTitle)
If IsEmpty(strComputer) Then WScript.Quit
strComputer = Trim(strComputer)
If strComputer = "" Then strComputer = "."

'Wscript.Echo GetAddRemove(strComputer)

Dim sCompName : sCompName = GetProbedID(StrComputer)

Dim sFileName
sFileName = sCompName & "_" & GetDTFileName() & "_Software.txt"

Dim s : s = GetAddRemove(strComputer)

If WriteFile(s, sFileName) Then
  'optional prompt for display
  If MsgBox("Finished processing.  Results saved to " & sFileName & _
            vbcrlf & vbcrlf & "Do you want to view the results now?", _
            4 + 32, sTitle) = 6 Then
    WScript.CreateObject("WScript.Shell").Run sFileName, 9
  End If
End If

Function GetAddRemove(sComp)
  'Function credit to Torgeir Bakken
  Dim cnt, oReg, sBaseKey, iRC, aSubKeys
  Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
  Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
              sComp & "/root/default:StdRegProv")
  sBaseKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
  iRC = oReg.EnumKey(HKLM, sBaseKey, aSubKeys)

  Dim sKey, sValue, sTmp, sVersion, sDateValue, sYr, sMth, sDay

  For Each sKey In aSubKeys
    iRC = oReg.GetStringValue(HKLM, sBaseKey & sKey, "DisplayName", sValue)
    If iRC <> 0 Then
      oReg.GetStringValue HKLM, sBaseKey & sKey, "QuietDisplayName", sValue
    End If
    If sValue <> "" Then
      iRC = oReg.GetStringValue(HKLM, sBaseKey & sKey, _
                                "DisplayVersion", sVersion)
      If sVersion <> "" Then
        sValue = sValue & vbTab & "Ver: " & sVersion
      Else
        sValue = sValue & vbTab 
      End If
      iRC = oReg.GetStringValue(HKLM, sBaseKey & sKey, _
                                "InstallDate", sDateValue)
      If sDateValue <> "" Then
        sYr =  Left(sDateValue, 4)
        sMth = Mid(sDateValue, 5, 2)
        sDay = Right(sDateValue, 2)
        'some Registry entries have improper date format
        On Error Resume Next 
        sDateValue = DateSerial(sYr, sMth, sDay)
        On Error GoTo 0
        If sdateValue <> "" Then
          sValue = sValue & vbTab & "Installed: " & sDateValue
        End If
      End If
      sTmp = sTmp & sValue & vbcrlf
    cnt = cnt + 1
    End If
  Next
  sTmp = BubbleSort(sTmp)
  GetAddRemove = "INSTALLED SOFTWARE (" & cnt & ") - " & sCompName & _
                 " - " & Now() & vbcrlf & vbcrlf & sTmp 
End Function

Function BubbleSort(sTmp)
  'cheapo bubble sort
  Dim aTmp, i, j, temp
  aTmp = Split(sTmp, vbcrlf)  
  For i = UBound(aTmp) - 1 To 0 Step -1
    For j = 0 to i - 1
      If LCase(aTmp(j)) > LCase(aTmp(j+1)) Then
        temp = aTmp(j + 1)
        aTmp(j + 1) = aTmp(j)
        aTmp(j) = temp
      End if
    Next
  Next
  BubbleSort = Join(aTmp, vbcrlf)
End Function

Function GetProbedID(sComp)
  Dim objWMIService, colItems, objItem
  Set objWMIService = GetObject("winmgmts:\\" & sComp & "\root\cimv2")
  Set colItems = objWMIService.ExecQuery("Select SystemName from " & _
                                         "Win32_NetworkAdapter",,48)
  For Each objItem in colItems
    GetProbedID = objItem.SystemName
  Next
End Function

Function GetDTFileName()
  dim sNow, sMth, sDay, sYr, sHr, sMin, sSec
  sNow = Now
  sMth = Right("0" & Month(sNow), 2)
  sDay = Right("0" & Day(sNow), 2)
  sYr = Right("00" & Year(sNow), 4)
  sHr = Right("0" & Hour(sNow), 2)
  sMin = Right("0" & Minute(sNow), 2)
  sSec = Right("0" & Second(sNow), 2)
  GetDTFileName = sMth & sDay & sYr & "_" & sHr & sMin & sSec
End Function

Function WriteFile(sData, sFileName)
  Dim fso, OutFile, bWrite
  bWrite = True
  Set fso = CreateObject("Scripting.FileSystemObject")
  On Error Resume Next
  Set OutFile = fso.OpenTextFile(sFileName, 2, True)
  'Possibly need a prompt to close the file and one recursion attempt.
  If Err = 70 Then
    Wscript.Echo "Could not write to file " & sFileName & ", results " & _
                 "not saved." & vbcrlf & vbcrlf & "This is probably " & _
                 "because the file is already open."
    bWrite = False
  ElseIf Err Then
    WScript.Echo err & vbcrlf & err.description
    bWrite = False
  End If
  On Error GoTo 0
  If bWrite Then
    OutFile.WriteLine(sData)
    OutFile.Close
  End If
  Set fso = Nothing
  Set OutFile = Nothing
  WriteFile = bWrite
End Function

SCRIPT 3(获取映射驱动器)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("mappedoutput.txt", True)
' List Mapped Network Drives


On Error Resume Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_MappedLogicalDisk")

For Each objItem in colItems
    objFile.Writeline "Compressed: " & objItem.Compressed
    objFile.Writeline "Description: " & objItem.Description
    objFile.Writeline "Device ID: " & objItem.DeviceID
    objFile.Writeline "File System: " & objItem.FileSystem
    objFile.Writeline "Free Space: " & objItem.FreeSpace
    objFile.Writeline "Maximum Component Length: " & objItem.MaximumComponentLength
    objFile.Writeline "Name: " & objItem.Name
    objFile.Writeline "Provider Name: " & objItem.ProviderName
    objFile.Writeline "Session ID: " & objItem.SessionID
    objFile.Writeline "Size: " & objItem.Size
    objFile.Writeline "Supports Disk Quotas: " & objItem.SupportsDiskQuotas
    objFile.Writeline "Supports File-Based Compression: " & _
        objItem.SupportsFileBasedCompression
    objFile.Writeline "Volume Name: " & objItem.VolumeName
    objFile.Writeline "Volume Serial Number: " & objItem.VolumeSerialNumber
    objFile.Writeline
Next

再次感谢你

1 个答案:

答案 0 :(得分:2)

你能把所有三个脚本都放在一个单独的脚本中吗?在这种情况下,您只需输入一次IP地址。

或者编写另一个脚本,它将询问IP地址并使用cscript调用这些脚本并将IPaddress作为参数传递给它们。试试这个代码:

strcomputer = inputbox("Enter the IP address")
set obj1 = createobject("wscript.shell")
set obj2 = createobject("wscript.shell")
set obj3 = createobject("wscript.shell")
pgm1 = "cscript script1.vbs " & strcomputer
pgm2 = "cscript script2.vbs " & strcomputer
pgm3 = "cscript script3.vbs " & strcomputer
obj1.run pgm1,3,true
obj2.run pgm2,3,true
obj3.run pgm3,3,true
set obj1 = nothing
set obj2 = nothing
set obj3 = nothing

在上面的代码中,script1.vbs,script2.vbs,script3.vbs是你的3个脚本,你将使用新脚本逐个执行它们。 在script1.vbs中,添加以下代码行:

strcomputer = wscript.Arguments.item(0)

它会将您从新脚本传递的第一个参数存储到script1.vbs,并存储到变量'strcomputer'(在您的情况下为IP地址)。 同样,在script2.vbs和script3.vbs中也添加语句

strcomputer = wscript.Arguments.item(0)

关于你的输出文件,我不确定你要求的是什么。也许这可以提供帮助:

使用以下内容写入文件(如果数据已存在则覆盖):

Set fso1 = CreateObject("Scripting.FileSystemObject" )            
Set file1 = fso1.OpenTextFile("C:\New\textfile1.txt",2,true)

使用以下内容添加数据或附加到文件(不会覆盖):

Set fso1 = CreateObject("Scripting.FileSystemObject" )            
Set file1 = fso1.OpenTextFile("C:\New\textfile1.txt",8,true)

使用以下内容从文件中读取:

Set fso1 = CreateObject("Scripting.FileSystemObject" )            
Set file1 = fso1.OpenTextFile("C:\New\textfile1.txt",1,true)