简体   繁体   English

您好,我需要知道如何启动10个记事本,然后使用cmdlet停止所有10个进程(仅适用于Powershell)

[英]Hello, I need to know how to start 10 notepad and then stop all 10 processes using cmdlets (powershell only)

Start-process notepad | 开始过程记事本| stop-process 停止过程

pretty much all I know what to do. 我几乎知道该怎么做。

Start-Process just starts a process; Start-Process只是启动一个过程; it does not provide a way to reference that process after it has been started. 它没有提供启动该过程后引用该过程的方法。 Another way to do it would be: 另一种方法是:

# // Declare an array to hold Process IDs
$aProcessIDs = @()

# // Create a Wscript.Shell object to use for running processes
$oWshShell = New-Object -ComObject Wscript.Shell

# // Start 10 instances of notepad
for ($i=1;$i -le 10;$i++)
{
    # // Run notepad.exe
    $oProcess = $oWshShell.Exec('notepad.exe')

    # // Add the ProcessID of the running process to the array
    $aProcessIDs += $oProcess.ProcessID
}

# /// Wait 10 seconds
Start-Sleep -s 10

# // Terminate all processes
foreach ($iProcessID in $aProcessIDs)
{
    Stop-Process -Id $iProcessID
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在PowerShell中启动和停止进程? - How to start and stop processes in PowerShell? Powershell 脚本停止/启动 IIS 在 Windows 10 - Powershell Script to stop/start IIS in Windows 10 是否可以/如何使用某些cmdlet停止powershell? - Is it possible to/how do you stop powershell using certain cmdlets? 如何仅使用 CIM cmdlet(而非 WMI cmdlet)在 Powershell 中创建 VSS 卷影副本? - How to create a VSS shadow copy in Powershell using only CIM cmdlets (not WMI cmdlets)? 在PowerShell中使用Get-EventLog如何在消息中仅显示10个字符 - Using Get-EventLog in PowerShell how can I show only 10 characters in the message 如何使用Powershell cmdlet处理logman结果? - How do I work with logman results using powershell cmdlets? 如何在powershell中列出所有已安装、可运行的cmdlet? - How to list all installed, runnable cmdlets in powershell? 我正在尝试编写将下载所有附件的Powershell脚本,当我下载仅前10个附件时 - I'm trying to Write a Powershell script that will download all attachments, When i it Downloads the first 10 attachments only 如何使用PowerShell在IIS中停止和启动各个网站? - How can I stop and start individual websites in IIS using PowerShell? 如何在 powershell 脚本结束时自动停止所有启动的进程 - How to automatically stop all started processes at the end of a powershell script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM