监控 powershell 脚本的挂起实例
我正在编写一个 Powershell 脚本来执行一系列操作,完成后它将作为计划任务运行。出于这个原因,我希望能够在开始运行脚本时检查旧实例是否仍然存在,并杀死旧实例(如果存在)。
我想我会使用这样的东西:
$process = Get_Process | $name
$process.kill
但是如何以简单的方式获取 $name 变量?
有人有更好的建议吗? 此致, 吉斯利
I´m writing a Powershell script to do a bunch of things and when finished it will be run as a scheduled task. For that reason I want to be able to check whether an older instance is still alive when I start running the script and kill the older one if it exists.
I was thinking I would use something like this:
$process = Get_Process | $name
$process.kill
But how to get the $name variable in a simple way?
Does anyone have a better suggestion?
Best regards,
Gísli
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 Windows 计划任务配置中执行此操作。这些设置取决于您使用的操作系统。
编辑:也就是说,您可以将任务配置为在一段时间后(即下一个任务开始时)被终止。
You can do this in windows scheduled task configuration. The settings depends on the OS you are using though.
EDIT: that is you can configure the task to be killed after a certain period of time (i.e. when your next one starts).
为什么需要获取名字?
Get-Process
返回高保真Process
对象,您可以直接对其进行操作。要获取特定名称的进程,请使用
$n = Get-Process notepad
,然后执行$n.kill()
来终止它。如果您确实需要再次检查名称,请执行$n.Name
。要查看可以使用哪些属性和方法,请尝试$n | get-member
并确保您阅读手册:http:// technet.microsoft.com/en-us/library/dd347630.aspx
Why do you need to get the name?
Get-Process
returns high fidelityProcess
objects and you can operate on it directly.To get a process of a particular name use
$n = Get-Process notepad
, say, and then do$n.kill()
to kill it. If you do need to check the name again, do$n.Name
. To see what properties and methods you can use, try$n | get-member
And make sure you read the manual: http://technet.microsoft.com/en-us/library/dd347630.aspx