Does anyone have script that will work to execute a .bat file? I have been trying everything I can find and nothing will execute my bat file. My bat file works fine when I run it just not in a script in Incontrol.
Script to execute a bat file
- 797 Views
- Last Post 10 March 2017
Nelis249,
Thanks for the reply,
I actually ran into a problem with a .bat file not being able to interact with other programs... turns out it had to do with the applications being launched in a different session. There is an article on MSDN that describes the different sessions in Windows.
Sidenote: the article describes basically how to make a virus (or worm) it goes without saying don't do that... I linked to it because it does a good job describing sandboxing in Windows Vista and later.
Now, I avoid the issue by launching all programs in the same session.
Security rules for services is only related to interaction. Services can no longer perform interactive type of actions, e.g. interact with the desktop. This includes if you wanted a .bat file to launch some type of GUI application, since that requires desktop type interaction. Services can still execute .bat, .vbs, ps1, .exe's and so on as long as they are command line type interactions. I do this all the time. No difference in Win10. By the way, this secuity change occurred between XP and Vista. Here are more techical details (https://msdn.microsoft.com/en-us/library/windows/desktop/ms683502(v=vs.85).aspx).
I know this is an older post but did anyone get a script to run .bat files when running Axial Control as a service in Windows 10?
I see there was success on XP but the security rules for services are different in newer versions of windows.
Script to execute an .exe file with passed params:
using System;
using System.Collections.Generic;
using System.Text;
using MLS.ZWave.Service.Rules;
using MLS.ZWave.BusinessObjects;
public class Runbat : ScriptBase, ScriptInterface {
public void runScript() {
try {
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.Arguments = "Record Porch";
psi.FileName = @"C:\House\hs.exe";
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process.Start(psi);
} catch (Exception ex) {
// Log the exception here
var message = ex.Message;
}
}
}
Thanks for this, I have a Chinese relay I been piddling with forever. This script gives me control of it.
Check this one.....Run Bat file from script
Wells
I know this is an old thread but I finally made this work with some tweaks. I am running IC on an old XP laptop.
1. IC compiles the scripts each time it starts. So I had to deselect run as service and close and restart the program each time I changed the script for testing.
2. You have to "escape" any back slashes and quotes you use in the passed parameters. ie use a \ before the character.
3. Once you are happy with you script you can select run as a service, but in order for it to run the bat file you have to edit the properties of the service and select allow access to desktop. It must be a security issue. See link below.
http://stackoverflow.com/questions/178633/minimum-rights-required-to-run-a-windows-service-as-a-domain-account
I tried to make the work forever. For my system it would only work if RUN AS A SERVICE WAS Deselected as an option in incontrol.
So this script works to run a .bat file.
using System;
using System.Collections.Generic;
using System.Text;
using MLS.ZWave.Service.Rules;
using MLS.ZWave.BusinessObjects;
public class Runbat : ScriptBase, ScriptInterface {
public void runScript() {
try {
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = @"C:\BiCommand\AllCameraRecord.bat";
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process.Start(psi);
} catch (Exception ex) {
// Log the exception here
var message = ex.Message;
}
}
}
But I had to ad a directory change at the begining of the .bat file to change to the correct directory for it to work.
C:
cd\?????
But it is working now.
How about this (update the value for psi.FileName to point to the batch file you want to run):
using System;
using System.Collections.Generic;
using System.Text;
using MLS.ZWave.Service.Rules;
using MLS.ZWave.BusinessObjects;
using System.Diagnostics;
using MLS.HA.DeviceController.Common.Device;
public class RunBat : ScriptBase, ScriptInterface {
public void runScript() {
try {
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = @"c:\Scripts\MyBatchFile.cmd";
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process.Start(psi);
} catch (Exception ex) {
// Log the exception here
var message = ex.Message;
}
}
}
Other scrips I have tried that Ryan and others had posted with the test email in them send the email but will not fun my bat file. So I am still looking for the fix.
Yes and nothing happened. I am still on the hunt.
Did you try what I posted in post #2263? It looks like it should work to me. I can't help anymore.
Mike D.
bicommand trigger Cam1 Cam2 Cam3
That is it.
Saved that as the allcamerarecord.bat
The bat file has to be in the same folder as the bicommand app and it is.
Maybe post the contents of the .bat file?
Mike D.
I am still not sure why I cant get this to work. I even moved the location of my Bat file to the default directory where cmd opens. Again the bat file works when I click on it but I get nothing from it when I put it into the script. If I open the cmd window and type the name of the bat file it works fine also?????
Try this:
using System;
using System.Collections.Generic;
using System.Text;
using MLS.ZWave.Service.Rules;
using MLS.ZWave.BusinessObjects;
public class Runbat : ScriptBase, ScriptInterface {
public void runScript() {
try {
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = @"C:\BiCommand\AllCameraRecord.bat";
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process.Start(psi);
} catch (Exception ex) {
// Log the exception here
var message = ex.Message;
}
}
}
You would just put C:\BiCommand\AllCameraRecord.bat in the exec line. Since your bat file has no arguments you don't even need the argument line.
Mike D.
So my bat file is located at C:\BiCommand folder and in that folder the bat file I want to run is AllCameraRecord.bat.
What would I put int he Argument Line and and what would I put in the line down below.
That line sets the arguments to .bat call to execute.
The effect is as if you typed:
porchConvertAndFTP.bat Record Porch
In other words, if the execute line was for a Dir command and the argument line was set to *.txt
it would be like typing
Dir *.txt
on the command line.
Here is a hint that gave me some trouble:
Make sure the references to the file to execute are fully qualified because it runs in the who-knows-where directory. If you are not in the directory that the file.bat is in you will need to refer to it like this:
C:\user\Mike\myFile.bat
Mike D.
Did this help? I will be glad to help more if you'd like me to.
What is the Record Porch referencing? I can see where to in put address to my .bat file but I am not sure what to put here?
psi.Arguments = "Record Porch";