Announcement

Collapse
No announcement yet.

Passing string from .vbs to .bat

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing string from .vbs to .bat

    I've got a batch file that calls a vb script. I need the vb script to do stuff and pass back a string. How do I get the string into a variable in the batch file?

    Batch file:
    Code:
    set reboot=no
    
    call vbscript.vbs %computername%
    if %reboot%==no goto EOF else goto reboot
    
    :reboot
    shutdown -r
    VB script example
    Code:
    Set argsFromBatchFile = Wscript.Arguments
    
    'not the actual code
    If argsFromBatchFile(0) = computer1 Then
        reboot="yes"
    Else
        reboot="no"
    End if
    So you see, I need the vb script to change the value of the %reboot% variable in the batch file. Thoughts?

    Thanks.

  • #2
    Re: Passing string from .vbs to .bat

    You know that you can do it all with just one script, whether using one batch or one vbscript.

    Why using the batch anyway?, shutdown.exe requires admin rights. With Vbs it is possible to reboot the system without requiring Admin privileges.


    However, if there is a reason to use the slow two scripts method, here are a few samples that answers your question.

    Batch (using errorlevel from vbs):
    Code:
    @echo off
    
    If Exist "%~0\..\vbscript.vbs" (
    cscript.exe //nologo "%~0\..\vbscript.vbs" %computername%
    )
    
    if %errorlevel% EQU 0 goto EOF else goto reboot
    
    :reboot
    shutdown.exe -r
    Vbscript (passing an exitcode):
    Code:
    Dim reboot
    
    Set argsFromBatchFile = Wscript.Arguments
    If argsFromBatchFile.count < 1 Then wscript.quit
    
    'not the actual code...
    
    'Make Case insensitive!
    If Lcase(argsFromBatchFile(0)) = LCase("name of computer") Then
    
       reboot= 911
    Else
       reboot= 0
    End if
    
    wscript.quit reboot
    --- Or, ----------
    Batch (using string from vbs):
    Code:
    @echo off
    
    >$$tempfile (
       cscript.exe //nologo "%~0\..\vbscript.vbs" %computername%
    ) && set /p reboot=<$$tempfile
    del $$tempfile
    
    if /i "%reboot%" EQU "yEs" goto:reboot
    goto eof
    
    :reboot
    shutdown.exe -r
    Vbscript (passing string through a temp-file):
    Code:
    Set argsFromBatchFile = Wscript.Arguments
    
    'not the actual code
    
    'Make Case insensitive!
    If Lcase(argsFromBatchFile(0)) = LCase("computername") Then
    
       wscript.echo "yes"
    Else
       wscript.echo "no"
    End if
    
    wscript.quit 0
    \Rems
    Last edited by Rems; 8th June 2010, 19:00.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Passing string from .vbs to .bat

      Originally posted by Rems View Post
      You know that you can do it all with just one script, whether using one batch or one vbscript.

      Why using the batch anyway?, shutdown.exe requires admin rights. With Vbs it is possible to reboot the system without requiring Admin privileges.
      We're using a batch file because that's the type of script used in the login scripts. What we want to do is grant the users temporary admin rights, run pspasswd to reset the local admin password when the user logs in, and restart the machine.

      I use shutdown -r to restart the PC because the user will be a local admin. If they're not a local admin, they won't be able to reset the password and I don't want the CP to reboot. I can think of times when I'd want to reboot regardless of admin status. How would I do that with VB?

      As an aside, here is how I reset the password:

      Code:
      Set oShell = CreateObject("WSCript.shell") 
      	sCmd = "\\unc to executable\pspasswd.exe /accepteula" & _
      		" " & argsFromBatchFile(0) & " " & "administrator" & " " & chr(34) & "new password" & chr(34)
      	oShell.Run sCmd, 0, True
      When it runs "oShell.Run sCmd, 0, True", I get a Security Warning from Windows. It asks if I want to allow pspasswd to run or not. How can I prevent that window from popping and assume the answer is to run the exe?

      Thanks.

      Comment


      • #4
        Re: Passing string from .vbs to .bat

        just a question.. why don't you change the password by using a startup script (via GPO)?
        Marcel
        Technical Consultant
        Netherlands
        http://www.phetios.com
        http://blog.nessus.nl

        MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
        "No matter how secure, there is always the human factor."

        "Enjoy life today, tomorrow may never come."
        "If you're going through hell, keep going. ~Winston Churchill"

        Comment


        • #5
          Re: Passing string from .vbs to .bat

          I like that exitcode method. I'll definitely use it in the future. This time though, we decided to go with the startup script method (I hadn't thought about it before).

          Comment

          Working...
          X