vbscript code to read input from text file avoiding manual efforts

后端 未结 2 1459
故里飘歌
故里飘歌 2021-01-28 00:29

I am drilling down Internet to get Vbscript code, where input is read from text file one per line and pass it to the commands in script. I am just a beginner and need help with

2条回答
  •  逝去的感伤
    2021-01-28 00:46

    This allow to read from arguments collection and from standard input when - is passed as argument

    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 
    

    This allow to still pass arguments in the command line, and, if any of the arguments is a dash, the stantdard input is read. This will allow you to do any of the following

    Usual argument pass

    cscript checkServers.vbs server1 server2
    

    Arguments and piped input

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

    Only redirected input

    cscript checkServers.vbs - < servers.txt
    

    Redirected input and arguments

    < servers.txt cscript checkServers.vbs - serverx
    

    Or any other needed combination of arguments and standard input

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

    EDITED to answer to comments

    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)
    

    Obviously, you need to maintain your CheckServerUpdateStatus function, this code only handles the parameter input.

提交回复
热议问题