Solved

Shell script syntax convert to VBScript

Posted on 2006-10-19
4
1,943 Views
Last Modified: 2008-01-09
I have a script which does a great job of using the uptime.exe to output a barebones uptime number. I am wanting to use a VBScript to do the same task and have it acquire hostnames from "servers.txt". The text file has one servername per line.

Uptime.exe <servername> is the syntax for remote access.

I am inluding the existing shell script below.

@echo off
setlocal
for /f "tokens=6,7,8,10 delims=, " %%a in ('"uptime.exe"') do (
  set dd=%%a%%b
  set hh=%%c
  set mm=%%d
)
if %hh% LSS 10 set hh=0%hh%
if %mm% LSS 10 set mm=0%mm%
set Uptime=%dd%,%hh%:%mm%
echo Uptime: %Uptime%
pause
0
Comment
Question by:johndarby
  • 2
4 Comments
 
LVL 23

Expert Comment

by:basicinstinct
ID: 17770582
Can I ask why you want to convert this to a VBScript if it "does a great job"...?
If it ain't broke, don't fix it.
0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 400 total points
ID: 17770693
Maybe something like this:

Dim fso,wShell,fil,Server,wExec,upData,upTime,arrTime,x

Set fso = CreateObject("Scripting.FileSystemObject")
Set wShell = CreateObject("Wscript.Shell")

Set fil = fso.OpenTextFile("C:\Servers.txt",1,True)

While Not fil.AtEndOfStream

Server = fil.ReadLine

' make sure line is not blank
if Trim(Server) <> "" Then

Set wExec = wShell.Exec("uptime " & Server)

Do Until wExec.Status
    Wscript.Sleep 200
Loop

' get results
upData = wExec.StdOut.Readall

' get data after the ':' into upTime var
upTime = Trim(Mid(upData,Instr(upData,":") + 1))

' split it by comma
arrTime = Split(upTime,",")

' reset upTime to hold days
upTime = Left(arrTime(0),Instr(arrTime(0),"d") -1) & "day(s),"

' add hours. if len is 1 add a 0 to the front
Select Case Len(Trim(Left(arrTime(1),Instr(arrTime(1),"h") -1)))
      Case 1
            upTime = upTime & "0" & Trim(Left(arrTime(1),Instr(arrTime(1),"h") -1)) & ":"
      Case 2
            upTime = upTime & Trim(Left(arrTime(1),Instr(arrTime(1),"h") -1)) & ":"
End Select

' add minutes. if len is 1 add a 0 to the front
Select Case Len(Trim(Left(arrTime(2),Instr(arrTime(2),"m") -1)))
      Case 1
            upTime = upTime & "0" & Trim(Left(arrTime(2),Instr(arrTime(2),"m") -1))
      Case 2
            upTime = upTime & Trim(Left(arrTime(2),Instr(arrTime(2),"m") -1))
End Select

MsgBox  Server & " Has been up for " & upTime

End If

' get next line
Wend

fil.Close
Set fil = Nothing
Set fso = Nothing
Set wShell = Nothing
0
 
LVL 23

Assisted Solution

by:basicinstinct
basicinstinct earned 100 total points
ID: 17770882
How about this (just change "c:\whatever\mytst.bat" to the path to your batch file):

'Copy this code and paste it into an empty text file, then save as whatever.vbs
Set oShell = CreateObject("WScript.Shell")
oShell.Run "c:\whatever\mytst.bat", 0, True
0
 
LVL 1

Author Comment

by:johndarby
ID: 17777645
Vinny, you are a great instructor! I appreciate the time you took to explain the details. When I finally get a handle on scripting in VB, I plan to tackle C#. But until then, I am thankful for your splendid tutelage.

basicinstinct,
thanks for your input as well. Maybe I should give a more through explanation with my next question. I am definately the learner here and am seeking ways to identify and remember good syntax. Sometimes, porting from shell to VBS helps.

Thanks both!
JohnD
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question