如何以管理员身份运行Power shell
本文关键字:运行 Power shell 身份 管理员 | 更新日期: 2023-09-27 18:19:10
我的代码运行完美,当我运行它作为Admin。但是当我作为来宾运行它时,它给出了一个错误:
内部错误。
是否有任何方法我可以配置运行电源外壳作为管理编程?
string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
string password = "****";
System.Security.SecureString securePW = new System.Security.SecureString();
foreach (char c in password)
securePW.AppendChar(c);
securePW.MakeReadOnly();
string domainAndUsername = @"192.168.1.113'***";
string remoteMachineName = "****";
PSCredential remoteCredential = new PSCredential(domainAndUsername, securePW);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, remoteMachineName, 5985, "/wsman", shellUri, remoteCredential);
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo)) //_Error Here_//
我找到了一些解决方案,但它们是旧的,它们不再工作了:S
如果您使用Process,您可以使用以下命令作为admin运行:
var processStartInfo = new ProcessStartInfo("powershell")
{
UseShellExecute = true,
CreateNoWindow = true,
Verb = "runas",
//UserName = username,
//Password = MakeSecureString(password)
};
var exec = Process.Start(processStartInfo);
exec?.WaitForExit(0);
如果您不想要提示,那么您将不得不更改UAC。