从Start-process转换为System.Diagnostics.Process



我们通过start-process运行了powershell脚本

$EncodedCommand = 'JABFAHIAcgBvAHIALgBjAGwAZQBhAHIAKAApAA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIABbAHMAdAByAGkAbgBnAF0AIAAkAEUAbgBjAG8AZABlAGQAQQByAGcAdQBtAGUAbgB0AHMAIAA9ACAAJwBQAEEAQgBQAEEARwBJAEEAYQBnAEIAegBBAEMAQQBBAFYAZwBCAGwAQQBIAEkAQQBjAHcAQgBwAEEARwA4AEEAYgBnAEEAOQBBAEMASQBBAE0AUQBBAHUAQQBEAEUAQQBMAGcAQQB3AEEAQwA0AEEATQBRAEEAaQBBAEMAQQBBAGUAQQBCAHQAQQBHAHcAQQBiAGcAQgB6AEEARAAwAEEASQBnAEIAbwBBAEgAUQBBAGQAQQBCAHcAQQBEAG8AQQBMAHcAQQB2AEEASABNAEEAWQB3AEIAbwBBAEcAVQBBAGIAUQBCAGgAQQBIAE0AQQBMAGcAQgB0AEEARwBrAEEAWQB3AEIAeQBBAEcAOABBAGMAdwBCAHYAQQBHAFkAQQBkAEEAQQB1AEEARwBNAEEAYgB3AEIAdABBAEMAOABBAGMAQQBCAHYAQQBIAGMAQQBaAFEAQgB5AEEASABNAEEAYQBBAEIAbABBAEcAdwBBAGIAQQBBAHYAQQBEAEkAQQBNAEEAQQB3AEEARABRAEEATAB3AEEAdwBBAEQAUQBBAEkAZwBBACsAQQBBADAAQQBDAGcAQQBnAEEAQwBBAEEAUABBAEIAUABBAEcASQBBAGEAZwBBAGcAQQBGAEkAQQBaAFEAQgBtAEEARQBrAEEAWgBBAEEAOQBBAEMASQBBAE0AQQBBAGkAQQBEADQAQQBEAFEAQQBLAEEAQwBBAEEASQBBAEEAZwBBAEMAQQBBAFAAQQBCAFUAQQBFADQAQQBJAEEAQgBTAEEARwBVAEEAWgBnAEIASgBBAEcAUQBBAFAAUQBBAGkAQQBEAEEAQQBJAGcAQQArAEEAQQAwAEEAQwBnAEEAZwBB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AoAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAJABVAHMAZQByACAAfAAgAEUAeABwAG8AcgB0AC0AQwBsAGkAWABNAEwAIAAtAFAAYQB0AGgAIAAiAEMAOgBcAFUAcwBlAHIAcwBcAFAAdQBiAGwAaQBjAFwAVABNAFAAXABSAGUAcwB1AGwAdAAuAHgAbQBsACIAIAAtAEYAbwByAGMAZQANAAoAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAdAByAHkAewAgAHMAdABvAHAALQB0AHIAYQBuAHMAYwByAGkAcAB0ACAAfQBjAGEAdABjAGgAewB9AA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAjAGUAeABpAHQADQAKACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAjAGUAbgBkAHIAZQBnAGkAbwBuAA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAB9AA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAANAAoAIAAgACAAIAAgACAAIAAgACAAIAAgACAASQBuAHYAbwBrAGUALQBDAG8AbQBtAGEAbgBkACAALQBTAGMAcgBpAHAAdABCAGwAbwBjAGsAIAAkAFMAYwByAGkAcAB0AEIAbABvAGMAawAgAC0AQQByAGcAdQBtAGUAbgB0AEwAaQBzAHQAIAAgACQAQQByAGcAdQBtAGUAbgB0AHMA'
$Arguments =  '-Noexit', '-NoLogo', '–NoProfile', '-ExecutionPolicy RemoteSigned', "-EncodedCommand $EncodedCommand"
# line below executed successfully
start-process -filepath powershell -ArgumentList $Arguments

现在我们应该将代码转换为使用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()

我们没有脚本输出,$StartResult为true。还试图删除'-Noexit'参数。我想有一个新的窗口输出像在第一个例子。

连接参数不正确。命令行参数必须用空格分隔,并用双引号括起来。在你的情况下,你不需要引号,因为参数不包含空格。

试试这个:

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

相关内容

  • 没有找到相关文章