1

Need your help in solving this following issue.

We have a powershell script like:

invoke-command -ScriptBlock { [cmdletbinding()]
param(
    [parameter(mandatory=$True)]
    [string] $ticktfilepath      
)
$ticketdetails=get-content $ticktfilepath |%{if ( $_ -like '"AB*' ) {$_}}|%{echo "$($_.Split(',')[7].Split('"')[1])=$($_.Split(',')[3].Split(':')[0].Split(' ')[$_.Split(',')[3].Split(':')[0].Split(' ').length-1])=$($_.Split(',')[0].Split('"')[1]);"}
write-output $ticketdetails } -ArgumentList 'D:\file.csv' 

This script reads a csv file and for those lines in the csv with “AB…” at the start, does some string parsing. The csv file passed has rows with the “AB…” and hence result are returned. This runs perfectly when executing through powershell console or ISE.

But as per our requirement, where we trying to execute the same script through: 1. System.Diagnostics.Process with process start info having file name as powershell.exe and the argument at the above script. It fails for –like. i.e at |%{if ( $_ -like '"AB*' ). It is always false for the condition even though it is expected to be true. N.B. other powershell script works perfectly with this approach 2. Exactly Similar issue when executed through System.Management.Automation.Runspaces So looks like some constraint using the “–like” operator. We even with the System.Diagnostics.Process approach tried writing the script line by line leveraging the Process.StandardInput.WriteLine(line) but then powershell hangs.

Any pointer to address this will be highly appreciated.

While using System.Diagnostics.Process, I used something like:

                ProcessStartInfo processStartInfo = new ProcessStartInfo();
                processStartInfo.UseShellExecute = false;
                processStartInfo.RedirectStandardError = true;
                processStartInfo.RedirectStandardOutput = true;
                processStartInfo.FileName = "powershell.exe";
                processStartInfo.Arguments = <ScriptContent>;
                Process powerShellProc = new Process();
                powerShellProc.StartInfo = processStartInfo;
                powerShellProc.Start();

                string successMessage = powerShellProc.StandardOutput.ReadToEndAsync().Result;
                string errorMessage = powerShellProc.StandardError.ReadToEndAsync().Result;
                powerShellProc.WaitForExit();

where, ScriptContent- is the above powershell script.

Instead of like, even tried with startswith but then also the same result. But with powershell console or iSE, it works perfectly.

1 Answers1

1

It's probably too late to answer :)

You shouldn't call powerShellProc.StandardOutput.ReadToEndAsync().Result before process actually finished. You can use Invoke-Executable function from How to capture process output asynchronously in powershell?

The following order is working

   $outTask = $oProcess.StandardOutput.ReadToEndAsync();
    $errTask = $oProcess.StandardError.ReadToEndAsync();
    $bRet=$oProcess.WaitForExit($TimeoutMilliseconds)
    $outText = $outTask.Result;
    $errText = $errTask.Result;
Community
  • 1
  • 1
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170