Sunday, April 17, 2011

Restart iis on remote machine ( we have ip address ) by c# code

Hello How can restart iis on a remote machine? I know the ip address and admin user's user name and password information.

From stackoverflow
  • How remote is remote? You could just execute Shutdown.exe from your c# code? If that's not possible (firewalls etc) then the next best would probably be putting a service on there that you could call remotely (and securely!) that shuts the machine down.

  • You could use the "sc" command in order to control the iis-service on the remote machine.

    sc \\RemoteServer stop [iis-service-name]
    

    Use

    sc help
    

    in order to get a list of possible arguments.

    Also take a look at a microsoft kb-article on your subject.

  • I think shutdown.exe closes the computer but only I want is to restart the iis. I will restart a remote machine which is on the same network. And I know the ip address and user credential information.

    and how can I call sc command from c#?

  • You could use sc, as Thomas Franke suggested:

    sc \\RemoteServer stop iisadmin
    sc \\RemoteServer start w3svc
    

    or SysInternals' psexec. The PsTools suite is useful for these scenarios.

    psexec \\RemoteServer iisreset
    
  • ok thank you my code is like below but it does not work :( can you help me

    System.Security.SecureString pwd = new System.Security.SecureString();
            pwd.AppendChar('p');
            pwd.AppendChar('a');
            pwd.AppendChar('s');
            pwd.AppendChar('s');
            pwd.AppendChar('1');
            pwd.AppendChar('2');
            pwd.AppendChar('3');
            string computerName = "WORKPC1201";
            string domain = "EXTRAWORKDOMAIN";
            Process myProcess = new Process();
            //ProcessStartInfo remoteAdmin =
            //    new ProcessStartInfo("psexec.exe", domain + "\\\\" + computerName + " -i -c \"ie.bat\"");
            ProcessStartInfo remoteAdmin =
               new ProcessStartInfo("c:\\Windows\\System32\\sc.exe", "\\\\" + computerName + " stop iisadmin");
            remoteAdmin.UserName = "adminuser";
            remoteAdmin.Password = pwd;
            remoteAdmin.Domain = domain;
            myProcess.StartInfo = remoteAdmin;
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.Start();
            myProcess.WaitForExit();
    
    Matthew Talbert : How does it not work? Are there error codes? Have you tried running the command manually to make sure there isn't something else wrong?

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.