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.
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 }'