Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 运行空间Powershell命令的WSManConnectionInfo和PSCredential作为另一个用户_C#_Powershell_Asp.net Core Mvc - Fatal编程技术网

C# 运行空间Powershell命令的WSManConnectionInfo和PSCredential作为另一个用户

C# 运行空间Powershell命令的WSManConnectionInfo和PSCredential作为另一个用户,c#,powershell,asp.net-core-mvc,C#,Powershell,Asp.net Core Mvc,我一直在试图找出如何使Runspace Powershell命令以另一个用户的身份运行,我发现了如下示例: 但是,这不起作用,因为con.Credential是只读属性。我不知道这家伙怎么会说它管用 当我尝试用不同的方式做时: var cred = new PSCredential(objWINS + "\\" + objUser, objPW.ToSecureString()); var con = new WSManConnectionInfo(cred); WSManConnection

我一直在试图找出如何使Runspace Powershell命令以另一个用户的身份运行,我发现了如下示例:

但是,这不起作用,因为
con.Credential
是只读属性。我不知道这家伙怎么会说它管用

当我尝试用不同的方式做时:

var cred = new PSCredential(objWINS + "\\" + objUser, objPW.ToSecureString());
var con = new WSManConnectionInfo(cred);
WSManConnectionInfo
类需要更多信息,如Uri和Shell字符串。我只是尝试在这里运行Powershell命令,没有需要连接的Uri,我也不知道shell的用途,我在搜索中只能找到以下URL:

“”


这个类没有太多的信息,我真的无法收集到任何关于添加什么作为Uri的信息。

这就是为
WSManConnectionInfo
对象指定凭据的方式:

WSManConnectionInfo GetConnectionInfo(string computerName)
        {
            PSCredential creds = new PSCredential("UserName",
              GetSecurePassword("Password"));

            Uri remoteComputerUri = new Uri(string.Format("http://{0}:5985/wsman", computerName));
            WSManConnectionInfo connection = new WSManConnectionInfo(remoteComputerUri,
                "http://schemas.microsoft.com/powershell/Microsoft.PowerShell",
                creds);

            return connection;
        }

有关更多详细信息,请参见此(使用
GetSecurePassword
方法)

但是,当我尝试访问本地计算机时,为什么必须设置
remoteComputerUri
?如果必须-我应该在之前设置wsman吗?它只是一个名称,您可以将其从远程更改为本地:)
WSManConnectionInfo GetConnectionInfo(string computerName)
        {
            PSCredential creds = new PSCredential("UserName",
              GetSecurePassword("Password"));

            Uri remoteComputerUri = new Uri(string.Format("http://{0}:5985/wsman", computerName));
            WSManConnectionInfo connection = new WSManConnectionInfo(remoteComputerUri,
                "http://schemas.microsoft.com/powershell/Microsoft.PowerShell",
                creds);

            return connection;
        }