Can I send some text to the STDIN of an active process under Windows?

后端 未结 1 552
执笔经年
执笔经年 2020-11-28 13:23

I searched the web for that question and landed on Server Fault:

Can I send some text to the STDIN of an active process running in a screen session?

相关标签:
1条回答
  • .NET framework's Process and ProcessStartInfo classes can be used to create and control a process. Since Windows PowerShell can be used to instantiate .NET objects, the ability to control almost every aspect of a process is available from within PowerShell.

    Here's how you can send the dir command to a cmd.exe process (make sure to wrap this in a .ps1 file and then execute the script):

    $psi = New-Object System.Diagnostics.ProcessStartInfo;
    $psi.FileName = "cmd.exe"; #process file
    $psi.UseShellExecute = $false; #start the process from it's own executable file
    $psi.RedirectStandardInput = $true; #enable the process to read from standard input
    
    $p = [System.Diagnostics.Process]::Start($psi);
    
    Start-Sleep -s 2 #wait 2 seconds so that the process can be up and running
    
    $p.StandardInput.WriteLine("dir"); #StandardInput property of the Process is a .NET StreamWriter object
    
    0 讨论(0)
提交回复
热议问题