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
- 778 Views
- Last Post 10 March 2017
I can't help but we have been trying to get this voted up and worked on:
http://incontrolha.uservoice.com/forums/218427-general/suggestions/5390053-add-actions-to-incontrol-http-get-post-run-file
It would allow the running of batch files.
It's the one thing I'm waiting on is more control like this and especially the HTTP post/get that will allow a lot more functionality with inControl.
Things seem to be quietening off here.
Josh
I have a .cs file that runs a .bat file correctly. I would be glad to share it with you but don't know how to. Is there a way you could send me an email?
Mike D.
Mike I sent you a Private Message with my email. Thanks.
Hi guys, perhaps you could just cut and paste the relevant lines of code here on this forum? If possible, that would be much appreciated.
Cheers.
I should add, I'm trying to get this working too but not having much luck. I followed the instructions in this thread: http://store.incontrolzwave.com/boards/topic/27/run-autoit-script-or-batch-file-with-a-scene/page/2
I have verified that the code in that thread works but NOT for launching an exe. This is what I'm trying to do.
Well it looks like it is in the planning stages so this might make it easier.
Josh
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;
}
}
}
Script to record a file from a Foscam camera and then run a .bat file with passed params:
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 Max80 : ScriptBase, ScriptInterface {
///
/// This script finds any node that is above 80% and sets it to 80%.
///
/// ALWAYS MAKE COPIES OF SCRIPTS YOU INTEND TO CUSTOMIZE OR YOUR CHANGES
/// COULD BE LOST.
///
public void runScript() {
var vidfileName = "";
// How long the recording should go
var recordTimeSeconds = 120;
try {
// *****************
// * Change these to fit your system ****
var vlcPath = @"C:\program files (x86)\VideoLan\VLC\vlc.exe";
// This is where the video will be saved. Be sure it has a trailing slash at the end ()
var videoOutputPath = @"C:\House\PorchVideos\thisWeek\";
// This is the camera's short id (double click a device inside InControl to find this id)
var deviceShortId = 30;
// *****************
// ******************
var device = getNodeByShortId(deviceShortId) as CameraDevice;
if (device != null) {
vidfileName = string.Format("{0}.asf", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));
var asfUrl = string.Format("http://{0}/videostream.asf?user={1}&pwd={2}", device.ip, device.userName, device.password);
var psi = new ProcessStartInfo();
psi.FileName = vlcPath;
//var args = string.Format("{0} --qt-start-minimized --no-qt-notification --run-time=15 :demux=dump :demuxdump-file={1}{2} vlc://quit", device.providerDeviceId, videoOutputPath, fileName);
//var args = string.Format("\"{0}\" --run-time={3} :demux=dump :demuxdump-file=\"{1}{2}\"", asfUrl, videoOutputPath, vidfileName, recordTimeSeconds);
//var args = string.Format("-I dummy --dummy-quiet \"{0}\" --run-time={3} --sout=#transcode{{vcodec=h264,vb=1024,acodec=mp3,ab=128,channels=2,samplerate=44100}}:std{{access=file,mux=avi,dst=\"{1}{2}\"}} vlc://quit", asfUrl, videoOutputPath, vidfileName, recordTimeSeconds);
//var args = string.Format("\"{0}\" --qt-start-minimized --no-qt-notification --run-time={3} :sout=#duplicate{{dst=file{{dst=Y:\\SecurityVids\\test.asf}},dst=display}} :sout-keep vlc://quit", asfUrl, videoOutputPath, vidfileName, recordTimeSeconds);
var args = string.Format("\"{0}\" --qt-start-minimized --no-qt-notification --run-time={3} :sout=#duplicate{{dst=file{{dst={1}{2}}}}} vlc://quit", asfUrl, videoOutputPath, vidfileName, recordTimeSeconds);
writeFileLog("CamRecord:" + args);
psi.Arguments = args;
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process.Start(psi);
}
} catch (Exception ex) {
// Log the exception here
var message = ex.Message;
writeFileLog("CamRecord:" + message);
}
try
Guys,
I am a retired programmer but do not know C# so I just modded the examples in the Scripts directory and other examples until I got it to work.
Let me know if you have any questions whatsoever as I would be glad to answer them.
Mike D.
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";
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.
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.
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.
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;
}
}
}
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?????
Maybe post the contents of the .bat file?
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.
Did you try what I posted in post #2263? It looks like it should work to me. I can't help anymore.
Mike D.
Yes and nothing happened. I am still on the hunt.
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.
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;
}
}
}
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.
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.
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
Check this one.....Run Bat file from script
Wells
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.
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.
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).
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.