Getting Error "Object reference not set to an instance" while calling Pipeline.Invoke()

Nagarajan Rengamani 21 Reputation points
2021-02-17T14:03:45.397+00:00

Hi,

I am trying to execute get subscription using get-msolaccountsku. I am executing this script in .Net core application. Getting "Object reference not set to an instance of an object" error. Please help to resolve this.

        var scr = @"$username = '" + uName + @"'  

$password = ConvertTo-SecureString '" + Pwd + @"' -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
Connect-MsolService -Credential $creds
$getAzACC = Get-MsolAccountSku
$getAzACC";

        Runspace runspace = RunspaceFactory.CreateRunspace();  
        Runspace.DefaultRunspace = runspace;  
        runspace.Open();  
        StringBuilder stringBuilder = new StringBuilder();  
         

        var powershell = PowerShell.Create();  
         
        powershell.Commands.AddScript(scr);  

        Collection<PSObject> results1 = powershell.Invoke();  
        //runspace.Close(); // Really should be in a finally block  
        foreach (PSObject obj in results1)  
        {  
            stringBuilder.AppendLine(obj.ToString());  
        }  

This is code i am working. Getting "Object reference error" while executing Pipeline.Invoke.

@Rich Matheisen Please check my code.

Thanks.

Developer technologies | .NET | .NET Runtime
Windows for business | Windows Server | User experience | PowerShell
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

1 answer

Sort by: Most helpful
  1. Nagarajan Rengamani 21 Reputation points
    2021-02-17T15:48:13.4+00:00
            var scr = @"$username = '" + uName + @"'  
    

    $password = ConvertTo-SecureString '" + Pwd + @"' -AsPlainText -Force
    $creds = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
    Connect-MsolService -Credential $creds
    $getAzACC = Get-MsolAccountSku
    $getAzACC";

            Runspace runspace = RunspaceFactory.CreateRunspace();  
            Runspace.DefaultRunspace = runspace;  
            runspace.Open();  
            StringBuilder stringBuilder = new StringBuilder();  
             
    
            var powershell = PowerShell.Create();  
             
            powershell.Commands.AddScript(scr);  
    
            Collection<PSObject> results1 = powershell.Invoke();  
            //runspace.Close(); // Really should be in a finally block  
            foreach (PSObject obj in results1)  
            {  
                stringBuilder.AppendLine(obj.ToString());  
            }  
    

    This is code i am working. Getting "Object reference error" while executing Pipeline.Invoke.

    @Rich Matheisen @Viorel Please check my code.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.