Start process and then send arguments

Hi, sorry I’m quite new.

I have an executable that runs a cmd window on a server, I’d like to launch it with Powershell and then pass arguments to it to execute.

I made a test with regular cmd and this works

$command = "ping 8.8.8.8 -t"
$Path = "C:\Windows\System32\cmd.exe"
Start-Process $Path -ArgumentList "/c $command"

However when I use my cmd based executable it opens and does nothing, I believe it still needs to load after opening and so when the arguments are passed it isn’t ready to load them yet.
Is there a way to pass arguments to an already opened cmdlet?

I have tried this wich emulates the keyboard to write my command into the cmd window and then presses “enter”. It works, but requires another task on my pc to open via Remote Desktop the server so it detects a keyboard to emulate.

Write-Output "$command"| Clip
Invoke-Item "$Path"
Start-Sleep -Seconds 2
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[System.Windows.Forms.SendKeys]::SendWait("$command")
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Start-Sleep -Seconds 2
taskkill /f /im cmd.exe /T

I would like not to have to rely on a local computer for this and use only the server.

Thank you in advance for the help.
Have a ncie day

Is your “cmd based executable” not actually cmd.exe? I’m guessing it’s another launcher that calls cmd.exe to start a process/application? If that’s the case, you’re getting into some nested command stuff that gets a little grey…

Can you tell us what “cmd based executable” you’re working with and maybe provide us with the code you’re using and output from running it?

OpenSSL

Youy guessed right, I’m not using cmd.exe. I’m using OpenSSL (wich runs a cmd like window, It reads regular cmd commands) to encrypd and decrypt files.

I have some CSV files that will be generated on a server with 3DES encryption that I need to decrypt every day with OpensSL and then send to another remote PC for analysys and import.

The string I run is formatted like this (I construct it via Powershell using the various file path):

certutil -decode File1.csv File1.tmp.csv & openssl des-ede3 -d -nopad -nosalt -in File1.tmp.csv -out File1.decoded.csv -K 686436377773547232333473375448387772543134647a38

File 1 is my starting file
File1.tmp is a temporary file used in the process
File1.decoded is the decrypted file
6868… is the decryption key

Hi, do you perhaps have any more suggestions? Yuo didin’t reply on my last reply.
Thx again for your time

Well, certutil can be run from a standard command prompt or PowerShell without launching OpenSSL, at least in my experience. You can also run OpenSSL commands directly from a standard prompt or PowerShell without entering the interactive shell. So, if that’s all you need to do, you can create the command string and call the executable directly from the script.

I’ve included 2 options below. (Keep in mind that I have not executed this script, and I don’t even have OpenSSL installed on this machine, so you may have to tweak it until you get it right!)

Using the ampersand (‘&’) in front of the command tells the PowerShell interpreter to kick off the external process and let it do its thing. Using Start-Process to kick it off allows you to do things like controlling the window and receiving any output. In your case, I don’t think it would really matter, but look into the pros and cons of each.

$fileInput = "File1.csv"
$fileTemp = "File1.tmp.csv"
$fileDecrypt = "File1.decoded.csv"
$decryptKey = 686436377773547232333473375448387772543134647a38

& "C:\Windows\System32\certutil.exe -decode $fileInput $fileTemp"
& "<path to exe>\openssl.exe des-ede3 -d -nopad -nosalt -in $fileTemp -out $fileDecrypt -K $decryptKey"

##### OR #####

Start-Process -NoNewWindow -FilePath "C:\Windows\System32\certutil.exe" -ArgumentList "-decode $fileInput $fileTemp"
Start-Process -NoNewWindow -FilePath "<path to exe>\openssl.exe" -ArgumentList "des-ede3", "-d", "-nopad", "-nosalt", "-in $fileTemp", "-out $fileDecrypt", "-K $decryptKey"

So, I’m not sure I really answered your original question about passing args to a running process, but maybe I gave you a different approach to your solution!

Thank you so much! I used the second option and it works, there is only a single problem left, not all the files are decoded.

Right now in my start folder I have 17 files, decoding one after another didn’t wotk on all files (only 8 were decoded), so I tried putting a time-out between each decode process.

Start-Sleep -Milliseconds 5000 

My first time-out was 1 second and I noticed it decoded more files, so I tried increasing the time-out. With 5 seconds it decoded 16 out of 17 files, almost good, but with 6 seconds it decoded 12 files only.

Any suggestions? Note that i don’t know how many files there will be each day to decode, codul be 6 or could be 30 (more I doubt).

Thank you again

How are you looping through the files? I assume by “decoded” that you need both the certutil and the openssl lines to be successful?

I’m looping with a For-Each Object loop.

I first check of the original file is present in a folder I use for backup, if it isn’t I copy the file and then I proceed to decode it. The backup check is to not decode the same file twice.

For decode yes, I mean the 2 operations to be successful and having the decrypted CSV.

With the emulation of the keyboard timing wasn’t an issue, I don’t know if it is too fast or not in executing Certutil and opensssl

Perhaps you just need to add -wait to Start-Process and do them 1 by 1.

1 Like

Thank you, I tought later of that, a Sleep of 5 seconds (maybe I can reduce it, but for now it doesn’t matter much) between the 2 Satrt-Process fixed everything

I fixed it, a Sleep of 5 seconds (maybe I can reduce it, but for now it doesn’t matter much) between the 2 Start-Process made it work.

Thank you so much for the help

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.