Use PowerShell to interact with a console application that doesn't accept arguments
Today I needed to use a tool that our in house devs made a little while ago. It changes something in a record on a remote CRM server. The app doesn't accept arguments from the command line as it was never thought anyone would have to run this on 98 different records. It had an old school menu system where you pick an option, then it asks you for some info, then the record ID, then something else before finally doing the job. For example:
======= Utility Console ======== 1 - Delete Entity Record 2 - Change StateCode of Entity Record 3 - Change StateCode of Entity Related Entities Records 4 - Delete Registration and all related Guests 5 - Delete all Payments and related to Invoice 6 - Set Quote to WON 7 - Set Opportunity to WON 8 - Set Order to CANCELED 0 - Exit Please choose an action number to perform:
Screw this! There has to be a way to automate it!
Thankfully, there is. It's a bit like skipping through an automated attendant when you have all the options written down; dial 1800 555 333 then dial option 6,2,3.
To give you an example, I am going to use something a bit simpler than my console app that you can try on your machine:
We are going to use Start-Process and run nslookup with two options: ? and google.com. The ? will output the help for nslookup and google.com will return the DNS info for that domain.
What is happening in this example, is that I am creating a text file with the content specified in the here-string and saving it in the UTF8 encoding. Without the encoding explicitly set, I was getting some funky errors. I am then running nslookup, feeding those options into it, then saving the output to a file.
I expect to see the nslookup help info and the nslookup output for google.com inside out.txt, which I did.
Now, because I wanted to run this over and over with some different input, I will use the format operator (-f) to accomplish this and a loop for my data which, in this case, is just an array of IP address strings:
I have to save the output to two different files because Start-Process can't append.
One thing you should know before doing this is that input redirection does not work with hidden password fields. So if you need to input something that hides the text with *** or whatever, then you are just out of luck.