vbscript code to read input from text file avoiding manual efforts

微笑、不失礼 提交于 2019-12-02 11:33:57

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!