Ответ 1
.NET framework Process и ProcessStartInfo классы могут использоваться для создания и управления обработать. Поскольку Windows PowerShell может использоваться для создания экземпляров объектов .NET, возможность управления почти всеми аспектами процесса доступна из PowerShell.
Здесь вы можете отправить команду dir
в процесс cmd.exe
(обязательно оберните это в файл .ps1, а затем выполните script):
$psi = New-Object System.Diagnostics.ProcessStartInfo;
$psi.FileName = "cmd.exe"; #process file
$psi.UseShellExecute = $false; #start the process from it 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