Start-process fails when psdebug is set to 2

This topic contains 2 replies, has 2 voices, and was last updated by  Slawko 3 days, 20 hours ago.

  • Author
    Posts
  • #93615

    Slawko
    Participant

    Hi All,
    I think I encountered some interesting issue in powershell (PSVersion 5.1.14393.206). Looks like a defect to me, but if not, please let me know what am I doing wrong.

    When PSDebug is set to "-trace 2" each attempt of running start-process and assigning its value (System.Diagnostics.Process object) to a variable ends with a failure "Cannot convert value to type System.String." I was digging for some time in the Internet but without success.

    Here is the example stripped down to bare minumum:

    PS C:\> Set-PSDebug -trace 2
    PS C:\> $a = start-process -Wait -PassThru calc
    DEBUG:    1+  >>>> $a = start-process -Wait -PassThru calc
    DEBUG:     ! CALL function ''
    DEBUG:   19+                                         if ( &  >>>> { Set-StrictMode -Version 1; $_.PSMessageDetails } ) {
    DEBUG:     ! CALL function ''
    DEBUG:   19+                                         if ( & {  >>>> Set-StrictMode -Version 1; $_.PSMessageDetails } ) {
    DEBUG:   19+                                         if ( & { Set-StrictMode -Version 1;  >>>> $_.PSMessageDetails } ) {
    DEBUG:    1+ &  >>>> { Set-StrictMode -Version 1; $this.Exception.InnerException.PSMessageDetails }
    DEBUG:     ! CALL function ''
    DEBUG:    1+ & {  >>>> Set-StrictMode -Version 1; $this.Exception.InnerException.PSMessageDetails }
    DEBUG:    1+ & { Set-StrictMode -Version 1;  >>>> $this.Exception.InnerException.PSMessageDetails }
    DEBUG:    1+ & { Set-StrictMode -Version 1; $this.Exception.InnerException.PSMessageDetails  >>>> }
    DEBUG:   19+                                         if ( & { Set-StrictMode -Version 1; $_.PSMessageDetails  >>>> } ) {
    DEBUG:   26+                                         $errorCategoryMsg = &  >>>> { Set-StrictMode -Version 1; $_.ErrorCategory_Message }
    DEBUG:     ! CALL function ''
    DEBUG:   26+                                         $errorCategoryMsg = & {  >>>> Set-StrictMode -Version 1; $_.ErrorCategory_Message }
    DEBUG:   26+                                         $errorCategoryMsg = & { Set-StrictMode -Version 1;  >>>> $_.ErrorCategory_Message }
    DEBUG:   26+                                         $errorCategoryMsg = & { Set-StrictMode -Version 1; $_.ErrorCategory_Message  >>>> }
    DEBUG:   42+                                         $originInfo = &  >>>> { Set-StrictMode -Version 1; $_.OriginInfo }
    DEBUG:     ! CALL function ''
    DEBUG:   42+                                         $originInfo = & {  >>>> Set-StrictMode -Version 1; $_.OriginInfo }
    DEBUG:   42+                                         $originInfo = & { Set-StrictMode -Version 1;  >>>> $_.OriginInfo }
    DEBUG:   42+                                         $originInfo = & { Set-StrictMode -Version 1; $_.OriginInfo  >>>> }
    Cannot convert value to type System.String.
    At line:1 char:1
    + $a = start-process -Wait -PassThru calc
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvalidCastFromAnyTypeToString
    

    When I get rid of "-wait" switch – it automagically works and we can see that in the example above it most probably tried to display line "SET $a = " and conversion of this object (supposed to be System.Diagnostics.Process) to string has failed due to whatever reason:

    PS C:\> $a = start-process -PassThru calc
    DEBUG:    1+  >>>> $a = start-process -PassThru calc
    DEBUG:     ! CALL function ''
    DEBUG:     ! SET $a = 'System.Diagnostics.Process (calc)'.
    

    When I switch PSDebug to trace 1 it also helps, because it does not try to display value assigned to a variable:

    PS C:\> Set-PSDebug -trace 1
    PS C:\> $a = start-process -Wait -PassThru calc
    DEBUG:    1+  >>>> $a = start-process -Wait -PassThru calc
    

    The code was simplified to depict the issue. Original code executes other executable, instead of calculator. Also – I am not able to modify the code, because I'm trying to write autotests for already existing implementation. Otherwise I would just execute the program without "-wait" switch and then would wait for it to finish. Lastly – I would be _really_ happy if I could keep "Set-PSDebug -trace 2".

    Did you have a chance to encounter such issue? Is there any hope for me? 😉

    —- edited

    Here are other examples that I tried and finished with the same error:

    PS C:\> [System.Diagnostics.Process] $a = start-process -Wait -PassThru calc
    PS C:\> [System.Diagnostics.Process] $a = (start-process -Wait -PassThru calc)
    PS C:\> $a = (start-process -Wait -PassThru calc)
    
  • #93669

    Martin Schvartzman
    Participant

    Looks interesting. I used the Trace-Command with the TypeConversion source (traces the type conversion algorithm) to get more information, but it reports failing a null to bolean conversion

    Trace-Command -PSHost -Name TypeConversion -Expression { $a = start-process -Wait -PassThru calc }

    Would this be an acceptable workaround?

    start-process -Wait -PassThru calc -OutVariable a | Out-Null
  • #93735

    Slawko
    Participant

    Thank you Martin, your solution is currently the best way to fix this issue... if I could modify the code. 😉 As I said I'm trying to automate the testing of existing implementation without touching it, if possible. I guess my only choice for now is to forget about PSDebug and submit enhancement to the code to fix that line using your workaround.

You must be logged in to reply to this topic.