Solved

Shell script syntax convert to VBScript

Posted on 2006-10-19
4
1,919 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 159
Batch File tool box 5 49
Is doing tutor.com teaching in my situation advisable? 2 82
iSeries email authority 6 55
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…
This is about my first experience with programming Arduino.
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

815 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

12 Experts available now in Live!

Get 1:1 Help Now