Jam*_*mie 4 windows powershell
我正在尝试找到一种方法来让PowerShell在运行可执行文件时不生成命令窗口Start-Process.
如果我直接在脚本中调用可执行文件(例如.\program.exe),则程序运行(带有其参数),输出将返回到PowerShell窗口.
如果我使用Start-Process该程序会生成一个程序运行的命令窗口并返回它的输出.
如果我尝试使用脚本的-NoNewWindow开关Start-Process然后错误说它找不到exe文件.
我更愿意使用Start-Process访问-Wait交换机,因为脚本制作的程序和配置可能需要一些时间单独完成,我不希望以后的命令启动.
此代码在单独的命令窗口中运行可执行文件:
Start-Process DeploymentServer.UI.CommandLine.exe -ArgumentList "download --autoDownloadOn --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime" -Wait
Run Code Online (Sandbox Code Playgroud)
此代码在PowerShell控制台中运行exe:
.\DeploymentServer.UI.CommandLine.exe download --autoDownloadOn --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime
Run Code Online (Sandbox Code Playgroud)
如果我将-NoNewWindow添加到Start-Process代码中
Start-Process DeploymentServer.UI.CommandLine.exe -ArgumentList "download --autoDownloadOn --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime" -Wait -NoNewWindow
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Start-Process : This command cannot be executed due to the error: The system
cannot find the file specifie
At C:\Temp\SOLUS3Installv1.3.ps1:398 char:22
+ Start-Process <<<< DeploymentServer.UI.CommandLine.exe -ArgumentList "download --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime" -Wait -NoNewWindow
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
使用-NoNewWindow交换机时,应该在可执行文件名前加上当前目录:
Start-Process .\DeploymentServer.UI.CommandLine.exe -ArgumentList "download --autoDownloadOn --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime" -Wait -NoNewWindow
Run Code Online (Sandbox Code Playgroud)
背景资料:
Start-Process尝试做的第一件事是-FilePath通过PowerShell规则解析参数的值.如果成功,它将替换通过命令的完整路径传递的值.如果没有,它会保持不变的价值.
在Windows API中,有两种方法可以启动新进程:CreateProcess和ShellExecute.ShellExecute是默认值,但如果使用需要CreateProcess(例如-NoNewWindow)的cmdlet参数,则将使用该参数CreateProcess.对于这个问题,它们之间的区别在于,当查找要执行的命令时,CreateProcess使用当前进程的工作目录,同时ShellExecute使用指定的工作目录(Start-Process默认情况下根据当前文件系统提供程序位置传递,除非明确指定通过-WorkingDirectory).
PS Test:\> 1..3 |
>> ForEach-Object {
>> New-Item -Path $_ -ItemType Directory | Out-Null
>> Add-Type -TypeDefinition @"
>> static class Test {
>> static void Main(){
>> System.Console.WriteLine($_);
>> System.Console.ReadKey(true);
>> }
>> }
>> "@ -OutputAssembly $_\Test.exe
>> }
PS Test:\> [IO.Directory]::SetCurrentDirectory((Convert-Path 2))
PS Test:\> Set-Location 1
PS Test:\1> Start-Process -FilePath Test -WorkingDirectory ..\3 -Wait # Use ShellExecute. Print 3 in new windows.
PS Test:\1> Start-Process -FilePath .\Test -WorkingDirectory ..\3 -Wait # Use ShellExecute. Print 1 in new windows.
PS Test:\1> Start-Process -FilePath Test -WorkingDirectory ..\3 -Wait -NoNewWindow # Use CreateProcess.
2
PS Test:\1> Start-Process -FilePath .\Test -WorkingDirectory ..\3 -Wait -NoNewWindow # Use CreateProcess.
1
Run Code Online (Sandbox Code Playgroud)
更改FileSystem提供程序的当前位置时,PowerShell不会更新当前进程的工作目录,因此目录可能不同.
键入时:
Start-Process DeploymentServer.UI.CommandLine.exe -Wait -NoNewWindow
Run Code Online (Sandbox Code Playgroud)
Start-Process无法DeploymentServer.UI.CommandLine.exe通过PowerShell规则解析,因为FileSystem默认情况下它不会查找当前位置.它使用CreateProcess,因为你指定-NoNewWindow开关.因此,它最终会DeploymentServer.UI.CommandLine.exe在当前进程的工作目录中查找,该目录不包含此文件,从而导致错误.
| 归档时间: |
|
| 查看次数: |
6901 次 |
| 最近记录: |