Home > database >  Converting from Start-process to System.Diagnostics.Process
Converting from Start-process to System.Diagnostics.Process

Time:07-08

We have working powershell script ran through start-process

$EncodedCommand = 'JABFAHIAcgBvAHIALgBjAGwAZQBhAHIAKAApAA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIABbAHMAdAByAGkAbgBnAF0AIAAkAEUAbgBjAG8AZABlAGQAQQByAGcAdQBtAGUAbgB0AHMAIAA9ACAAJwBQAEEAQgBQAEEARwBJAEEAYQBnAEIAegBBAEMAQQBBAFYAZwBCAGwAQQBIAEkAQQBjAHcAQgBwAEEARwA4AEEAYgBnAEEAOQBBAEMASQBBAE0AUQBBAHUAQQBEAEUAQQBMAGcAQQB3AEEAQwA0AEEATQBRAEEAaQBBAEMAQQBBAGUAQQBCAHQAQQBHAHcAQQBiAGcAQgB6AEEARAAwAEEASQBnAEIAbwBBAEgAUQBBAGQAQQBCAHcAQQBEAG8AQQBMAHcAQQB2AEEASABNAEEAWQB3AEIAbwBBAEcAVQBBAGIAUQBCAGgAQQBIAE0AQQBMAGcAQgB0AEEARwBrAEEAWQB3AEIAeQBBAEcAOABBAGMAdwBCAHYAQQBHAFkAQQBkAEEAQQB1AEEARwBNAEEAYgB3AEIAdABBAEMAOABBAGMAQQBCAHYAQQBIAGMAQQBaAFEAQgB5AEEASABNAEEAYQBBAEIAbABBAEcAdwBBAGIAQQBBAHYAQQBEAEkAQQBNAEEAQQB3AEEARABRAEEATAB3AEEAdwBBAEQAUQBBAEkAZwBBACsAQQBBADAAQQBDAGcAQQBnAEEAQwBBAEEAUABBAEIAUABBAEcASQBBAGEAZwBBAGcAQQBGAEkAQQBaAFEAQgBtAEEARQBrAEEAWgBBAEEAOQBBAEMASQBBAE0AQQBBAGkAQQBEADQAQQBEAFEAQQBLAEEAQwBBAEEASQBBAEEAZwBBAEMAQQBBAFAAQQBCAFUAQQBFADQAQQBJAEEAQgBTAEEARwBVAEEAWgBnAEIASgBBAEcAUQBBAFAAUQBBAGkAQQBEAEEAQQB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AHIAbgAgAHIAZQBwAGwAYQBjAGUAKQANAAoAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAJABVAHMAZQByACAAfAAgAEUAeABwAG8AcgB0AC0AQwBsAGkAWABNAEwAIAAtAFAAYQB0AGgAIAAiAEMAOgBcAFUAcwBlAHIAcwBcAFAAdQBiAGwAaQBjAFwAVABNAFAAXABSAGUAcwB1AGwAdAAuAHgAbQBsACIAIAAtAEYAbwByAGMAZQANAAoAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAdAByAHkAewAgAHMAdABvAHAALQB0AHIAYQBuAHMAYwByAGkAcAB0ACAAfQBjAGEAdABjAGgAewB9AA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAjAGUAeABpAHQADQAKACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAjAGUAbgBkAHIAZQBnAGkAbwBuAA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAB9AA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAANAAoAIAAgACAAIAAgACAAIAAgACAAIAAgACAASQBuAHYAbwBrAGUALQBDAG8AbQBtAGEAbgBkACAALQBTAGMAcgBpAHAAdABCAGwAbwBjAGsAIAAkAFMAYwByAGkAcAB0AEIAbABvAGMAawAgAC0AQQByAGcAdQBtAGUAbgB0AEwAaQBzAHQAIAAgACQAQQByAGcAdQBtAGUAbgB0AHMA'

$Arguments =  '-Noexit', '-NoLogo', '–NoProfile', '-ExecutionPolicy RemoteSigned', "-EncodedCommand $EncodedCommand"

# line below executed successfully
start-process -filepath powershell -ArgumentList $Arguments

Now we should transform our code to use System.Diagnostics.Process

$Process = New-Object System.Diagnostics.Process

$ProcessStartInfoParam = [ordered]@{
    Arguments              = ( "'"   ( $Arguments -join "', '" )   "'" )
    CreateNoWindow         = $False
    FileName               = 'powershell'
    WindowStyle            = 'Normal'
    LoadUserProfile        = $False
    UseShellExecute        = $False
}

$ProcessStartInfo = New-Object -TypeName 'System.Diagnostics.ProcessStartInfo' -Property $ProcessStartInfoParam

$Process.StartInfo = $ProcessStartInfo
$StartResult = $Process.Start()

We have no script output and $StartResult is true. Also trying to remove '-Noexit' argument. I want to have new window with output like in first example.

CodePudding user response:

You are joining the arguments incorrectly. Command-line arguments must be separated by space and quoted using double-quotation marks. In your case you don't need quoting as the arguments don't contain whitespace.

Try this:

$ProcessStartInfoParam = [ordered]@{
    Arguments              = $Arguments -join ' '
    CreateNoWindow         = $False
    FileName               = 'powershell'
    WindowStyle            = 'Normal'
    LoadUserProfile        = $False
    UseShellExecute        = $False
}
  • Related