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) 我可以运行这个罚款:
$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe"
start-process $msbuild -wait
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此代码(下面)时,我收到一个错误:
$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe /v:q /nologo"
start-process $msbuild -wait
Run Code Online (Sandbox Code Playgroud)
有没有办法可以使用start-process将参数传递给MSBuild?我愿意不使用启动过程,我使用它的唯一原因是我需要将"命令"作为变量.
当我
在一行上有
C:\ WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe/v:q/nologo
时,如何在Powershell中处理它?
我应该使用某种eval()类型的函数吗?
您是否可能知道为什么会出现此错误以响应下面的代码.用户名和密码已验证为正确.
$secPassword = ConvertTo-SecureString "Password" -AsPlaintext -Force
$farmCredential = New-Object System.Management.Automation.PsCredential "SharePoint\SP_Farm",$secPassword
Start-Process $PSHOME\powershell.exe -Credential $FarmCredential -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `"Hello:`"`$outvar1`"}`"" -Wait
Run Code Online (Sandbox Code Playgroud)
错误;
Start-Process : This command cannot be executed due to the error: The directory name is invalid.
At C:\Users\Administrator.SHAREPOINT\AppData\Local\Temp\fb2956d7-87fc-4235-9f3c-742698cafe9f.ps1:8 char:14
+ Start-Process <<<< $PSHOME\powershell.exe -Credential $FarmCredential -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output
`"Hello:`"`$outvar1`"}`"" -Wait
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
Run Code Online (Sandbox Code Playgroud)
但是,这很好用.
Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `"Hello:`"`$outvar1`"}`"" -Wait …Run Code Online (Sandbox Code Playgroud) 我正在将旧cmd命令转换为Powershell,目前使用:
START "My Title" Path/To/ConsoleApp.exe
Run Code Online (Sandbox Code Playgroud)
这可以按预期使用My Title作为窗口标题启动ConsoleApp.这已被替换为正常工作的Start-Process,但未提供更改标题的机制.
有没有其他方法可以做到这一点,而无需使用cmd命令?
我试图从命令行提示符运行此命令:
"D:\\fiji\\fiji.exe -macro D:\\fiji\\macros\\FFTBatch.ijm --headless"
Run Code Online (Sandbox Code Playgroud)
当我在命令行控制台中输入它时,它工作得很好.
但是,当我试图使它从C#应用程序工作时,它失败了.我试过跟随,但似乎上面的命令没有以某种方式执行:
string fijiCmdText = "D:\\fiji\\fiji.exe -macro D:\\fiji\\macros\\FFTBatch.ijm --headless";
System.Diagnostics.Process.Start("cmd.exe", fijiCmdText);
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何改变它的工作?谢谢.
我想在循环中重复执行一个程序.
有时,程序崩溃,所以我想杀死它,以便下一次迭代可以正确启动.我通过超时确定这个.
我有超时工作,但无法获得程序的退出代码,我还需要确定其结果.
之前,我没有等待超时,但只是在Start-Process中使用了-wait,但是如果启动的程序崩溃,这会使脚本挂起.通过这种设置,我可以正确地获得退出代码.
我正在从ISE执行.
for ($i=0; $i -le $max_iterations; $i++)
{
$proc = Start-Process -filePath $programtorun -ArgumentList $argumentlist -workingdirectory $programtorunpath -PassThru
# wait up to x seconds for normal termination
Wait-Process -Timeout 300 -Name $programname
# if not exited, kill process
if(!$proc.hasExited) {
echo "kill the process"
#$proc.Kill() <- not working if proc is crashed
Start-Process -filePath "taskkill.exe" -Wait -ArgumentList '/F', '/IM', $fullprogramname
}
# this is where I want to use exit code but it comes in empty …Run Code Online (Sandbox Code Playgroud) 也许有一种方法可以使用我找不到的Start-Process cmdlet 来完成它?我在StackOverflow上找到的其他相关Q/A就是这样,这和所有这些都提供了一个解决方案来使用自定义C#代码.我的问题是具体的,有没有直接的方法在PowerShell中执行此操作?即你是在一个升级的PS控制台,并希望以非管理员身份运行一个进程.
你好堆栈社区:)
我有一个简单的目标。我想从另一个 Powershell 脚本启动一些 PowerShell 脚本,但有 3 个条件:
有一个类似的问题链接。但答案是使用文件作为在 2 个 PS 脚本之间进行通信的一种方式。我只是想避免访问冲突。@Update:主脚本将启动其他几个脚本。因此,如果要同时从多个用户执行执行,则文件解决方案可能会很棘手。
Script1.ps1是应该有字符串作为输出的脚本。(需要说明的是,这是一个虚构的脚本,真实的脚本有 150 行,所以我只是想举个例子)
param(
[String]$DeviceName
)
#Some code that needs special credentials
$a = "Device is: " + $DeviceName
$a
Run Code Online (Sandbox Code Playgroud)
ExecuteScripts.ps1应该调用具有上述 3 个条件的那个
我尝试了多种解决方案。这一个例如:
$arguments = "C:\..\script1.ps1" + " -ClientName" + $DeviceName
$output = Start-Process powershell -ArgumentList $arguments -Credential $credentials
$output
Run Code Online (Sandbox Code Playgroud)
我没有得到任何输出,我不能只是调用脚本
&C:\..\script1.ps1 -ClientName PCPC
Run Code Online (Sandbox Code Playgroud)
因为我无法将-Credential参数传递给它..
先感谢您!
我有一个脚本,我想作为一个预定的任务运行,但它没有做它应该做的事情.我正在尝试使用Start-Process和-Wait开关调用可执行文件,然后再继续.线是
Start-Process -FilePath"C:\ Pfx Engagement\Admin\Utilities\Backup Restore\BackupRestoreUtil.exe"-ArgumentList"/ f
"$backup_directory"" - Wait
如果我从命令提示符调用它,即:
powershell.\ script.ps1
有用.它运行命令并在继续之前等待它完成.该命令完成后必须运行的脚本还有更多内容.问题是,当它是一个计划任务时,它不会等待.做一些基本的故障排除,我首先尝试使用名为"Scripts"的计划任务帐户打开带有runas的cmd窗口.所以我跑了
runas/env/user:脚本cmd
使用任务帐户打开命令提示符窗口.从那个命令提示符,我再次尝试"powershell.\ script.ps1",这一次,它不会等待.它运行命令并在命令完成之前立即继续运行.所以我认为这可能是"Scripts"帐户的一个问题,直到我用runas Administrator打开一个新的命令提示符
runas/env/user:Administrator cmd
当我从此管理员命令提示符调用脚本时,也会忽略-Wait开关,并且在调用脚本后立即移动脚本而不等待它完成.
关于这一点的奇怪之处在于,当我从管理员帐户从命令提示符调用它而不执行runas时,它可以工作.相同的帐户,两个不同的结果.关于这里到底发生了什么的任何想法,同样重要的是,如何解决它?
操作系统是Server 2008 R2,运行PowerShell 3.0
powershell scheduled-tasks wait start-process windows-server-2008-r2
我正在使用PowerShell Start-Process调用遇到一种奇怪的行为.
这是电话:
$process = start-process `
"C:\somepath\MyBinary.exe" `
-PassThru `
-Credential $defaultCredential `
-Wait `
-WorkingDirectory "C:\somepath" `
-LoadUserProfile
if ($process.ExitCode -ne 0)
{
#do something
}
Run Code Online (Sandbox Code Playgroud)
此调用始终返回退出代码 - 1073741502.
快速搜索后,当程序无法加载其所需的dll(aka.STATUS_DLL_INIT_FAILED)时,此退出代码似乎与一般错误有关.
当我运行它没有-Credential $credential程序正确运行.
为了隔离问题,我some.exe在目标凭证的提示中手动启动,并且运行顺利.
所以问题似乎只来自start-process cmdlet有效启动进程的方式.
我找到了一些潜在的解决方案来解决这个问题,我尝试申请没有运气:链接和链接.
你知道这里发生了什么吗?
编辑1:
我直接或通过powershell脚本运行proc mon来监控程序活动.加载时似乎发生了这个问题kernelbase.dll.
本地procmon转储(工作):
9:06:35.3837439 AM MyBinary.exe 2620 Load Image C:\Windows\SysWOW64\kernelbase.dll SUCCESS Image Base: 0x76270000, Image Size: 0x47000
9:06:35.4317417 AM MyBinary.exe 2620 RegOpenKey HKLM\System\CurrentControlSet\Control\Nls\Sorting\Versions REPARSE Desired Access: Read …Run Code Online (Sandbox Code Playgroud) start-process ×10
powershell ×9
command-line ×2
wait ×2
c# ×1
credentials ×1
exit-code ×1
runas ×1
windows ×1