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.

8
  • BTW, Invoke-Command does not use cmdlet binding, so it useless here. Commented Nov 6, 2015 at 12:00
  • One addition: While using process or runspace (as mentioned below) we are leveraging script block to pass the script. As we have limitation that we cant use physical file. Commented Nov 6, 2015 at 12:20
  • Could you show exact command you tried? I try it myself and -like work as it should. Commented Nov 6, 2015 at 12:20
  • 1
    How about this: processStartInfo.Arguments = <ScriptContent>; -> processStartInfo.Arguments = "-Command \""+Regex.Replace(<ScriptContent>,@"""|\\(?=\\*"")",@"\$&")+"\"";? Commented Nov 6, 2015 at 12:49
  • 1
    I just trying to escape any characters, treated as special by PowerShell command line parser. Commented Nov 7, 2015 at 12:21

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.