I am using C# to execute a PowerShell script:
using (PowerShell powerShellInstance = PowerShell.Create())
{
string query = string.Format(@"$server = ""{0}""
$db = ""master""
$sql = ""SELECT DISTINCT hostprocess FROM master.dbo.sysprocesses""
$user = ""{1}""
$pass = ""{2}""
$processId = Invoke-sqlcmd -Server $server -Database $db -Username $user -Password $pass -Query $sql | select -ExpandProperty hostprocess
Write-Output $processId", Server, DbUserName, DbPass);
powerShellInstance.AddScript(query);
var results = powerShellInstance.Invoke();
}
When I run the script in the PowerShell console, it returns 2 string values. But in C#, results has 2 elements but they are null. What is the problem here and why is the behavior different when executing it via C#?