Link to home
Start Free TrialLog in
Avatar of Wibble_
Wibble_

asked on

VBscript to Javascript Coversion. Help! 1000 points!

I need to convert hte following script, as our new software restrictions dont allow me to write the vbs file that waits for a second (any other vbs wait function pegs the processor, which kind of defeats the object, as this is supposed to save energy

Oh, just so you dont have to figure it out, it checks to see if there is anyone logged in. If there isn't then it shuts the machine down, if there is it gives a 10 min counter with a picture of the world held in a pair of hands that fades to red before exiting if you cancel the countdown - we run it at 5:30 pm and every half hour after that till 11:00.

I'm not too good at javascript, so any help would be great. A complete solution is worh 1000 points (i'll start another question & give you the points from that as well.)

cheers,

Will.

<TITLE>  TreeSaver  </TITLE>
<HTA:APPLICATION id="TreeSaver"
applicationName="TreeSaver"
border="none"
borderStyle="raised"
caption="no"
icon=""
maximizeButton="no"
minimizeButton="no"
showInTaskbar="no"
windowState="normal"
innerBorder="yes"
navigable="no"
scroll="no"
scrollFlat="no"
singleInstance="yes"
sysMenu="yes"
contextMenu="no"
selection="yes"
version="1.0" />

<script language="vbscript">
'#################################################################
      window.moveto 2000,2000 'hide it
'first check exceptionlist
 Dim Act : Set Act = CreateObject("Wscript.shell")
 Dim CName : CName = Act.ExpandEnvironmentStrings("%ComputerName%")    
      if CheckWebExceptionList("http://intranet/toolkit/TreeSaver/exceptionlist.php", CName) then
            self.close()
            Rst1(1)
      end if
      'resize & center
       window.moveTo (screen.width -390) /2,(screen.height -470) /2
      window.resizeTo 390,470
Dim INTA, SHUTDOWN, user, TimeLeft, Onlist
 Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
 Dim Env : Set Env = Act.Environment("Process")
 Dim Http : Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
 Dim Sd : Sd = Act.ExpandEnvironmentStrings("%systemdrive%")
 Dim UName : UName = Act.ExpandEnvironmentStrings("%UserName%")  
 Dim objWMIService : Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & CName & "\root\cimv2") 'Grab machine object from WMI
 Dim colComputer : Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
 
 Sub Window_onLoad
      'check eceptionlist

            
      For Each objComputer in colComputer
            user=objComputer.UserName 'extract users from object, not pretty, but works
      Next
             If not isNULL(user) then 'if there is a user logged in then
                  'start the countdown
                  INTA = 600 'secs to count from
                  Do Until INTA = 0
                        INTA = INTA - 1
                        Timeleft=SecsToHours(INTA)
                        DataArea1.InnerHTML = "<FONT FACE=""Arial"">This computer will automagically shutdown in:</font><br><FONT SIZE=36 FACE=""Arial""> " & TimeLeft(1) & ":" & TimeLeft(0) & " </font><br><FONT FACE=""Arial"">mins:secs</font>"
                        Rst1(1)
                              If SHUTDOWN=1 then 'canx button pressed so dont shutdown
                                    INTA=0
                              else 'shut machine down
                                    SHUTDOWN=0
                              End if
                  Loop
            else
                  SHUTDOWN=0
            End If
            
      if SHUTDOWN=0 then
            DataArea1.InnerHTML = "Bye Bye..."
            Act.Exec ("%SYSTEMROOT%\SYSTEM32\SHUTDOWN.EXE -s -f -t 00")
            'Act.Exec("%systemroot%\system32\cmd.exe")
            self.close()
      else
            Rst1(2)
            self.close()
      end if
end sub
'##############################################################################
   
   Function Rst1(secs) ' Makes a vbs file to wait 1000ms = 1 sec
         Dim Ts : Set Ts = Fso.OpenTextFile(SD & "\Rest1.vbs", 2, true)
         Ts.WriteLine "Wscript.sleep " & secs * 1000
         Ts.close
         Act.run(SD & "\Rest1.vbs"), 1 , True
         Fso.DeleteFile(SD & "\Rest1.vbs")
   End Function
 
   Function Quit
         turnred(MyImg)
         DataArea1.InnerHTML = "<BR><FONT FACE=""Arial""><b>Shutdown Cancelled.</b><br>Isn't it time to go home yet, " & Uname & "?</font>"
         SHUTDOWN=1
   End function
   
   Function TurnRed (oImg) 'turns the image red
      oImg.filters(0).Apply()
      oImg.src="http://intranet/toolkit/TreeSaver/handglobe_red.jpg"
    oImg.filters(0).Play()
   End Function
   
   Function SecsToHours(intTotalSecs)'Convert time in seconds to hours, minutes, seconds and return in array.
      intHours = intTotalSecs \ 3600
      intMinutes = (intTotalSecs Mod 3600) \ 60
      intSeconds = intTotalSecs Mod 60
      SecsToHours = Array(intSeconds, intMinutes, intHours)
   End Function
   
   Function CheckWebExceptionList(strUrl, strCompname) 'As Boolean
      Dim web, strPage, strData, strBuffer, lngCounter, ado, check
    On Error Resume Next
    CheckWebExceptionList = False
      'Download the file with any available object
    Err.Clear
    Set web = Nothing
    Set web = CreateObject("WinHttp.WinHttpRequest.5.1")
    If web Is Nothing Then Set web = CreateObject("WinHttp.WinHttpRequest")
    If web Is Nothing Then Set web = CreateObject("MSXML2.ServerXMLHTTP")
    If web Is Nothing Then Set web = CreateObject("Microsoft.XMLHTTP")
    web.Open "GET", strURL, False
    web.Send
    If Err.Number <> 0 Then
        CheckWebExceptionList = False
        Set web = Nothing
        Exit Function
    End If
    If web.Status <> "200" Then
        CheckWebExceptionList = False
        Set web = Nothing
        Exit Function
    End If
    strPage = web.ResponseText
    Set web = Nothing
    'Now check the string for the exception
      check=InStr(1,strPage,VBCRLF & strCompname & ",",1)
      if check = 0 then
            CheckWebExceptionList = False
            else
            CheckWebExceptionList = True
      end if
      Exit Function
   End Function
'##############################################################################  

<body bgcolor=black Title="Treesaver. Irritating but necessary" STYLE= "10.75pt Arial; color:#99ccff">
<p align="center">
<BR>
<FONT SIZE=24 FACE="Arial"><B>Tree</b>Saver</FONT>
<BR>
<BR>
<IMG ID="MyImg" src="http://intranet/toolkit/TreeSaver/handglobe.jpg"  STYLE="FILTER: progid:DXImageTransform.Microsoft.Fade(Overlap=1.00)" />
<BR>
<br>
<input id=runbutton class="button" type="button" value="No, I love my carbon emissions!" name="ok_button"STYLE="font:10pt Arial;color:#006c6c;font-weight:Bold-Italic;
   "Title="This will cancel the shutdown" onclick="Quit">
<br>
<br>
<span id=DataArea1> </span>
</p>
</body>
Avatar of Wibble_
Wibble_

ASKER

Oh, if you want the graphics (to see it in it's full glory) then they are here:

http://www.b3tards.com/uploads/handglobe.jpg
http://www.b3tards.com/uploads/handglobe_red.jpg
Avatar of Curtis Shull
Will,

this should get you really close.

<TITLE>  TreeSaver  </TITLE>
<HTA:APPLICATION id="TreeSaver"
applicationName="TreeSaver"
border="none"
borderStyle="raised"
caption="no"
icon=""
maximizeButton="no"
minimizeButton="no"
showInTaskbar="no"
windowState="normal"
innerBorder="yes"
navigable="no"
scroll="no"
scrollFlat="no"
singleInstance="yes"
sysMenu="yes"
contextMenu="no"
selection="yes"
version="1.0" />

<SCRIPT language="JavaScript">
/*Set Script Type-------------------------------------------------------------------------------------*/
     window.moveto 2000,2000 //hide it;
//first check exceptionlist
 var Act :Act = new ActiveXObject("Wscript.shell");
 var CName : CName = Act.ExpandEnvironmentStrings("%ComputerName%")    ;
     if(CheckWebExceptionList("http://intranet/toolkit/TreeSaver/exceptionlist.php", CName)){ ;
          self.close();
          Rst1(1);
     }
     //resize + center;
      window.moveTo (screen.width -390) /2,(screen.height -470) /2;
      window.resizeTo 390,470;
 var INTA, SHUTDOWN, user, TimeLeft, Onlist;
 var Fso :Fso = new ActiveXObject("Scripting.FileSystemObject");
 var Env :Env = Act.Environment("Process");
 var Http :Http = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
 var Sd : Sd = Act.ExpandEnvironmentStrings("%systemdrive%");
 var UName : UName = Act.ExpandEnvironmentStrings("%UserName%")   ;
 var objWMIService :objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + CName + "\root\cimv2") 'Grab machine object from WMI;
 var colComputer :colComputer = objWMIService.ExecQuery  ("Select * from Win32_ComputerSystem");

 Window.onLoad = function{
     //check eceptionlist


     For Each objComputer in colComputer;
          user=objComputer.UserName //extract users from object, ! pretty, but works;
     Next;
           if(! isNULL(user)){ //if(there is a user logged in){
               //start the countdown;
               INTA = 600 //secs to count from;
               Do Until INTA = 0;
                    INTA = INTA - 1;
                    Timeleft=SecsToHours(INTA);
                    DataArea1.InnerHTML = "<FONT FACE=""Arial"">This computer will automagically shutdown in:</font><br><FONT SIZE=36 FACE=""Arial""> " + TimeLeft(1) + ":" + TimeLeft(0) + " </font><br><FONT FACE=""Arial"">mins:secs</font>";
                    Rst1(1);
                         if(SHUTDOWN==1){ //canx button pressed so dont shutdown;
                              INTA=0;
                         }else{ //shut machine down;
                              SHUTDOWN=0;
                         }
               Loop;
          }else{
               SHUTDOWN=0;
          }

     if(SHUTDOWN==0){
          DataArea1.InnerHTML = "Bye Bye...";
          Act.Exec ("%SYSTEMROOT%\SYSTEM32\SHUTDOWN.EXE -s -f -t 00");
          //Act.Exec("%systemroot%\system32\cmd.exe");
          self.close();
     }else{
          Rst1(2);
          self.close();
     }
}
//##############################################################################

   function Rst1(secs) // Makes a vbs file to wait 1000ms = 1 sec{
        var Ts :Ts = Fso.OpenTextFile(SD + "\Rest1.vbs", 2, true);
        Ts.WriteLine "Wscript.sleep " + secs * 1000;
        Ts.close;
        Act.run(SD + "\Rest1.vbs"), 1 , True;
        Fso.DeleteFile(SD + "\Rest1.vbs");
   }

   function Quit{
        turnred(MyImg);
        DataArea1.InnerHTML = "<BR><FONT FACE=""Arial""><b>Shutdown Cancelled.</b><br>Isn't it time to go home yet, " + Uname + "?</font>";
        SHUTDOWN=1;
   }

   function TurnRed (oImg) //turns the image red{
     oImg.filters(0).Apply();
     oImg.src="http://intranet/toolkit/TreeSaver/handglobe_red.jpg";
     oImg.filters(0).Play();
   }

   function SecsToHours(intTotalSecs)//Convert time in seconds to hours, minutes, seconds && return in array.{
     intHours = intTotalSecs \ 3600;
     intMinutes = (intTotalSecs Mod 3600) \ 60;
     intSeconds = intTotalSecs Mod 60;
     SecsToHours = Array(intSeconds, intMinutes, intHours);
   }

   function CheckWebExceptionList(strUrl, strCompname) //As Boolean{
     var web, strPage, strData, strBuffer, lngCounter, ado, check;
    On Error Resume Next;
    CheckWebExceptionList = False;
     //Download the file with any available object;
    Err.Clear;
   web = null;
   web = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
    if(web Is null){web = new ActiveXObject("WinHttp.WinHttpRequest");
    if(web Is null){web = new ActiveXObject("MSXML2.ServerXMLHTTP");
    if(web Is null){web = new ActiveXObject("Microsoft.XMLHTTP");
    web.Open "GET", strURL, False;
    web.Send;
    if(Err.Number != 0){
        CheckWebExceptionList = False;
       web = null;
        //Exit Function
    }
    if(web.Status != "200"){
        CheckWebExceptionList = False;
       web = null;
        //Exit Function
    }
    strPage = web.ResponseText;
   web = null;
    //Now check the string for the exception;
     check=InStr(1,strPage,'\r\n' + strCompname + ",",1);
     if(check == 0){
          CheckWebExceptionList = False;
          }else{
          CheckWebExceptionList = True;
     }
     //Exit Function
   }
   </SCRIPT>
/*End js portion--------------------------------------------------------------------------------------------*/
<body bgcolor=black Title="Treesaver. Irritating but necessary" STYLE= "10.75pt Arial; color:#99ccff">
<p align="center">
<BR>
<FONT SIZE=24 FACE="Arial"><B>Tree</b>Saver</FONT>
<BR>
<BR>
<IMG ID="MyImg" src="http://intranet/toolkit/TreeSaver/handglobe.jpg"  STYLE="FILTER: progid:DXImageTransform.Microsoft.Fade(Overlap=1.00)" />
<BR>
<br>
<input id=runbutton class="button" type="button" value="No, I love my carbon emissions!" name="ok_button"STYLE="font:10pt Arial;color:#006c6c;font-weight:Bold-Italic;
   "Title="This will cancel the shutdown" onclick="Quit">
<br>
<br>
<span id=DataArea1> </span>
</p>
</body>

Avatar of Wibble_

ASKER

Thanks, but I can google for automatic translation services myself :-/
ASKER CERTIFIED SOLUTION
Avatar of Curtis Shull
Curtis Shull
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wibble_

ASKER

True, I was just being lazy.. I guess you guys have better things to do. I think i probably would too if the situation was reversed ;-)

W.