Solved

Shell script syntax convert to VBScript

Posted on 2006-10-19
4
1,894 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now