vbscript代码从文本文件中读取输入,避免手动操作

时间:2014-02-09 02:33:56

标签: vbscript text-files file-io wsh

我正在浏览Internet以获取Vbscript代码,其中每行一个文本文件读取输入并将其传递给脚本中的命令。我只是一个初学者,需要帮助。

基本上我正在收集脚本以在我们的环境中获取待处理的服务器补丁。脚本如下所示:

    '#
    '# 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

基本上我正在寻找一个放在那里的代码,其中服务器名称输入应该在CLI中执行命令后手动给出,可以在文本文件中给出;就像文本文件中的很多服务器一样,每个服务器每行执行一次,脚本一次执行一次。

干杯!

2 个答案:

答案 0 :(得分:1)

-作为参数传递时,允许从参数集合和标准输入读取

Dim server

For Each server in WScript.Arguments.UnNamed
    If server="-" Then 
        Do While Not WScript.StdIn.AtEndOfStream
            WScript.Echo "Redirected: " + WScript.StdIn.ReadLine
        Loop
    Else 
        WScript.Echo "Argument: " + server
    End If
Next 

这允许仍然在命令行中传递参数,并且,如果任何参数是破折号,则读取标准输入。这将允许您执行以下任何操作

通常的论点传递

cscript checkServers.vbs server1 server2

参数和管道输入

type servers.txt | cscript checkServers.vbs server1 server2 - 

仅重定向输入

cscript checkServers.vbs - < servers.txt

重定向的输入和参数

< servers.txt cscript checkServers.vbs - serverx

或任何其他所需的参数组合和标准输入

type servers.txt | cscript checkservers.vbs server1 server2 - aditionalServer

已编辑以回复评论

Option Explicit
Dim strServer 

If WScript.Arguments.UnNamed.Length < 1 Then 
    CheckServerUpdateStatus "localhost"
Else
    For Each strServer in WScript.Arguments.UnNamed
        If strServer="-" Then 
            Do While Not WScript.StdIn.AtEndOfStream
                strServer = Trim(WScript.StdIn.ReadLine)
                If Len(strServer) > 0 Then CheckServerUpdateStatus strServer 
            Loop
        Else 
            CheckServerUpdateStatus strServer 
        End If
    Next 
End If
WScript.Quit(0)

显然,你需要维护你的CheckServerUpdateStatus函数,这段代码只处理参数输入。

答案 1 :(得分:1)

如果您正在尝试为一堆计算机获取挂起的Windows更新安装,您可以在Powershell中使用它:

$computers = gc text_file_of_computers.txt
ForEach ($computer in $computers) {
     ("-" * 30)+"`n" # Horizontal line

     Write-Host "Patches not yet installed for $($computer)" -f "Yellow"
     Get-Hotfix -co $computer| Where {$_.InstalledOn -eq $null}

     "`n"+("-" * 30) # Horizontal line
}

如您所见,我们只显示$null InstalledOn值的补丁,这意味着它们尚未安装。

相关问题