-2

When users click on a button, I want it to run the logon script(launching from server), but each computer in different servers, so I get the server name. But the netlogon.StartInfo.Arguments = slnres + @"/c \netlogon\logon.cmd"; line is not working as it should be. It should run the logon.cmd on the PC(mapping network drivers, printers, etc), and then the CMD should close.

 private void MapNetwork_Click(object sender, EventArgs e)
    {
        Process sln = new Process();
        sln.StartInfo.UseShellExecute = false;
        sln.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        sln.StartInfo.FileName = "cmd.exe";
        sln.StartInfo.Arguments = "/c echo %logonserver%";
        sln.StartInfo.RedirectStandardOutput = true;
        sln.Start();
        string slnres = sln.StandardOutput.ReadToEnd();
        label1.Text = slnres;

        Process netlogon = new Process();
        netlogon.StartInfo.UseShellExecute = false;
        netlogon.StartInfo.FileName = "cmd.exe";
        netlogon.StartInfo.Arguments = slnres + @"/c \netlogon\logon.cmd";
        netlogon.Start();
    }
  • Your question not giving a clear idea about what you want to solve? or it's not clear.So try to add the result your getting (screenshot) prefered – Dhanil Dinesan Nov 25 '19 at 18:26
  • 2
    It looks like you can greatly simplify your task by using [System.Environment.GetEnvironmentVariable("logonserver")](https://stackoverflow.com/q/431836/205233). – Filburt Nov 25 '19 at 18:32
  • I would suggest reading: https://stackoverflow.com/help/how-to-ask to learn how to ask a question properly. If you follow the guidelines, you won't have as many downvotes and you will get an answer quicker. –  Nov 25 '19 at 18:37
  • Thanks for helping simplify my task, but after all stll just pop up and close. On my home PC I should get error because not finding the logon.cmd. Next time I'll check how-to-ask. – Tamás Varga Nov 25 '19 at 18:52
  • Can you describe what your overall goal is? Why are you trying to open a command prompt? – Gabriel Luci Nov 25 '19 at 18:53
  • My goal is to run this command: \\logonserver\netlogon\logon.cmd. This logon.cmd sometimes not runing automatically so this how manually possible to run. – Tamás Varga Nov 25 '19 at 18:57

2 Answers2

1

A couple things:

  1. You don't need to run a command prompt to get an environment variable. You can use Environment.GetEnvironmentVariable.

  2. Your Arguments property for your call to logon.cmd is being constructed into this:

\\myserver/c \netlogon\logon.cmd

When I think you want this:

/c \\myserver\netlogon\logon.cmd

So make sure you put slnres at the right place in your string. Your code should look like this:

private void MapNetwork_Click(object sender, EventArgs e)
{
    string slnres = Environment.GetEnvironmentVariable("logonserver");
    label1.Text = slnres;

    Process netlogon = new Process();
    netlogon.StartInfo.UseShellExecute = false;
    netlogon.StartInfo.FileName = "cmd.exe";
    netlogon.StartInfo.Arguments = "/c " + slnres + @"\netlogon\logon.cmd";
    netlogon.Start();
}
Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
  • `netlogon.StartInfo.Arguments = @$"/c {slnres}\netlogon\logon.cmd";` Few errors came out: Invailid expression term" ;expected Keyword, identifier, or string expected after verbatim specifier: @ Unrecognized escape sequence between netlogon and logon.cmd – Tamás Varga Nov 25 '19 at 19:12
  • The `$` is the [string interpolation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated) feature, which was introduced in C# 6. It sounds like you are using an older project and can't use that. I've updated my answer to use string concatenation instead. – Gabriel Luci Nov 25 '19 at 19:15
0

i am a little confused about your question and i am not rly sure if i understand you correctly. some time ago i made a program where i had to run few powershell commands, so i made a class for it. redirected to your button it would look like that:

(and remember you need the fqdn to your file location => Reading File From Network Location)

using System.Diagnostics;

    //class lvl scope vars
    string output;
    string ErrorOutput;

    private void MapNetwork_Click(object sender, EventArgs e)
    {
        //define process arguments
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = @"cmd.exe";
        startInfo.Arguments = @"FQDN path to your file on the server; exit";
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;

        //start process
        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();
        process.WaitForExit();

        //outpunt handling
        if (string.IsNullOrEmpty(ErrorOutput))
        {
            return output;
        }
        else
        {
            return ErrorOutput;
        }
    }
  1. first of all i would check if your application is able to open the file one the shared network location. (server available? access rights to server? serer mapped?)
  2. after that you can check if he is able to start the file locally. (does it need admin rights to run the *.cmd, *.bat file)
  3. now you can check if your application runs it correctly.
Dave
  • 120
  • 1
  • 10