0

I have an existing desktop app written in C# (I have the source code) that gets invoked from a script (currently batch script, but I will rewrite it as a PowerShell script) with a number of command-line arguments. I need to pass a password from the script to the app, and I am wondering how to do it in a more secure way. The best option to get password in PowerShell would be via the PSCredential object. Once PowerShell gets the password from the user, how can it pass it to the desktop app (and passing it in plain text via command line is not an option)?

I suspect one option would be to encrypt password in PowerShell and then decrypt it in C#, but I am wondering if there is a more elegant way to do this. If it matters, the script executes the desktop app in a loop with various parameters, but the same user credentials.

Alek Davis
  • 10,628
  • 2
  • 41
  • 53
  • I don't know what would be more "elegant" than encrypting and decrypting. I'm pretty sure that's the only way you could do it, aside from having the Desktop App ask for the input. If you're able to describe what it does in the loop and why you need to pass it a password from PowerShell, maybe we can provide more insight on how to approach it. I also take it, you want these to be separate, IE not calling the PowerShell form the DesktopApp. – Jim Dec 15 '20 at 02:31
  • 1
    Put the creds in the Windows Credential (credman) store and call is as needed from there. Modules that you can learn from are in the MS powershelllgallery.com [Find-Module -Name '*credential*']. See also: [Secrets Management Development Release](https://devblogs.microsoft.com/powershell/secrets-management-development-release) – postanote Dec 15 '20 at 02:53
  • @Jimithus: You are probably correct. I have a console app that take email template and a data file as inputs, merges them and sends to the specified recipients. The powershell script calls this app to process different templates and multiple languages. The password is needed to authenticate against SMTP server. So when processing 100+ notifications, I don't want to enter password for each template and just do it one. – Alek Davis Dec 15 '20 at 06:08
  • @postanote: I know how to get credentials in PowerShell. The question was how to transfer them to a C# app. – Alek Davis Dec 15 '20 at 06:09
  • 1
    @AlekDavis, take a look here: https://stackoverflow.com/questions/30859038 – Jim Dec 15 '20 at 15:04
  • 1
    @Jimithus: Sweet, I was just thinking about it. Thanks for the pointer. – Alek Davis Dec 16 '20 at 00:48

0 Answers0