Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to Check if Machine is Online

Status
Not open for further replies.

djtech2k

MIS
Jul 24, 2003
1,097
US
I am writing a script to do some server management. This script reads from a text file and then connects to each server and performs certain tasks.

One thing I am trying to build in is the ability to check if a server is online before it tries to perform any actions so that the script does not get hung up or fail when running a large list of machines.

My only guess was to do a ping check. I am doing this, but it is not working how I need it to. Here is the check and the function that I am using:

Code:
If Not IsPingable(objItem) Then
wscript.echo objItem & " is not pingable" & ";" & ";" & ";" & ";" & ";"
wscript.quit
End If

Function IsPingable(objItem)
 Dim objShell, objExec, strCmd, strTemp
 
 strCmd = "ping -n 1 " & objItem
 
 Set objShell = CreateObject("WScript.Shell")
 Set objExec = objShell.Exec(strCmd)
 strTemp = UCase(objExec.StdOut.ReadAll)
 
 If InStr(strTemp, "MS") Then
   IsPingable = True 
 Else
   IsPingable = False
 End If
End Function

Now what is happening is that if the machine is pingable, it carries on and does its thing. However, once it encounters one that is not pingable, then it dies. I know it is because I am doing a quit, but what else can I do that would handle this?

This particular part is inside of a "For Each..Next" Loop that in turn is inside of a "Do While" loop that is reading a text file with server names in it.

Any ideas to do my "online" check or to fix what I am doing now?
 
If i put in the "X" as an argument, I want all of my output to be put into an xls. For some reason, it is failing when opening/creating the spreadsheet.
 
Whoops..forgot to put the command line

cscript.exe script.vbs c:\computerlist.txt /format:x /outfile:c:\output.xls

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks very much. I agree that creating functions/subs is very tidy, but I am not 100% clear on how to pass values between them all.

I do have a question. I see your sample line to run this script. How does the script handle the "/output:X" type of parameters? I know how to use the wscript.arguments obviously, but where is it told to handle the "/param"?
 
Have a look at the WshNamed object.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV. For some reason, I did not see that. It makes sense now.

I have tried this and I received the same error as before.

Ran this command:

cscript Computer_Local_Accounts_By_Date_2.vbs c:\servers.txt /format:x /outfile:c:\output.xls


Got this Output:

Script Started....11/6/2007 10:27:02 AM
C:\OUTPUT.XLS
Spreadsheet cannot be opened: C:\OUTPUT.XLS

Why is it failing to create/open the xls? The same thing happened with the last version that I posted.
 
More research has told me that the error is on this line:

objExcel.Workbooks.Open strExcelPath

After that line, the err.number goes from 0 to 1004.
 
Well the way you had it, it was opening a spreadsheet to write to. If it is not suppose to do that then you just don't do the Open and instead use Add

objExcel.Workbooks.Open >> objExcel.Workbooks.Add

The path you specify for the excel file would then be used at the end of the script to save it.

See the changes to the SetupExcel function and the new ExcelCleanUp sub to save and quit.

Code:
Option Explicit

Main()

Sub Main()
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    
    WScript.Echo "Script Started...." & Now()
    
    ' call function to get server/computer list file location
    Dim strServerList : strServerList = GetServerList
    ' if no input file is specified then exit the sub/quit the script
    If strServerList = "" Then Exit Sub
    
    Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objTextFile : Set objTextFile = objFSO.OpenTextFile(strServerList, ForReading)
    ' get output format by calling sub; will ask for parameters if not passed via command line
    Dim strOutputFormat : strOutputFormat = GetOuputFileFormat
    ' exit if no output format specified
    If strOutputFormat = "" Then Exit Sub
    Dim strOutputFile : strOutputFile = GetOutputFilePath(strOutputFormat)
    Dim intRow : intRow = "1"
    
    Select Case strOutputFormat
        Case "EXCEL"
            ' if excel format is chosen then get object by calling setupexcel function
            Dim objExcel : Set objExcel = SetupExcel(strOutputFile)
            objExcel.Cells(intRow, 1).Value = "Server"
            objExcel.Cells(intRow, 2).Value = "User"
            objExcel.Cells(intRow, 3).Value = "Last PW Set in Days"
            objExcel.Cells(intRow, 4).Value = "PW Expired?"
            objExcel.Cells(intRow, 5).Value = "Locked?"
            objExcel.Cells(intRow, 6).Value = "Disabled?"
            intRow = intRow + 1
        Case "TEXT"
            WScript.Echo "Server" & ";" & "User" & ";" & _
                         "Last Password Set in Days" & ";" & _
                         "Password Expired?" & ";" & "Account Locked?" & _
                         ";" & "Account Disabled?"
        Case Else
    End Select

    Dim strComputer
    Do Until objTextFile.AtEndOfStream
        strComputer = Trim(objTextFile.Readline)
        If strComputer <> "" Then
            If Reachable(strComputer) Then
                Call GetUserInfo(strComputer, strOutputFormat, objExcel, intRow)
            Else
                Select Case strOutputFormat
                    Case "EXCEL"    objExcel.Cells(intRow, 1).Value = strComputer
                    Case "TEXT"        WScript.Echo strComputer & " is not pingable" & ";" & ";" & ";" & ";" & ";"
                End Select
            End If
            intRow = intRow + 1
        End If
    Loop
    
    If strOutputFormat = "EXCEL" Then ExcelCleanUp objExcel, strOutputFile
    WScript.Echo "Script Ended...." & Now()
End Sub

Sub ExcelCleanUp(objExcel, strOutputFile)
	objExcel.ActiveWorkbook.SaveAs strOutputFile
	objExcel.Quit
End Sub

Sub GetUserInfo(strComputer, strOutputFormat, objOutFile, intRow)
    Dim objComputer : Set objComputer = GetObject("WinNT://" & strComputer)
    objComputer.Filter = Array("user")

    Dim objUser, dts, d, hh, mn, ss
    For Each objUser In objComputer
        objUser.GetInfo
        dts = objUser.passwordage
        d = int(dts/60/60/24)
        dts = dts Mod 60*60*24
        hh = right("00" & int(dts/60/60),2)
        dts = dts mod 60*60
        mn = right("00" & int(dts/60),2)
        dts = dts mod 60
        ss = right("00" & dts,2)
        If d > 88 Then
            Select Case strOutputFormat
                Case "EXCEL"
                    objOutFile.Cells(intRow, 1).Value = objComputer.Name
                    objOutFile.Cells(intRow, 2).Value = objUser.name
                    objOutFile.Cells(intRow, 3).Value = d
                    objOutFile.Cells(intRow, 4).Value = objUser.passwordexpired
                    objOutFile.Cells(intRow, 5).Value = objUser.isaccountlocked
                    objOutFile.Cells(intRow, 6).Value = objUser.accountdisabled
                Case "TEXT"
                    WScript.Echo objComputer.Name & ";" & objUser.name & ";" & d & ";" & objUser.passwordexpired & ";" & _
                    objUser.isaccountlocked & ";" & objUser.accountdisabled
            End Select
            intRow = intRow + 1
        End If
    Next
End Sub

Function GetServerList
    If WScript.Arguments.Count > 0 Then
        GetServerList = WScript.Arguments(0)
    Else
        Dim objDialog : Set objDialog = CreateObject("UserAccounts.CommonDialog")
        objDialog.Filter = "Text Input File|*.txt|All Files|*.*"
        objDialog.InitialDir = "C:\"
        Dim intResult : intResult = objDialog.ShowOpen
        
        If intResult <> 0 Then
            GetServerList = objDialog.filename
        End If
    End If
End Function

Function GetOuputFileFormat
    Dim strOutputFormat : strOutputFormat = UCase(WScript.Arguments.Named("format"))
    If strOutputFormat = "" Then strOutputFormat = UCase(InputBox(_
    "Enter output file format. i.e. X or Excel for Excel. T or Text for Text", "Output File Format", "Text"))
    Select Case strOutputFormat
        Case "X", "EXCEL"     GetOuputFileFormat = "EXCEL"
        Case "T", "TEXT"     GetOuputFileFormat = "TEXT"
        Case Else             GetOuputFileFormat = ""
    End Select
End Function

Function GetOutputFilePath(strOutputFormat)
    Dim strOutputFile : strOutputFile = UCase(WScript.Arguments.Named("outfile"))
    Dim strDefaultPath
    Select Case strOutputFormat
        Case "EXCEL"    strDefaultPath = "C:\temp\output.xls"
        Case "TEXT"     strDefaultPath = ""
    End Select
    If strOutputFile = "" Then strOutputFile = UCase(InputBox("Enter the output file path.", "Output File Path", strDefaultPath))
    GetOutputFilePath = strOutputFile
End Function

Function Reachable(strComputer)
    Dim strCmd : strCmd = "ping -n 1 " & strComputer
    Dim objShell : Set objShell = CreateObject("WScript.Shell")
    Dim objExec : Set objExec = objShell.Exec(strCmd)
    Dim strOutput : strOutput = UCase(objExec.StdOut.ReadAll)
    If InStr(strOutput, "MS") Then
        Reachable = True
    Else
        Reachable = False
    End If
End Function

Function SetupExcel(strExcelPath)
    WScript.Echo strExcelPath
    
    ' Check for required arguments.
    If Not strExcelPath <> "" Then
        WScript.Echo "Argument <SpreadsheetName> required. For example:" _
                     & VbCrLf _
                     & "cscript script.vbs c:\servers.txt /format:x /outfile:c:\output.xls"
        Wscript.Quit(0)
    End If
    
    ' Bind to Excel object.
    On Error Resume Next
    Dim objExcel : Set objExcel = CreateObject("Excel.Application")
    If Err.Number <> 0 Then
        WScript.Echo "Excel application not found."
        Wscript.Quit
    End If
    On Error GoTo 0

    ' Open spreadsheet.
    On Error Resume Next
    objExcel.Workbooks.Add
    objExcel.Visible = True
    If Err.Number <> 0 Then
        On Error GoTo 0
        WScript.Echo "Spreadsheet cannot be opened: " & strExcelPath
        Wscript.Quit
    End If
    On Error GoTo 0
    
    Set SetupExcel = objExcel
End Function



--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top