【问题标题】:VB Script that needs to read from a text file需要从文本文件中读取的 VB 脚本
【发布时间】:2014-05-27 21:12:21
【问题描述】:

我有一个 VB 脚本,它发送到一串服务器并检查是否有任何未决的 Windows 更新。它完美地工作。唯一的问题是运行我必须输入的脚本: cscript pending.vbs server01 server02 server03 server04 等等。我有超过 300 台服务器要运行它。我需要能够使用服务器名称更新文本文件,而不是手动输入它们。我在下面发布脚本: 到目前为止,我还不是程序员,但我确实懂一些。

谢谢!

'#
'# ServerPendingUpdates.vbs
'#
'# Usage: cscript ServerPendingUpdates.vbs {servername} {servername} {servername} {servername}
'#    If no {servername} specified then 'localhost' assumed
'#
'# To do: Error handling
'#
Option Explicit
Dim strServer        : strServer         =  GetArgValue(0,"localhost")


'#
'# Loop through the input parameters for each server
'#
Dim i
For i = 0 To WScript.Arguments.Count - 1
    CheckServerUpdateStatus GetArgValue(i,"localhost") 'strServer
Next

WScript.Quit(0)

Function CheckServerUpdateStatus( ByVal strServer )

    WScript.Echo vbCRLF & "Connecting to " & strServer & " to check software update status..."

    Dim blnRebootRequired    : blnRebootRequired     = False
    Dim blnRebootPending    : blnRebootPending     = False
    Dim objSession        : Set objSession    = CreateObject("Microsoft.Update.Session", strServer)
    Dim objUpdateSearcher     : Set objUpdateSearcher    = objSession.CreateUpdateSearcher
    Dim objSearchResult    : Set objSearchResult     = objUpdateSearcher.Search(" IsAssigned=1 and IsHidden=0 and Type='Software'")

    '#
    '#
    '#
    Dim i, objUpdate
    Dim intPendingInstalls    : intPendingInstalls     = 0

    For i = 0 To objSearchResult.Updates.Count-1
        Set objUpdate = objSearchResult.Updates.Item(I) 

        If objUpdate.IsInstalled Then
            If objUpdate.RebootRequired Then
                blnRebootPending     = True
            End If
        Else
            intPendingInstalls    = intPendingInstalls + 1
            'If objUpdate.RebootRequired Then    '### This property is FALSE before installation and only set to TRUE after installation to indicate that this patch forced a reboot.
            If objUpdate.InstallationBehavior.RebootBehavior <> 0 Then
                '# http://msdn.microsoft.com/en-us/library/aa386064%28v=VS.85%29.aspx
                '# InstallationBehavior.RebootBehavior = 0    Never reboot
                '# InstallationBehavior.RebootBehavior = 1    Must reboot
                '# InstallationBehavior.RebootBehavior = 2    Can request reboot
                blnRebootRequired     = True
            End If

        End If
    Next

    WScript.Echo strServer & " has " & intPendingInstalls & " updates pending installation"

    If blnRebootRequired Then
        WScript.Echo strServer & " WILL need to be rebooted to complete the installation of these updates."
    Else
        WScript.Echo strServer & " WILL NOT require a reboot to install these updates."
    End If


    '#
    '#
    '#
    If blnRebootPending Then
        WScript.Echo strServer & " is waiting for a REBOOT to complete a previous installation."
    End If 
End Function



'#
'#
'#
Function GetArgValue( intArgItem, strDefault )
    If WScript.Arguments.Count > intArgItem Then
        GetArgValue = WScript.Arguments.Item(intArgItem)
    Else
        GetArgValue = strDefault
    End If
End Function

【问题讨论】:

标签: vbscript scripting


【解决方案1】:

您可以在 vbs 中使用 ASDI 对象查询所有域:

Set DomObj = GetObject("WinNT://" & strDomain )
DomObj.Filter = Array("computer")

会输出一个数组(DomObj)

或使用 LDAP:

Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
strQuery = "<LDAP://" & strDNSDomain & "> (objectCategory=computer);distinguishedName,operatingSystem;subtree"

来源: http://www.scriptlook.com/list-servers-domain/ & http://www.scriptlook.com/check-services-every-server-domain/

【讨论】:

    【解决方案2】:

    只需创建一个包含服务器列表的文本文件,每行一个。 打开文件(OpenTextFile)并逐行读取。请参阅这些链接以供参考

    http://ss64.com/vb/filesystemobject.html

    FileSystemObject Object

    path = "serverlist.txt"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(path, 1)
    Do Until objFile.AtEndOfStream
      CheckServerUpdateStatus(objFile.ReadLine)
    Loop
    

    【讨论】:

    • 这会取代脚本中的一个部分吗?
    • 是的,它可以替换开始附近的'Dim i'和'Next'之间的部分
    • 我在你告诉我的地方添加了代码,我收到一个关于“路径”未定义的错误:Dim i path = "machines.txt" Set objFSO = CreateObject("Scripting.FileSystemObject")设置 objFile = objFSO.OpenTextFile(path, 1) 直到 objFile.AtEndOfStream CheckServerUpdateStatus(objFile.ReadLine) 循环
    • 同时删除 Dim i,使该行以 path = "machines.txt" 开始
    • 我觉得我很亲密。它正在查看文件,但这就是我得到的:连接到 y_H 运行时错误:远程服务器机器不存在。我不确定它从哪里得到 y_H?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 2016-06-03
    • 2013-07-30
    • 2011-06-26
    • 1970-01-01
    • 2013-09-28
    相关资源
    最近更新 更多