2012-12-11 26 views
2

我在遠程計算機上有一個批處理文件。通過右鍵單擊該文件並選擇「以管理員身份運行」選項運行該批處理文件。要以編程方式運行此批處理文件(位於遠程計算機),我使用C#ManagementScope類。但我無法找到任何選項使用ManagementScope類設置'以管理員身份運行'選項。如何使用C#運行遠程計算機程序「以管理員身份運行」#

我需要代碼解決方案(任何示例代碼都非常出色)來執行該批處理文件。

回答

1

你應該看看使用c# Process Class。它比使用WMI更快。

您可以通過用戶名&密碼憑據這樣的,它是一類我創建較早前:

class Logon : Process 
{ 
    internal Logon(string filename, string username, string passwordtxt, string argument) 
    { 
     StartInfo.Domain = "Your-Domain"; 
     StartInfo.FileName = filename; 
     StartInfo.UserName = username; 
     StartInfo.Password = GetSecurePassword(passwordtxt); 
     StartInfo.UseShellExecute = false; 
     StartInfo.Arguments = argument; 
     StartInfo.LoadUserProfile = true; 
    } 

    public System.Security.SecureString GetSecurePassword(string passwordtxt) 
    { 
     SecureString SS = new SecureString(); 
     foreach (char PSW in passwordtxt) 
     { 
      SS.AppendChar(PSW); 
     } 

     return SS; 
    } 
} 

在你的應用程序,你只需具備以下條件:

public void verifyuser(string filename, string argument) 
{ 
    try 
    { 
     var logon = new SecureLogon(
     filename, txtuser.Text, txtpassword.Text, argument); 

     logon.Start(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message,"Notification"); 
    } 
} 
+0

謝謝德里克的回覆。由於我需要運行遠程批處理文件,我可以在哪裏設置遠程計算機名稱或IP – user1894688

+0

您需要使用PSEXEC Service mate,Google it ..它允許您在遠程計算機上運行dos命令。 – Derek

0

你可以使用psexec來做這個。

Process.Start("psexec", "params"); 
+0

感謝巴厘島的回覆。通過使用此代碼,我們正在獲取「拒絕訪問」錯誤。任何進一步的信息將不勝感激 – user1894688

0

您是否嘗試將此添加到「批處理」的開頭?

runas /user:Administrator Example1Server.exe 
相關問題