2

I'm trying to put together a powershell script that we can use for some custom scripts to a commercial IT monitoring solution (StableNet by Infosim.de). The product has a neat feature on passing on parameter to scripts in a secure manner, and avoid the parameters being visible in a system tools like ps/Task Manager. Rather than having them as parameters on the command line, the solution can call the script with a "--inline" parameter, which expects the called script to then read all other parameters via inline parameter setting. When inline parameters are used, the running script should be able to start and wait for the calling code to supply the parameters separated by CR/LF.

I've done some code using C# which simply uses:

string host = Console.ReadLine(); string user = Console.ReadLine(); string pass = Console.ReadLine(); string filePath = Console.ReadLine();

This works fine in C#. But I would also like to make some powershell script for quicker development. However, as StableNet starts all powershell scripts with -noninteractive parameter I can't use Read-Host calls to ask for the inline parameters.

So the question is if anyone has any good ideas how I can achieve these inline parameter settings with a Powershell script and still make it work as a -noninteractive script?

I've tried a few options based on the [System.Console]::OpenStandardInput().Read(), but I've yet to be successful.

2
  • Standard Input only works if you Pipe the input to the script. Why can't you use Read - Host? Where do you plan to get input if you do not use Read-Host? Commented Jul 2, 2024 at 23:42
  • As the monitoring agent calls powershell scripts with the -noninteractive option, the Read-Host function is not available. I have no control of how the scripts are called by the software, only how it provides its parameters to the called scripts. This might not be possible at all to achive using the inline option with powerscript, but I thought I'd hear if others had some ideas. Commented Jul 3, 2024 at 15:36

1 Answer 1

3

Use the automatic $input variable to access stdin input provided to PowerShell's CLI (powershell.exe for Windows PowerShell, pwsh for PowerShell (Core) 7).

For instance:

echo host1 user1 pass1 | powershell -noninteractive -c '$hostVar, $userVar, $passVar = @($input); [pscustomobject] @{ Host = $hostVar; User = $userVar; Pass = $passVar }'

Output:

Host  User  Pass
----  ----  ----
host1 user1 pass1

If you cannot provide pipeline input yet cannot avoid -NonInteractive in the PowerShell CLI call (which precludes use of Read-Host), you can use System.Console.ReadLine, just as in C#, adapted to PowerShell syntax, i.e. [Console]::ReadLine():

powershell -noninteractive -c '$hostVar = [Console]::ReadLine(); $userVar = [Console]::ReadLine(); $passVar = [Console]::ReadLine(); [pscustomobject] @{ Host = $hostVar; User = $userVar; Pass = $passVar }'
Sign up to request clarification or add additional context in comments.

3 Comments

The use of $input won't work either as far as I can see. The monitoring software starts powershell without piping the variables, and there's no way to change how this is performed. So the script is supposed to start up, then wait for input + CR/LF for each parameter the script expects. Once all the parameters have been provided, the script execution continues uninterrupted. So I've got to find another way than using read-host to work around the "-noninteractive" mode, but I'm starting to think there is no way around this limitation with -noninteractive.
@ThomasN, you can use [Console]::ReadLine(), which isn't affected by -NonInteractive - please see my update.
Brilliant, that works! i'm pretty sure i tried a variant of the [Console]::ReadLine() earlier on, but I must have done something wrong during testing. Could have been fooled that it doesn't work unless you call it with -noninteractive. Thanks for you help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.