PowerShell启动进程详解:参数、权限与输出重定向
PowerShell启动进程详解:参数、权限与输出重定向
如何在 PowerShell 中使用启动进程
要从 PowerShell 中运行应用程序、进程或脚本,您只需输入文件路径即可。但这将在与 PowerShell 会话相同的环境和上下文中启动该进程。
当您想要以不同用户身份运行进程、在新窗口中启动它,甚至同时启动多个进程时,您将需要使用 Start-Process cmdlet。
在本文中,我们将了解启动进程 cmdlet。我们如何以提升的权限运行进程,在新窗口中运行它,甚至完全隐藏。我还将为您提供一些有用的示例来帮助您入门。
在 PowerShell 中使用启动进程
Start-Process cmdlet 允许您从 PowerShell 中在计算机上运行一个或多个进程。它旨在异步运行进程或运行提升的应用程序/脚本(具有管理权限)。
如果需要在 PowerShell 中同步运行脚本或其他控制台程序,则无需使用 Start-Process cmdlet。原因是您可以将其输出重定向到 PowerShell。
这是 cmdlet 的缺点之一,您无法将输出或错误流重定向到 PowerShell。您唯一的选择是将输出重定向到文本文件。
那么让我们看看如何使用 cmdlet。我们可以使用以下参数来启动进程:
-FilePath
指定要运行的文件、应用程序或进程
-ArgumentList
指定要启动的流程所使用的参数
-Credential
用于运行该进程的用户帐户
-WorkingDirectory
进程应该开始的位置
-NoNewWindow
不要为此过程打开新窗口
-RedirectStandardError
指定将错误输出重定向到的文本文件
-RedirectStandardInput
包含流程输入的文本文件
-RedirectStandardOutput
指定将输出重定向到的文本文件
-WindowStyle
正常、隐藏、最小化或最大化
-Wait
等待该过程完成,然后再继续执行脚本
-UseNewEnvironment
该进程将使用自己的环境变量,而不是 PowerShell 会话的环境变量
因此,要简单地使用 PowerShell 打开应用程序,我们可以使用以下命令:
Start-Process Notepad.exe
# Simply typing notepad.exe in PowerShell will have the same result:
Notepad.exe
这将在新窗口中打开记事本,并具有与 PowerShell 会话相同的权限。该过程是异步运行的,这意味着 PowerShell 将继续执行脚本,即使该过程尚未完成。
等待进程完成
在 PowerShell 中启动进程最常见的等待方式是等待其完成。为此,我们可以使用 -wait
参数,这将确保 PowerShell 会等待该进程和所有子进程完成。
假设我们有一个 bat 文件,我们想从 PowerShell 启动它并等待该过程完成:
# Start the process example.bat and wait for it to finish
Start-Process -FilePath "c:\temp\example.bat" -Wait
这将运行 bat 文件并等待其完成,然后再继续脚本或恢复输入。请记住,默认情况下不会捕获该过程的任何输出。所以你不会知道bat文件是失败还是成功完成。
窗口大小和隐藏进程
如果我们使用上面的示例运行bat文件,它将在新窗口中运行bat文件。该窗口将具有正常大小并在该过程完成时关闭。在启动进程cmdlet中,我们可以指定窗口大小,甚至完全隐藏它。
我们可以选择使用参数 -WindowStyle
和 -NoNewWindow
。显然,我们不能同时使用这两个参数。
要在没有任何窗口的情况下运行 bat 文件,我们可以在 PowerShell 中使用以下命令:
# Start the process example.bat, without any window and wait for it to finish
Start-Process -FilePath "c:\temp\example.bat" -Wait -WindowStyle Hidden
您不会得到任何反馈,除非您的脚本将在该过程完成后继续。但是,我们可以将过程结果重定向到文本文件,稍后会详细介绍。
要在最大化、正常或最小化窗口中运行进程,您可以使用以下选项:
# Default behavior:
Start-Process -FilePath "c:\temp\example.bat" -Wait -WindowStyle Normal
# Maximized
Start-Process -FilePath "c:\temp\example.bat" -Wait -WindowStyle Maximized
# Minimized
Start-Process -FilePath "c:\temp\example.bat" -Wait -WindowStyle Minimized
我们可以使用的另一个参数是 -NoNewWindow
。这将在与 PowerShell 脚本相同的窗口中运行该进程。此选项仅适用于基于命令行的进程。例如,您无法在与 PowerShell 相同的窗口中打开记事本。
Start-Process -FilePath "c:\temp\example.bat" -Wait -NoNewWindow
在启动进程中使用参数
您想要启动的某些进程或脚本需要参数(参数)。您的第一个想法可能是在文件路径中的引号之间添加参数,但正如您可能已经注意到的那样,这是行不通的。
要将参数传递给要启动的进程,您需要使用 -arguments
参数。
让我们以从 PowerShell 运行 MSI 为例。要静默运行 MSI,我们需要提供参数 /quiet
或 /qn
,并且我们可能不想重新启动,因此我们添加 /norestart
。
Start-Process -FilePath "C:\temp\example.msi" -Wait -ArgumentList "/quiet /norestart"
# Or arguments as string array:
Start-Process -FilePath "C:\temp\example.msi" -Wait -ArgumentList "/quiet","/norestart"
PowerShell 启动进程已提升
当您使用 Start-Process 启动进程时,它将在与 PowerShell 会话相同的用户上下文中运行。但某些进程可能需要提升权限才能运行。为此,我们可以使用 -Verb
参数。
请记住,您不能组合 -Verb
和 -NoNewWindow
,因为您要启动的进程必须在新窗口中打开。
要以提升的权限运行 example.bat,我们可以使用以下命令:
Start-Process -FilePath "c:\temp\example.bat" -Wait -Verb RunAs
根据文件扩展名,其他选项也是可能的。例如,我们可以使用 -Verb Print
打印文本文件。
以不同用户身份启动进程
也可以以不同的用户身份运行进程。默认情况下,该过程将使用当前登录用户的凭据执行。
首先,您需要创建一个 PSCredential 对象并将其存储为安全字符串。然后,您可以使用参数 -Credential
将凭据传递给 cmdlet。
请记住,安全字符串的使用并不是超级安全,因此请确保安全字符串尽可能安全。
# Create credential object
# You can store these also in a text file
$username = Read-Host "Enter your username"
$secureStringPwd = Read-Host -assecurestring "Please enter your password"
# Create credential object
$credObject = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd
Start-Process -FilePath "c:\temp\example.bat" -Wait -Credentials $credObject
重定向输出
Start-Process cmdlet 的输出无法传递到 PowerShell。我们唯一的选择是将输出重定向到文本文件。变量将不起作用。
因此,您可以执行以下操作来捕获输出:
# Redirect the output to example-output.txt
Start-Process -FilePath "c:\temp\example.bat" -Wait -RedirectStandardOutput c:\temp\example-output.txt
# Read the contents of example-output.txt
$output = Get-Content c:\temp\example-output.txt
行不通的是:
# Storing the output into a variable will throw an error
Start-Process -FilePath "c:\temp\example.bat" -Wait -RedirectStandardOutput $output
# $result will be empty
$result = Start-Process -FilePath "c:\temp\example.bat" -Wait
如果您只想捕获过程的错误,那么您可以使用:
Start-Process -FilePath "c:\temp\example.bat" -Wait -RedirectStandardError c:\temp\example-output.txt
获取进程ID
我想解释的最后一个选项是 -Passtru
参数。它将返回我们已经启动的进程的进程对象。当您想要在某个进程运行时间过长时自动停止该进程时,这会很有用。
$process = Start-Process "C:\temp\example.bat" -PassThru
# Get the process ud
$process.ID
# Wait 1 second
Start-Sleep 1
# Kill the process
Stop-Process -id $process.Id
总结
Start-Process cmdlet 非常适合从 PowerShell 中运行一个或多个应用程序或脚本。不幸的是,我们无法轻松捕获该过程的输出,但通过一个小解决方法,我们仍然能够在 PowerShell 中检查结果。