Run wsf/vbs without tempfile

 Running Windows Script Files (WSF/VBS) without creating a temporary file can be done by using a technique called "command line embedding." With this approach, you embed the script content directly into the command line or batch file. This is useful when you want to run a script without creating an external file. Here's how you can achieve this for VBS scripts:


```batch

@echo off

setlocal enabledelayedexpansion


:: Your VBS script content, enclosed in double carets (^^)

set "vbsScript=^^

Set objShell = CreateObject("WScript.Shell")^^

objShell.Popup "Hello, World!", 5, "Popup Example", 64^^

"


:: Create a temporary batch file with embedded VBS script

set "batchFile=%temp%\embedded_script.bat"

echo !vbsScript! > "!batchFile!"


:: Run the batch file to execute the embedded script

cmd /c "!batchFile!"


:: Clean up the temporary batch file

del "!batchFile!"


endlocal

```


In this example:


1. `vbsScript` contains your VBS script content. It's embedded within double carets (`^^`) to preserve newlines and special characters.


2. A temporary batch file (`embedded_script.bat`) is created and populated with the embedded VBS script using `echo`.


3. The `cmd /c` command is used to execute the temporary batch file, which, in turn, runs the embedded VBS script.


4. After the script is executed, the temporary batch file is deleted.


This approach allows you to run a VBS script without creating an external `.vbs` file. Be cautious when working with embedded scripts, as they can be more challenging to maintain and debug compared to separate script files.

Comments

Popular posts from this blog

bad character U+002D '-' in my helm template

GitLab pipeline stopped working with invalid yaml error

How do I add a printer in OpenSUSE which is being shared by a CUPS print server?