Start-Process访问StandardError和StandardOutput属性时,PowerShell 命令中是否存在错误?
如果我运行以下命令,我得不到输出:
$process = Start-Process -FilePath ping -ArgumentList localhost -NoNewWindow -PassThru -Wait
$process.StandardOutput
$process.StandardError
Run Code Online (Sandbox Code Playgroud)
但是,如果我将输出重定向到文件,我得到预期的结果:
$process = Start-Process -FilePath ping -ArgumentList localhost -NoNewWindow -PassThru -Wait -RedirectStandardOutput stdout.txt -RedirectStandardError stderr.txt
Run Code Online (Sandbox Code Playgroud) 是否有可能在一次运行中将stdout从外部程序重定向到外部程序的变量和stderr到另一个变量?
例如:
$global:ERRORS = @();
$global:PROGERR = @();
function test() {
# Can we redirect errors to $PROGERR here, leaving stdout for $OUTPUT?
$OUTPUT = (& myprogram.exe 'argv[0]', 'argv[1]');
if ( $OUTPUT | select-string -Pattern "foo" ) {
# do stuff
} else {
$global:ERRORS += "test(): oh noes! 'foo' missing!";
}
}
test;
if ( @($global:ERRORS).length -gt 0 ) {
Write-Host "Script specific error occurred";
foreach ( $err in $global:ERRORS ) {
$host.ui.WriteErrorLine("err: $err");
}
} else {
Write-Host …Run Code Online (Sandbox Code Playgroud) 在 powershell 脚本中,我正在运行一个命令,该命令以管理员身份启动一个新的 powershell(如果我不是,如果需要,取决于$arg),然后运行该脚本。
我正在尝试将 stdout 和 stderr 重定向到第一个终端。
不是试图让事情变得更容易,也有争论。
param([string]$arg="help")
if($arg -eq "start" -Or $arg -eq "stop")
{
if().groups -match "S-1-5-32-544"))
{
Start-Process powershell -Verb runas -ArgumentList " -file servicemssql.ps1 $arg"
exit
}
}
$Services = "MSSQLSERVER", "SQLSERVERAGENT", "MSSQLServerOLAPService", "SSASTELEMETRY", "SQLBrowser", `
"SQLTELEMETRY", "MSSQLLaunchpad", "SQLWriter", "MSSQLFDLauncher"
function startsql {
"starting SQL services"
Foreach ($s in $Services) {
"starting $s"
Start-Service -Name "$s"
}
}
function stopsql {
"stopping SQL services"
Foreach ($s in $Services) {
"stopping $s" …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码运行cmd命令:
ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
cmd.RedirectStandardInput = true;
cmd.RedirectStandardOutput = true;
cmd.RedirectStandardError = true;
cmd.UseShellExecute = false;
cmd.CreateNoWindow = true;
cmd.WindowStyle = ProcessWindowStyle.Hidden;
Process exec = Process.Start(cmd);
exec.StandardInput.WriteLine("sc create \"BaliService\" binPath= \"{0}\\BaliService.exe\"", Directory.GetCurrentDirectory());
Run Code Online (Sandbox Code Playgroud)
此命令需要管理员privelages,如果我以管理员身份运行cmd并键入命令它完美运行但不是当我以管理员身份运行此应用程序时.我已经添加了
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
每次打开exe时提示uac的清单文件.
我已经看到了关于这个的多个问题,他们似乎都建议在提升的应用程序下运行的任何进程都具有相同的权限,但这对我不起作用.
我试过cmd.Verb = "runas";但没有骰子.
我正在我的C#应用程序中启动一个运行控制台应用程序的进程.我重定向了标准输入和输出,并且能够通过StandardOutput.ReadLine()读取几行.我确信我已正确配置ProcessStartInfo.
控制台应用程序启动时会输出几行(以"marker"行结尾),然后等待输入.收到输入后,它再次输出几行(再次以"标记"行结束),依此类推.我的目的是从它读取行直到我收到"标记"行,此时我知道发送适当的输入字符串.
我的问题是,经过几次迭代,程序挂起.暂停调试器往往会将挂起置于对StandardOutput.EndOfStream的调用中.以下测试代码就是这种情况:
while (!mProcess.StandardOutput.EndOfStream) // Program hangs here.
{
Console.WriteLine(mProcess.StandardOutput.ReadLine());
}
Run Code Online (Sandbox Code Playgroud)
当我测试"marker"行时,如果我在读取行后尝试访问StandardOutput.EndOfStream,我会得到同样的挂起:
string line = "";
while (!isMarker(line))
{
line = mProcess.StandardOutput.ReadLine();
}
bool eos = mProcess.StandardOutput.EndOfStream; // Program hangs here.
Run Code Online (Sandbox Code Playgroud)
我可能会做什么导致这个属性如此可怕地执行?
powershell ×3
c# ×2
admin-rights ×1
command-line ×1
hang ×1
process ×1
stderr ×1
stdout ×1
windows ×1