Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shell script syntax convert to VBScript

Posted on 2006-10-19
4
Medium Priority
?
2,078 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 1600 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 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

782 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