so im having this weird issue with a win 7 pc. I have this application that runs this powershell script from a .bat file.
This is the code:
public void GetLastestEarthBackground()
{
var directoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts");
var vbsFile = Path.Combine(directoryPath, Settings.VBSFileName);
if (File.Exists(vbsFile))
{
var process = new Process
{
StartInfo =
{
WorkingDirectory = directoryPath,
Verb = "runas",
UseShellExecute = true,
FileName = "run.bat", //Settings.VBSFileName,
Arguments = "//B //Nologo"
}
};
process.Start();
process.WaitForExit();
}
}
When i run it from my application i see this in the command line:
But if i double click on the batch file i get this resut:
Any ideas?
This seems like a issue with user rights, the program running the script probably does not have the same rights as your user.
Related
I am basically trying to create and run a Windows Service on a server. This service has mainly 2 jobs,
To log a timestamp to a file on a UNC (different Server path) so as to check access
Call a simple console app using a different user
Now, I have created the windows service, but it won't log in the Server path as it is started via Local system. So I tried to deploy it using a user account with access to the Server Path and it logged, but then the whole point is to us the local system.
So now, i changed the requirement a little bit, Now is it possible to deploy the service as Local System only , but instead of directly logging on the server path, i would rather make the console app do the logging. But how should I make the console app run as a different user account.
This is the code I am using for calling the console app via a different user (mind you the service is run under LocalSystem), and it is either giving the error, "Cannot find the file specified" or "Invalid Directory Name" but i assure you both the things exist. I even used the paths fed in the startInfo by debugging to check.
public static void InitiateCommandProcess()
{
Process process = new Process();
SecureString ssPwd = new SecureString();
string fileName = ConfigurationManager.AppSettings["FileName"];
string workingDirectory = ConfigurationManager.AppSettings["ApplicationDirectory"];
string param = ConfigurationManager.AppSettings["PARAM"];
string userName = "myUserName";
string password = "MyPassWord";
for (int x = 0; x < password.Length; x++)
{
ssPwd.AppendChar(password[x]);
}
ProcessStartInfo startInfo = new ProcessStartInfo
{
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
ErrorDialog = false,
FileName = #"" + fileName,
WorkingDirectory = #"" + workingDirectory,
Arguments = #"" + param,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
Domain = "EMEA",
UserName = userName,
Password = ssPwd
};
process.StartInfo = startInfo;
try
{
Logger.Log($"Working in Directory Path : {workingDirectory} );");
Logger.Log($" Username : {userName} and Executing : {fileName}");
process.Start();
Logger.Log($"Process \"{ConfigurationManager.AppSettings["FileName"]}\" Executed at {DateTime.Now}");
}
catch (Exception ex)
{
Logger.Log($"Error occured in command process execution, Message : {ex.Message}", true);
}
}
The server path is like this, "\dfs\folder\folder..."
I am a little new to this so please bear with me.
I am trying to run an exe file from my own machine:
string versionInFolder = #"c:\test.exe";
public void Install(string versionInFolder)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
UseShellExecute = false,
FileName = versionInFolder,
WindowStyle = ProcessWindowStyle.Hidden,
};
using (Process process = Process.Start(processStartInfo))
{
process.WaitForExit();
}
}
This file exist and can run manually but i got this error:
System.ComponentModel.Win32Exception: 'The requested operation
requires elevation'
I found this post but did not understand the reason for this error and how to solve it.
You need to run your programm as admininistrator.
Check that first.
And if that doesn't work, or if you're the administrator, try to move your file in another place.
As discussed in other post, I came to know that Verb = "runas" works as elevated.
I need to run "logman.exe" arguments with Elevated privileged. With below code, I am not getting any output,
try
{
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "logman.exe",
Arguments = "PerfCounterCustom",
Verb = "runas",
RedirectStandardOutput = true,
CreateNoWindow = true,
}
};
process.Start();
string lineData;
while ((lineData = process.StandardOutput.ReadLine()) != null)
{
if (lineData.Contains("Root Path:"))
{
Console.WriteLine(lineData.Trim());
}
}
process.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Note - When I am running above EXE, right click as Admin, I m getting the output.
What changes required so that I can make Elevated through code in C# and output?
Process.Start() can use the OS or Shell (Explorer.exe) to spawn a new process, but only the Shell will prompt for elevation.
Thus you have to specify UseShellExecute=true in ProcessStartInfo, according to this answer: processstartinfo-verb-runas-not-working
UseShellExecute=false will allow you to capture Standard Output and Standard Error messages.
Is there a way to capture a response from the Process class using it to launch Internet Explorer.
Process proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #"C:\Program Files\Internet Explorer\iexplore.exe",
Arguments = "http://www.google.com",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
System.Threading.Thread.Sleep(3000);
proc.Kill();
I tried :
while (!proc.StandardOutput.EndOfStream) {
string line = proc.StandardOutput.ReadLine();
// do something with line
}
but IE sends back an immediate response of hey, i opened and called the URL. Anyone have any ideas?
There is no way of getting information from IE when it is running in a separate process.
However, you can run it inside of a Web Browser control in your own Form. IE then exposes a number of events that you can listen to.
I have problem with running cmd or exe file from asp.net page.
I used this code:
protected void btnRun_OnClick(object sender, EventArgs e)
{
var p = new Process();
var info = new ProcessStartInfo
{
FileName = Server.MapPath("~/print.cmd"),
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
};
p.StartInfo = info;
p.Start();
p.WaitForExit();
}
When I run my web application on VS Development Server everything is ok,
but when I deploy this code on IIS (ver 7.5), the code working without errors but
nothing happens.
I used search on google and SO, but I didn't find any solution
I tried http://support.microsoft.com/kb/555134 and http://support.microsoft.com/default.aspx?scid=kb;en-us;317012
I set 'Allow service to interact with desctop' for IIS Admin service.
I played with user rights but this didn't give me any results.
I changed Identity for Application Pools
is it possible to run cmd or exe with IIS?
Try:
var info = new ProcessStartInfo
{
FileName = Server.MapPath("~/print.cmd"),
UseShellExecute = true,
Verb = "runas",
WindowStyle = ProcessWindowStyle.Hidden,
};