-1

I'm trying to use PowerShell through .Net to connect to and and run some scripts on my local machine. I'm using Visual Studio 2019. I keep getting exceptions when I attempt to open my Runspace.

Consider the following code snippet:

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();
}

I've tried using the IPAddress of my machine in addition to "localhost" and "." I get the following exception messages:

  • IPAddress -> "Connecting to remote server 192.168.1.121 failed with the following error message : The WinRM client cannot process the request..."
  • "localhost", "." -> "... Access is denied. For more information, see the about_Remote_Troubleshooting Help topic."

I've checked in PowerShell and the WinRM service is running on my local machine.

What am I doing wrong here.

Is there some configuration/setting which needs to be completed on my local machine in order for this to work?

Thanks, JohnB

JohnB
  • 3,921
  • 8
  • 49
  • 99

1 Answers1

-1

OK, so I figured it out. I needed to make sure that I called "Enable-PSRemoting" to make sure remoting was enabled. Then both "localhost" and "." worked.

JohnB
  • 3,921
  • 8
  • 49
  • 99