I
I
Ivan2019-02-06 17:03:30
PowerShell
Ivan, 2019-02-06 17:03:30

How to run console application in powershell?

Good afternoon.
It is necessary to execute a console application and get the output into a variable with the array type.
Found the launch of the application through an object:

$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "ping"
$pinfo.Arguments = "8.8.8.8 -n 1"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.CreateNoWindow = $true
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$output = $p.StandardOutput.ReadToEnd()
$output += $p.StandardError.ReadToEnd()
$output

at the output, the variable is represented by a string that could not be transferred to the array through split.
What you need is obtained through launch:
& ping 8.8.8.8 -n 1
but to pass parameters you have to do this:
$param = '8.8.8.8'
$param2 = '-n'
$param3 = '1'
& "ping" $param $param2 $param3

I would like to get something like
$param = '8.8.8.8 -n 1'
& "ping" $param

Is there any way to do this or is there a better way?
start-process allows you to do this, but through uploading to a file and further reading from it, which is not desirable
Powershell 4.0 version

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
azarij, 2019-02-06
@azarij

and the console application is exactly ping?
what is the end goal of this script?
if you check the availability of hosts, that is, test-connection and test-netconnection. if they are not in power 4, then upgrade to 5.1.

B
beerchaser, 2019-02-06
@beerchaser

I came to the next view
And then parsing the output file. It turned out more predictable ... In terms of time - comparable.
In your example, you could try FileName="cmd" Arguments= "ping with options". StdErr and StdOut are best separated into different variables.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question