Search code examples
batch-fileembed

How to share values between batch file and embedded script


I am trying to share values between my batch file and embedded script within it and I can't find a way to make it work. I am relatively new to embedded scripting...

I have tried to find an answer on the web and I can't find an answer to my question. It would need to be (all the scripts) in the same .bat file...

<!-- : Begin batch script
@echo off
cls
set "Shared_UserName=VelocityDK"
goto ShareValue

:ShareValue
cls
cscript //nologo "%~f0?.wsf" //job:UserName
pause >nul
cls & exit /force

----- Begin wsf script --->
<package>
    <job id="UserName">
        <script language="VBScript">
            Dim Shared_UserName As String = %Shared_UserName%
            WScript.Echo "Your username is: " & Shared_UserName
        </script>
    </job>
</package>

I am expecting the embedded VBScript to write the following output: Your username is VelocityDK. but instead, I get a message saying:

Microsoft VBScript compilation error: Expected end of statement


Solution

  • If you plan to use shell methods inside VBScript then use Windows Scripting Host (WSH) automation object. Please find the code:

    Set wshShell = CreateObject( "WScript.Shell" )
    userName = wshShell.ExpandEnvironmentStrings("%Shared_UserName%")
    WScript.Echo "Your username is: " & userName
    

    See refs :devguru.com or ss64.com