culter
culter

Reputation: 5697

cmd to powershell connection

I need to write the output of cmd command DIR to powershell variable(array). Is it possible? I need it because I use cmd scripting in WinSCP (SFTP client) and when I'm connected to sftp server, I can use cmd commands only, not powershell. But I need to get names and size of files that are in the remote directory to check if the transfer was successful. This is my script which connects to sftp and upload some files:

C:\"Program Files"\WinSCP\winscp.com /console /command "option batch abort" "option confirm off" "open sftp://login:[email protected]" "cd import" "option transfer binary" "put C:\_ZbankyNaOdoslanie\*.gpg" "put C:\_ZbankyNaOdoslanie\*.chk" "close" "exit"

Thank you.

Upvotes: 1

Views: 1498

Answers (1)

Andy Arismendi
Andy Arismendi

Reputation: 52567

You can invoke this line in PowerShell using the call operator & and assign it's stdout and/or stderr to a variable.

Example:

$output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2

The variable $output will be an array of strings. Each line of output will be an array element.

More examples:

  • Just stdout: $output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2 2>$null
  • Just stderr: $output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2 1>$null
  • Both: $output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2 2>&1

Upvotes: 3

Related Questions