You just need to enumerate the contents of your PSOutput variable. There’s an example of that on the page you linked:
// loop through each output object item
foreach (PSObject outputItem in PSOutput)
{
// if null object was dumped to the pipeline during the script then a null
// object may be present here. check for null to prevent potential NRE.
if (outputItem != null)
{
//TODO: do something with the output item
Console.WriteLine(outputItem.BaseObject.GetType().FullName);
Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
}
}
Here, you’re just adding the equivalent PowerShell command to launch your script that you would use at a shell prompt. The call operator (&) and the single-quotation marks are there just in case the path to your script happens to contain spaces.
using (PowerShell PowerShellInstance = PowerShell.Create())
{
string test = “Get-Content .\aa.txt | ForEach-Object{$_ -replace "\s+",","}”;
//PowerShellInstance.AddScript(test);[b] //Working[/b]
PowerShellInstance.AddScript(@"& 'D:\.........\bin\Debug\test.ps1'"); [b]//Not working[/b]
Collection PSOutput = PowerShellInstance.Invoke();
foreach (PSObject outputItem in PSOutput)
{
if (PowerShellInstance.Streams.Error.Count > 0)
{
// error records were written to the error stream.
// do something with the items found.
}
if (outputItem != null)
{
Console.WriteLine(outputItem);
}
}