vbscript code to read input from text file avoiding manual efforts

后端 未结 2 1458
故里飘歌
故里飘歌 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.

    0 讨论(0)
  • 2021-01-28 01:08

    If you are trying to get the pending windows update installs for a bunch of computers, you can use this in 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
    }
    

    As you can see, we only show patches which have a $null value for InstalledOn, which means they have not been installed as yet.

    powershell

    0 讨论(0)
提交回复
热议问题