Read-Host Write-Host, . , Write-Host, Read-Host. powershell .
string readHostOverride = @"
function Read-Host {
param(
[string]$Prompt
)
$StartInfo = New-Object System.Diagnostics.ProcessStartInfo
#$StartInfo.CreateNoWindow = $true
$StartInfo.UseShellExecute = $false
$StartInfo.RedirectStandardOutput = $true
$StartInfo.RedirectStandardError = $true
$StartInfo.FileName = 'powershell.exe'
$StartInfo.Arguments = @(""-Command"", ""Read-Host"", ""-Prompt"", ""'$Prompt'"")
$Process = New-Object System.Diagnostics.Process
$Process.StartInfo = $StartInfo
[void]$Process.Start()
$Output = $Process.StandardOutput.ReadToEnd()
$Process.WaitForExit()
return $Output.TrimEnd()
}
";
string writeHostOverride = @"
function Write-Host {
}
";
Run(readHostOverride);
Run(writeHostOverride);
Run(), , powershell.
, Write-Host; , .
For commentators suggesting switching to Write-Output instead of Write-Host, this is NOT a solution for many use cases. For example, you do not want your user notifications to arrive in your output stream.
If you need to use the source cmdlets, use this:
Microsoft.PowerShell.Utility\Read-Host -Prompt "Enter your input"
source
share