0

On running my below code, I get error on Powershell.create as below, has anyone seen this error before, I have Micosoft.Powershell.Sdk installled. What is wrong?? also I dont have InvokeAsync method in my powershell methods? why is that

Code:

public async Task<string> RunScript( string contentScript, Dictionary<string, EntityProperty> parameters)
        {
            List<string> parameterList = new List<string>();
            foreach( var item in parameters )
            {
                parameterList.Add( item.Value.ToString() );
            }
            using( PowerShell ps = PowerShell.Create() )
            {
                ps.AddScript( contentScript );
                ps.AddParameters( parameters );
                
                IAsyncResult async = ps.BeginInvoke();
                StringBuilder stringBuilder = new StringBuilder();
                foreach( PSObject result in ps.EndInvoke( async ) )
                {
                    stringBuilder.AppendLine( result.ToString() );
                }
                return stringBuilder.ToString();
            }
        }

Error:

System.NullReferenceException: 'Object reference not set to an instance of an object.' 5__4 was null.

4
  • Which version of .NET and which version of the SDk? Commented Feb 19, 2021 at 13:16
  • I see this when i click F12 on my PowerShell key word Assembly System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 // C:\Users\Test\.nuget\packages\powershellstandard.library\5.1.1\lib\netstandard2.0\System.Management.Automation.dll #endregion Commented Feb 19, 2021 at 13:19
  • Also Microsft Sdk version is 7.1.0 and I tried with latest one as well Commented Feb 19, 2021 at 13:20
  • If I make the method synchronous that is remove Task<string> with string I just get error as Object reference Not set Commented Feb 19, 2021 at 13:21

1 Answer 1

1

I fixed it by installing Microsoft.PowerShell.Sdk(7.0.0) and Microsft.WSMan.Runtime(7.0.0) version

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.