Hi there,
I need to execute a PowerShell script from c sharp- console application. One of the object sends an array of string elements from PowerShell.
I am unable to get the arrays/objects/list items in c sharp. The values for the same are coming as null in console application.
Please suggest me asap.
Thanks.
PowerShell code: $sqlInstances = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances write-output "Sql Server is installed and the sql instances are: " $sqlInstances C sharp code- RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); Runspace.Open(); RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace); scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); PowerShell objPowershell = PowerShell.Create(); objPowershell.Runspace = runspace; var directory = Environment.CurrentDirectory; string filePath = System.IO.Path.Combine(directory, "Install-DIYMPS.ps1"); string scriptContents = File.ReadAllText(filePath); objPowershell.AddScript(scriptContents); Collection<PSObject> psObjects; psObjects = objPowershell.Invoke(); objPowershell.Dispose(); StringBuilder stringBuilder = new StringBuilder(); foreach (PSObject obj in psObjects) { if (obj != null) { stringBuilder.AppendLine(obj.ToString()); stringBuilder.AppendLine(); } } Console.Write(stringBuilder.ToString());