How to Use Bat Files and Batch Scripts in Ubot Studio 

 July 21, 2021

By  Reed

Batch scripting is a powerful but overlooked tool for accomplishing really simple yet important tasks. With it you can run commands you would normally run in command prompt, with the advantage of having it in a saved file that can be ran whenever it is opened.

Run a Bat File in Ubot

Running a .bat file in Ubot Studio is easy. It’s exactly the same as running powershell. You simply use the Shell command like so:

 shell("C:\\Users\\myComp\\OneDrive\\Desktop\\NetDasebleThenSleep.bat")

The shell command contains the file path to the .bat file we wish to run. Now whenever the bot is ran the .bat file will be opened and executed.

Run Batch Scripts in Ubot

You can also run a batch script by pasting it into any of these commands and functions: heopas shell batch hidden, shell batch and shell batch hidden, and the function $shell batch hidden. The “hidden” means that the shell window will not be displayed and the user will never see the batch script run. The function $shell batch hidden can be ran in a variable to save the output of a batch script.  Your batch script needs to be single spaced and without the @echo at the top to work in these commands and functions.

plugin command("Advanced Shell.dll", "shell batch", "PAUSE
TIMEOUT 3", "Yes")

This batch script will open the shell window and wait for the user to hit the keyboard before continuing and counting down from 3. Whenever using PAUSE you should test the batch script in a visible shell so you can see it working. I’ll show you how you can have Ubot interact with this Shell window in the How to Test Before Compiling section below.

Running Batch Scripts That Require Administrator Rights

Actions such as turning off a computer or disabling a network adapter require Admin rights. This is the very nature of Windows, and you should expect that Admin rights be required for many of the tasks a bot will do.

A batch script that requires admin rights takes planning when using in a bot built with Ubot Studio. You don’t want the user to have to sit there and confirm Admin rights each time they are needed. You want to request admin rights as little as possible to make your bots as passive as possible. It is smart to have it done at beginning of the bot when the user is likely there and engaging in the software.  Perhaps the simplest solution is to compile your bot and set the bot’s .exe to “run as administrator”, but this can be a pain.

To run a batch script in Ubot with admin rights, we have a couple options.

Use a Plugin to Run As Administrator

The plugin you will need is the Elite Ubot plugin from Nick of Elitebotters.com. His plugin has a command that lets you run a file with Admin rights.

 plugin command("EliteUbot.dll", "EB Run As Administrator", "C:\\Users\\myComp\\OneDrive\\Desktop\\NetDasebleThenSleep.bat")

Now whenever the bot is ran the user will be asked to provide administrator privilege’s. If your bot has a lot of functions to accomplish before it needs to do the task that requires admin privilege’s, then you should put a conditional in the batch script that makes it wait for those tasks to complete. One solution is to add a PAUSE.

PAUSE makes a batch script wait for the user to press a key with the shell window activated. To have your bot send a keystroke to that window you will need to compile it and give it admin rights. Without admin rights the bot cannot send keystrokes to the Shell window. There’s no issue if the script requires no admin privileges, and you’ll see how that is done below.

How to Test Before Compiling

Imagine having to compile your bot every time and then assigning it admin rights just to see if it works? Forget that! A better solution is to use the command bot runas admin  from the free plugin Software Windows.

plugin command(“SoftwareWindows.dll”, “bot runas admin”)

Running this command will open a new Ubot Studio instance that HAS admin rights, which will allow you to test if Ubot can indeed interact with a Shell. To interact with a Shell in Ubot you would use one of two commands to focus the window: os bring to front or set active window. 

 plugin command("OSCommandsFunctions.dll", "os bring to front", "C:\\Windows\\SysWOW64\\cmd.exe")

This command comes from the free plugin OS Commands. It runs faster than the next one.

 plugin command("WindowsCommands.dll", "set active window", "C:\\Windows\\SysWOW64\\cmd.exe", "ConsoleWindowClass")

This command is from the free plugin Windows Commands, and is somewhat slow but has a wizard that helps guarantee you are targeting the right window.

To then interact with the shell window we use the keyboard event command that also comes from the Windows Commands plugin.

plugin command("WindowsCommands.dll", "keyboard event", "Up", "Key Press")

Compile a Ubot Bot to Run With Admin Rights

You can make a compiled ubot bot have admin rights by following the steps below.

This is a bit too involved and not always the best solution, especially if you are actively developing.

Passing Admin Actions Off to a Helper Bot

Another solution is to put all the admin actions in another bot that accepts incoming hotkeys to execute admin actions. There’s nothing wrong with building a “helper bot” that is specifically made to execute admin functions, especially since it saves time and is something you can use as a companion to your bots. I find Autoit to be a great companion tool to Ubot so the example I am about to share uses a compiled Autoit bot to run admin functions. It launches in tandem with ubot and waits in the background for specific hotkeys that are tied to an admin function.

Instead of setting our compiled Ubot bot to have admin privileges, we instead have our Autoit helper bot request it by adding this to the top of our Autoit script:

#RequireAdmin

Whenever the script runs it will request admin rights. We then pass on the torch for loading the .bat file that requires admin rights to our Autoit script and use Autoit’s ShellExecute() command.

ShellExecute("C:\Users\myComp\OneDrive\Desktop\NetDasebleThenSleep.bat")

You would paste the path to the .bat file in that command the same way you did in the Shell command in Ubot. We then put the ShellExecute() into a function and assign a hotkey to it. The entire Autoit script will look something this:

#RequireAdmin
HotKeySet("{UP}","HotKeyPressed") ; up key
HotKeySet("{.}", "Terminate")

While 1;loop forever until ubot sends hotkeys

sleep(100)

WEnd

Func HotKeyPressed()
ShellExecute("C:\Users\myComp\OneDrive\Desktop\NetDasebleThenSleep.bat")
WinActivate("C:\Windows\SysWOW64\cmd.exe")
sleep(3000)
Send("{SPACE}")
EndFunc

Func Terminate()
Exit 0
EndFunc

You have shellexecute() which launches the bat file, winactivate() to bring the shell window into focus, and send() to send the spacebar key to the shell window. All of this sits inside a function that waits for the UP arrow hotkey to be pressed. We can have this one Autoit file host all our admin actions, assigning each a hotkey and a function that executes the action.  In this example we have Autoit launching a bat file that requires admin rights to run. Ideally we would have a bat file for each function like we have above. This enables us to have loads of “helper .bat’s” to use in different ways in our bots, and a “helper bot” that makes it easy to as pressing a hotkey to execute them. Whenever we want to run an admin function, we have Ubot hit the appropriate hotkey!

Have Ubot Launch and Communicate With the Helper Bot

We then compile our Autoit script into an .exe using the “compile” option in Scite, which is Autoit’s scripting tool. This command is available by hitting ctrl+f7.

Once your “helper bot” is saved you then use the EB run as admin command in Ubot to launch the .exe. The Shell command does not work for running programs that require admin rights. An error popup will jump in claiming File Not Found. The correct way to do it is with the command EB run as admin like so…

 plugin command("EliteUbot.dll", "EB Run As Administrator", "C:\\Users\\myComp\\OneDrive\\Desktop\\shell-batch-admin.exe")

You would run that at the top of your bot to have the helper bot launch in tandem and wait in the background. Then, when you are ready to execute an admin function you would have Ubot focus the shell window and then send the appropriate hotkey like so…

plugin command("WindowsCommands.dll", "keyboard event", "Up", "Key Press")
plugin command("OSCommandsFunctions.dll", "os bring to front", "C:\\Windows\\SysWOW64\\cmd.exe") plugin command("WindowsCommands.dll", "keyboard event", "Space", "Key Press")

The commands we are using here is os bring to front and keyboard event, which comes from the free plugin Windows Commands developed by Ubot’s Seth Turin.

And last but not least we have to close out our helper bot by hitting the assigned hotkey for exiting it. In this case we set the period to close the helper bot. When you shut down your Ubot make sure it sends a final keyboard event for closing the helper bot.

plugin command("WindowsCommands.dll", "keyboard event", ".", "Key Press")

And there you have it, an alternative way of having a “helper bot” execute admin functions whenever a Ubot bot uses the appropriate hotkey. This “helper bot” method is ideal if you plan on developing lots of bots as it is always a good idea to break up bots into smaller parts that can be managed individually and used together as needed. Always take notes on how each bot works and what the intended use is. This helps tremendously.


Reed

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Subscribe to our newsletter now!

RSS
Follow by Email
YouTube
X

Forgot Password?

Join Us