PowerShell / PowerShellStandard

MIT License
158 stars 24 forks source link

PSCredential class is broken when using this library with .netcore or .netframework #74

Closed youcefnb closed 4 years ago

youcefnb commented 4 years ago

give PSCredential a username and secure string via "PSCredential creds = new PSCredential("username",secureString);" is not working. When the code runs, the object that is returned shows the username and password as nulls. This is not a problem when running this code in .net 4.7.2. This issue is present with .netcore and maybe .netstandard.

You can reproduce this issue with the following code. Please run in .netcore.

        var secureString = new SecureString();
        var username = "username";
        foreach (char c in @"*password")
        {
            secureString.AppendChar(c);
        }

        PSCredential creds = new PSCredential(
            username,
            secureString);    
SeeminglyScience commented 4 years ago

@findyoucef This is just a reference library. It's intended mainly for binary modules as PowerShell's real assemblies will already be loaded into the process.

If you're looking to host PowerShell in an netcoreapp application, you'll need to reference Microsoft.PowerShell.SDK instead.

youcefnb commented 4 years ago

@findyoucef This is just a reference library. It's intended mainly for binary modules as PowerShell's real assemblies will already be loaded into the process.

If you're looking to host PowerShell in an netcoreapp application, you'll need to reference Microsoft.PowerShell.SDK instead.

Thanks for the reply. I'm building a .netstandard library that needs to have some powershell functionality. I need the library to be .netstandard because I need to be able to leverage with .netcore, and .netframework 4.7.2, but Microsoft.PowerShell.SDK doesn't support .netstandard

Though I'm still confused about why this code works in in .net 4.7.2, but not .netcore??

Thanks for your help

Youcef

SeeminglyScience commented 4 years ago

Thanks for the reply. I'm building a .netstandard library that needs to have some powershell functionality. I need the library to be .netstandard because I need to be able to leverage with .netcore, and .netframework 4.7.2, but Microsoft.PowerShell.SDK doesn't support .netstandard

How are you testing the library? What do you expect it to be loaded in? If you're loading it outside of a PowerShell process (or anything that already has the PowerShell assemblies loaded) then you'll get an empty implementation. If you look at the source in this repo, all of the implementations look like public string PropertyName => default.

Though I'm still confused about why this code works in in .net 4.7.2, but not .netcore??

In 4.7.2 it's probably pulling from the GAC instead of the assembly you're shipping with. Core doesn't look in the GAC, so it's loading the reference library instead.

youcefnb commented 4 years ago

Thanks for the reply. I'm building a .netstandard library that needs to have some powershell functionality. I need the library to be .netstandard because I need to be able to leverage with .netcore, and .netframework 4.7.2, but Microsoft.PowerShell.SDK doesn't support .netstandard

How are you testing the library? What do you expect it to be loaded in? If you're loading it outside of a PowerShell process (or anything that already has the PowerShell assemblies loaded) then you'll get an empty implementation. If you look at the source in this repo, all of the implementations look like public string PropertyName => default.

Though I'm still confused about why this code works in in .net 4.7.2, but not .netcore??

In 4.7.2 it's probably pulling from the GAC instead of the assembly you're shipping with. Core doesn't look in the GAC, so it's loading the reference library instead.

Thanks! This helped. I got everything up and running.