簡體   English   中英

如何使用C#運行遠程計算機程序“以管理員身份運行”

[英]how to run a remote computer program 'run as administrator' using C#

我在遠程計算機上有一個批處理文件。 通過右鍵單擊該文件並選擇選項“以管理員身份運行”,可以運行該批處理文件。 要以編程方式運行此批處理文件(位於遠程計算機中),請使用C#ManagementScope類。 但是我找不到使用ManagementScope類設置“以管理員身份運行”選項的任何選項。

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

您應該研究使用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");
    }
}

您可以使用psexec來執行此操作。

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

您是否嘗試過將其添加到“批處理”的開頭?

runas /user:Administrator Example1Server.exe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM