【问题标题】:Why do I get exceptions when trying to use .Net PowerShell Runspace to connect to my local Windows 10 machine?尝试使用 .Net PowerShell Runspace 连接到本地 Windows 10 计算机时,为什么会出现异常?
【发布时间】:2020-08-17 23:01:17
【问题描述】:

我正在尝试通过 .Net 使用 PowerShell 连接到我的 本地计算机 并在其上运行一些脚本。我正在使用 Visual Studio 2019。 当我尝试打开 Runspace 时,我不断收到异常。

考虑以下代码 sn-p:

private SecureString ConvertToSecureString(string password)
{
    var secure = new SecureString();
    foreach (char c in password)
    {
        secure.AppendChar(c);
    }
    return secure;
}

private void foo()
{
    var credentials = new PSCredential("TestUser", ConvertToSecureString("TestPWD"));
    var connectionInfo = new WSManConnectionInfo();
    var addr = "192.168.1.123"  // "localhost", "."
    var connectionInfo = new WSManConnectionInfo(false, addr, 5985, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", credentials, 15000);

    var testRunSpace = RunspaceFactory.CreateRunspace(connectionInfo);
    testRunSpace.Open();
}

除了“localhost”和“.”之外,我还尝试使用我机器的 IPAddress。 我收到以下异常消息:

  • IPAddress -> “连接到远程服务器 192.168.1.121 失败,错误消息如下:WinRM 客户端无法处理请求...”
  • "localhost", "." -> "...访问被拒绝。有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。"

我已签入 PowerShell,WinRM 服务正在我的本地计算机上运行。

我在这里做错了什么。

是否需要在我的本地计算机上完成一些配置/设置才能使其正常工作?

谢谢,约翰B

【问题讨论】:

  • 您尝试过以管理员身份运行 Visual Studio?
  • @derekbaker783 - 试过了,但结果还是一样。

标签: .net powershell winrm runspace


【解决方案1】:

好的,所以我想通了。 我需要确保我调用了“Enable-PSRemoting”以确保启用了远程处理。 然后“localhost”和“.”都起作用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多