How can I restart IIS from C# code running as a user who is an administrator?
Asked Answered
D

6

10

Typically (in Windows 7), installing a program will ask for permission to modify the system. As an administrator, I can give the authorization without supplying a password.

I'm trying to figure out how to take an administrator action (restart IIS) from C# code running as a user who is AN administrator, but the not THE "Administrator" account.

Darton answered 24/10, 2011 at 2:17 Comment(0)
W
5

To run a process as elevated you can use the runas verb.

Process elevated = new Process();
elevated.StartInfo.Verb = "runas";
elevated.StartInfo.FileName = "Whatever.exe";
elevated.Start();

For restarting IIS (as mentioned before) use iisreset.

Hope you find this useful.

Wellturned answered 24/10, 2011 at 9:25 Comment(2)
In a command prompt, when I type "runas /user:Administrator iisreset", it asks me for a password, even though the user who launched the command prompt is an administrator. I should not need a password.Darton
I don't think that the runas command is exactly the same as the runas verb. From the question I assumed that You would like to do this from code.Wellturned
P
4

For anyone still looking for this, here is code that I use to help me out with this.

    private static void DoIISReset()
    {
        Process iisReset = new Process();
        iisReset.StartInfo.FileName = "iisreset.exe";
        iisReset.StartInfo.RedirectStandardOutput = true;
        iisReset.StartInfo.UseShellExecute = false;
        iisReset.Start();
        iisReset.WaitForExit();
    }

Hope this helps!

Phung answered 6/7, 2016 at 11:42 Comment(2)
I using full path for filename : (@"C:\Windows\System32\iisreset.exe")Sunroom
How to do it but for remote iis?Melanite
P
3

Try to execute the IISReset command from C#

http://technet.microsoft.com/en-us/library/cc758159(WS.10).aspx

iisreset /noforce

Using ProcessStart

System.Diagnostics.Process.Start(@"C:\Windows\System32\iisreset.exe");

If you're using AD Authentication and you're an administrator this should work

Peipeiffer answered 24/10, 2011 at 2:22 Comment(2)
I'm not using AD authentication, I'm just on a home computer. And it didn't work.Darton
issreset.exe -> iisreset.exeRunck
T
3
System.Diagnostics.Process.Start(@"C:\Windows\System32\iisreset.exe");

This code help to you but you can get Access Denied.

For you to not get Access Denied:

  1. Right Click Project
  2. Add New İtem
  3. Add Application Manifest File
  4. Change this section

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

To this

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Triclinium answered 6/5, 2017 at 13:19 Comment(0)
F
0

There are two ways to do this but fr both you need to run VS as administration.

  1. This code will prompt in an empty cmd for some time and will close the window automatically.

    Process iisReset = new Process(); iisReset.StartInfo.FileName = "iisreset.exe"; iisReset.StartInfo.RedirectStandardOutput = true; iisReset.StartInfo.UseShellExecute = false; iisReset.Start(); iisReset.WaitForExit();

    1. this code will also restart IIS and it will prompt CMD with few processing.

      Process.Start(@"C:\WINDOWS\system32\iisreset.exe", "/noforce");

Frustum answered 31/7, 2018 at 11:33 Comment(0)
W
-1

Here is a link to how this is done in power shell http://www.computerperformance.co.uk/powershell/powershell_service_start.htm

Another possibility would be to use WMI http://www.motobit.com/tips/detpg_vbs-wmi-restart-service/

Here is another way directly in # http://www.csharp-examples.net/restart-windows-service/

I hope this helps....

Woolsey answered 24/10, 2011 at 4:32 Comment(3)
This does not answer the question around the requirement of doing it in C#Loader
@Dave Friedel, The last link has C# examples how to restart a service. So it does answer the questionWoolsey
I'll remove the down vote, but you do not provide the specifics for restarting IIS with a link to a remote site that doesn't answer the question. To provide value, some pseudo code outlining the action would be more useful to the community. - Update - my vote is locked unless the answer is edited. Plus it would lead them down other issues like the link here: #19764027Loader

© 2022 - 2024 — McMap. All rights reserved.