Automate the .aspx through windows task scheduler in server - c#

I have an .aspx in that it will send email to the employees. I want this page should be automatically send to employees everyday. So i have published in IIS and created an windows task scheduler in the server "C:\Program Files\Internet Explorer\IEXPLORE.EXE""http://syspexsap03/AUTOEMAIL/Default.aspx" It works fine "if the operations runs successfully" i receive email after that still IE is working in the background apps in server.so immediately it cannot run the next task..It works locally.In case of server when i deploy no use. I have used this codes to kill the process:
Process[] AllProcesses = Process.GetProcesses();
foreach (var process in AllProcesses)
{
if (process.MainWindowTitle != "")
{
string s = process.ProcessName.ToLower();
if (s == "iexplore" || s == "iexplorer")
process.Kill();
}
}
and tried to do with the process id no use every time it generate new Pid:
Process p = Process.GetProcessById(15844);
p.Kill();
Process p1 = Process.GetProcessById(13380);
p1.Kill();
Process p2 = Process.GetProcessById(196);
p2.Kill();
How to schedule the aspx page in the windows task scheduler and kill the IE at the same time to run the next task ? Should run everyday on a specified time.. Please guide me..

In order to make an activity which runs at a particular schedule you don't need an aspx page or web application and publish in IIS for it.
It should be a console application where the .exe file can be scheduled in the window task scheduler at a particular time.Task Scheduler runs ordinary EXEs.
Also if you are sending email to employees which belong to same domain then there is no limitation on mails per day.
But if the email are not of the same domain then there is an limitation. Link below in order to check the limitation
http://group-mail.com/sending-email/email-send-limits-and-options/

Related

Give commands to multiple RDP and wait for result

I have a repetitive task at the end of the month to give commands to multiple Remote Desktop Connections (Win7, Win Server 2008, Win server 2012, Win 8 ...) and i need to open all of them one by one to do this task. I want somekind of tool that would log on each and every one of them and give commands.
Here is what i tried :
public Form1()
{
InitializeComponent();
rdp.Server = "1.2.3.4";
rdp.UserName = "Rmlabuser2";
IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
secured.ClearTextPassword = "Rmlabuser2";
rdp.Connect();
// open cmd.exe and give commands like VER and return output into a message text box
// rdp.SecuredSettings.StartProgram = #"c:\windows\System32\cmd.exe";
}
Full code :
http://www.codeproject.com/Articles/43705/Remote-Desktop-using-C-NET
Any ideeas?
Thanks.
You can use psexec to run commands on remote computer.
If you need to run commands within active session, you can create a scheduled task on that computer that will do the needed stuff. Scheduled tasks can be configured to run under currently logged-in session or from system/predefined account.
There are a number of C# libraries that can work with windows scheduled tasks. For example http://taskscheduler.codeplex.com/

How to start an exe from a .NET Windows Service for updating the service

I have a windows service that I would like to be automatically and silently updated. I started using wyBuild to implement this, but have had some issues with it, and decided to try to build my own. I've written a standalone exe that can be called to do the update procedure: checks for a new zip file with the update, downloads it, unzips, stop the windows service, copy files from the zip, then restart the service. This exe works fine when I run it from the commandline and wasn't really difficult to write.
However, now I would like the service (the same one being updated) to shell out to the updater exe to update itself. I first tried Process.Start:
var proc = Process.Start(pathToUpdaterExe);
proc.WaitForExit(60000);
This called the updater, but when the updater stops the service, the process is killed and the update stops. I did some searching and it sounds like the solution is to use a separate AppDomain. This is what I have now:
Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;
Evidence objEvidence = new System.Security.Policy.Evidence(baseEvidence);
AppDomainSetup setup = new AppDomainSetup();
var updateDomain = AppDomain.CreateDomain("updateDomain", objEvidence, setup);
updateDomain.ExecuteAssembly(updater);
AppDomain.Unload(updateDomain);
However, now I get the error System.IO.IOException: "The process cannot access the file 'C:\Program Files (x86)\Company\Service\Service.dll' because it is being used by another process" when attempting to copy over the new Service.dll
Again, I've stopped the service at this point. I've confirmed this with logging. I can't imagine what would have Service.dll still locked, so I added code to check to see what is locking it:
public static IEnumerable<Process> GetProcessesLocking(string filePath)
{
var result = new List<Process>();
result.Clear();
var processes = Process.GetProcesses();
foreach (Process proc in processes)
{
try
{
if (proc.HasExited) continue;
foreach (ProcessModule module in proc.Modules)
{
if ((module.FileName.ToLower().CompareTo(filePath.ToLower()) == 0))
{
result.Add(proc);
break;
}
}
}
catch (Exception ex)
{
Log(ex.ToString());
Log("There was an error checking " + proc.ProcessName );
}
}
return result;
}
However this code indicates that nothing has a lock on the dll (result is empty and nothing is logged indicating an error).
I suspect I'm running afoul of some UAC issue that is the real cause of the IOException. The windows service runs as LocalSystem. All that to ask: How should I be running the update exe from the windows service such that it has rights to copy files in c:\Program Files?
Update
As the comments and answer suggest, Process.Start can work, but there is some nuance. You have to start cmd.exe and use it to start the updater. I also found I could not use a full path for the updater exe and that I needed to set UseShellExecute=false. This is my final working code that launches the updater from the .NET service:
var cmd = "/c start updater.exe";
var startInfo = new ProcessStartInfo("cmd.exe");
startInfo.Arguments = cmd;
startInfo.WorkingDirectory = AssemblyDirectory;
startInfo.UseShellExecute = false;
var proc = Process.Start(startInfo);
I did this exact thing - using a simpler (some might say kludgy) approach. The service:
Produces a batch command,
Downloads the new executables to a staging location,
Starts a process: cmd.exe which, in turn, runs the batch script w/o waiting for it to complete, and then
Immediately terminates itself.
The batch command:
Pings 127.0.0.1 five times,
Copies the executables to the final location and,
Restarts the service.
Works like clockwork. The ping is a reliable 5 second delay - lets the service shutdown before copying the files.
Edit:
Just for completeness - I realized that by batch cmd is pinging 127.0.0.1 not 128.0.0.1 and so I edited this answer to reflect that. I suppose either works - but 128.0.0.1 pings timeout, where 127.0.0.1 resolves to "me". Since I'm only using it as a poor-man's delay, it serves the purpose either way.

how can I check and kill another instance of my application?

I face to a problem that my customer is already running the application appA. Then they go to desktop (not kill the appA), and upgrade the application with the version up by run appA.ps1 with PowerShell. After that, click on appA after installed -> get an exception. I guess the root cause is that have another instance is running.
My question is how can I check my application is already running? Can I kill it?
Additional, my application is windows 8 store, c#.
If you wand to keep your current instance running and kill all other instances of your application, you can do this:
using namespace System.Diagnostics;
...
// get current process
Process current = Process.GetCurrentProcess();
// get all the processes with currnent process name
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
process.Kill();
}
}
From Social MSDN:
All the Metro-style applications work in the highly sandboxed environment and there is no way to directly start an external application.
You cannot access windows app processes. Unfortunately, you aren't able to check for running windows apps.
I didn't find another way so far.
No Metro-style application
At first, you need the processes:
var processes = Process.GetProcessesByName("your application name")
To check whether your program is already running:
var isRunning = processes.Length > 1;
Then you loop through them and close the processes:
foreach (var process in processes)
{
process.CloseMainWindow();
// OR more aggressive:
process.Kill();
}

c# Open IE From Scheduled Task

I have a c# console application that I want to run from task scheduler that has 2 main functions: 1) Closes all Internet Explorer processes; and 2) Restarts Internet Explorer and loads the appropriate website.
The console app does exactly what it is supposed to do if run from the command line, but fails if executed from Task Scheduler.
The app is designed to run on the client computer the only function of which is to load a single website and broadcast the website to our internal TV Channel 195. We have connection issues with our ISP and while the connection issue is usually temporary, Internet explorer needs to be restarted to re-show the website.
I want to set it up to run multiple times each day to eliminate any possible connection issues between the web server and the client.
private static void StartExplorer()
{
Process _process;
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "iexplore.exe",
Arguments = "-noframemerging -private -k \"http://tv.TheelmAtClark.Com\""
};
try{
_process = Process.Start(psi);
}
catch(Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
}
Is it possible to run the app using task scheduler?
I would recommend that you look at alternative approaches, if possible.
A Firefox plugin like Reload Every is designed to do just this. I use this in our to project to a big screen TV.
However, if you are keen on doing this via Internet explorer, again there are two approaches
1) Something similar to the Firefox plugin I mentioned above - Autorefresher for IE
2) If you insist on having a task scheduler, as you mentioned above, here is how I think you can do it-
To kill all Internet Explorer instances, use PSKill. Invoke it via Process.Start with arguments to kill Internet Explorer.
To launch a new instance, try invoking Process.Start with UseShellExecute=true.

Closing process start from windows application on application close or exit

I am working on windows application. i have to run some window exe from my app, i am able to do the same but when i close my application these exe remains on running condition, i am not getting how can i close those exe. Please suggest some tips.
To run the Process
private void StartChildProcess(string fileName)
{
Process newProcess = new Process();
newProcess.StartInfo = new ProcessStartInfo(fileName); ;
newProcess.Start();
localProcess.Push(newProcess);
}
To close the process
private void CloseStartedProcesses()
{
while (localProcess.Count > 0)
{
Process process = localProcess.Pop();
if (process != null && !process.HasExited)
{
process.CloseMainWindow();
process.Close();
}
}
}
Some options:
Setup some communication system so the Main application can alert the other application to shutdown (read up on some WCF information or remoting)
Create a do.shutdown file and let the second application check if that file exists, simple but efficient.
Use the process.Kill options
Use Sendkey or equivalent to send a 'quit' key combination
Use Windows API - P/Invoke. FindWindow() or EnumWindows() to get the window handle. Then you can send WM_CLOSE or WM_QUIT to end the application via the SendMessage() function.
Note that if the application checks for user input on exiting (like a MessageBox asking weather the user really wants to quit) the only option might be to send WM_DESTROY which would be equivalent to Process.Kill (at least in respects to causing data loss - I am not certain it is the absolute equivalent).
Try this:
Process[] p = Process.GetProcessesByName("osk");
foreach (var item in p)
{
item.Kill();
}
The reason that the EXE you've ran from your application doesn't terminate once you close your application is probably because the 2nd application runs as a DIFFERENT, SEPARATE process.
If you run another process with System.Diagnostics.Process, it will remain in background until terminated manually or until it finishes it's job.
try this Process proc = Process.GetProcessesByName("processname");
proc.Kill();

Categories