![](/img/trans.png)
[英]Piping in a file on the command-line using System.Diagnostics.Process
[英]Restart windows from asp.net page using command prompt and System.Diagnostics.Process
我正在嘗試使用System.Diagnostics.Process從Web服務內部重新啟動Windows Server 2003。
public static string Dorestart()
{
var si = new Process();
si.StartInfo.UserName = "administrator"; // Credentials of administrator user
var sc = new SecureString();
foreach (char c in "AdminPassword")
{
sc.AppendChar(c);
}
si.StartInfo.Password = sc;
si.StartInfo.UseShellExecute = false;
si.StartInfo.FileName = "cmd.exe";
si.StartInfo.Arguments = "\"c:\\windows\\system32\\shutdown.exe\" -r -f -t 0 -c \"Restart Reason\" -d p:4:1";
si.StartInfo.CreateNoWindow = true;
string res = "";
try
{
si.Start();
si.WaitForExit();
res = "Minor Job done... wait 2 minutes to complete action";
}
catch (Exception ex)
{
res= ex.Message;
}
si.Close();
si.Dispose();
return res;
}
對於文件名和參數部分,我也對此進行了測試:
si.StartInfo.FileName = "shutdown.exe";
si.StartInfo.Arguments = "/r /f /t 0 /c \"" + UReason + "\" /d p:4:1";
從RUN命令直接使用文件名和參數實際上會重新啟動PC,但是在Web服務上卻出現此錯誤:
在服務器桌面上:應用程序無法正確初始化(0xC0000142)。 單擊確定終止應用程序。
在事件日志中,我有這個:
Process information:
Process ID: 2676
Process name: w3wp.exe
Account name: NT AUTHORITY\NETWORK SERVICE
Exception information:
Exception type: HttpException
Exception message: Request timed out.
Request information:
Request URL: http://mywebsite.com/webservice.asmx
Request path: /webservice.asmx
User host address: <IP Address>
User:
Is authenticated: False
Authentication Type:
Thread account name: NT AUTHORITY\NETWORK SERVICE
Thread information:
Thread ID: 7
Thread account name: NT AUTHORITY\NETWORK SERVICE
Is impersonating: False
在Web應用程序上沒有錯誤。
如果有人告訴我如何解決此問題並使Web服務具有重啟功能,我將不勝感激。
終於成功了...
有很多問題。 我在此處記錄了該過程以供將來參考。 當心安全隱患。
多虧了Wjdavis5 ,我將AppPool Identity更改為Local System。
感謝從ASP .NET頁面運行批處理文件 ,我從代碼中刪除了幾行:
public string DoJob()
{
var si = new Process
{
StartInfo =
{
FileName = "shutdown.exe",
Arguments = "-r -f -t 0 -c "Shutdown Reason" -d p:4:1",
WorkingDirectory = "c:\\windows\\system32\\"
}
};
string res;
try
{
si.Start();
si.WaitForExit();
res = "<br />Minor Job done... wait 2 minutes to complete action<br />You can now close this window";
}
catch (Exception ex)
{
res = ex.Message;
}
si.Close();
si.Dispose();
return res;
}
為了消除安全風險,以及隱藏網站和Web服務的一些安全方法(例如,使用子域和非標准端口),我利用Uwe Keim的A小C#類中的模擬用戶來偽造上述方法內容,此代碼:
try
{
using (new Impersonator("Admin Username", ".", "Admin Password"))
{
// Above method Content
.
.
.
}
}
catch (Exception ex)
{
return "Invalid Username or Password";
}
此代碼檢查提供的憑據在服務器上是否有效。 我沒有在此應用程序上測試非管理用戶,因為該服務器沒有任何用戶。
隨時發表評論和糾正。 問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.