1

I need to save variable value(s) to my ftp server directly from memory, without creating a local file. Variable can be single line or multi line.

For example:

PowerShell C:\> $cm = tasklist /S localhost /FO LIST
PowerShell C:\> $cm

............
............
............
.....list of tasks
...etc...

PowerShell C:\> $session_token = "bG9uZyBzZXNzaW9uIHRva2VubG9uZyBzZXNzaW9uIHRva2VubG9uZ"

I know how to create a file with tasklist output and upload to ftp server every 10 seconds (scheduled task), but i need to do it without file creation. Any solutions?

mklement0
  • 382,024
  • 64
  • 607
  • 775
ValB
  • 19
  • 2
  • I'm not a server guy but one possibility (you can google it) is to use powershell to create a RAM Disk, store your file there then upload it to your FTP server. No local storage required. – RetiredGeek Apr 01 '23 at 14:05

1 Answers1

0

Recent Windows versions ship with curl (C:\Windows\System32\curl.exe).

Per the manual, the --upload-file (-T) option accepts - in lieu of a local file path, in which case its uses the data received via stdin as the file content to upload.

Therefore, something like the following should work (untested):

# Note: Be sure to use curl.exe, i.e *with extension ".exe"* in Windows PowerShell.
tasklist /S localhost /FO LIST | 
  curl.exe --upload-file - ftp://ftp.example.com/dir/path/remote-file

The caveat is that from PowerShell, as of version 7.3.3, that only works with text data, which is encoded based on the $OutputEncoding preference variable.

If you have binary data to send, such as the output from an external program, call via cmd /c, as shown in this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775