Powershell帮助,如果进程存在,停止它,否则启动服务?
我对 Powershell 还很陌生。我正在运行 2 个不同的脚本,我想将它们合并为一个脚本。
脚本 1 有 1 行,
Stop-Process -ProcessName alcore.* -force
其目的是结束任何以“alcore”开头的进程。
脚本 2 也有 1 行,
Start-Service -displayname crk*
它启动任何以 crk 开头的服务。
我怎样才能将这些合并到一个脚本中?如果进程正在运行,我希望停止它们,如果没有,我希望启动服务。我怎样才能做到这一点?
我正在尝试这个,但它不起作用
$services = Get-Process alcore.*
if($services.Count -qe 1){
Stop-Process -ProcessName alcore.* -force
} else {
Start-Service -displayname crk*
}
我怎样才能正确地做到这一点?我还应该将它们包装在一个函数中并调用该函数吗?这样看起来比较干净一点。感谢您的任何帮助。
干杯,
〜ck
I'm pretty new to Powershell. I have 2 different scripts I'm running that I would like to combine into one script.
Script 1 has 1 line
Stop-Process -ProcessName alcore.* -force
It's purpose is to end any process that begines with "alcore."
Script 2 has 1 line as well
Start-Service -displayname crk*
It starts any service that begins with crk.
How can I combine these into one script? If the processes are running I wish to stop them, if not, I wish to start the services. How can I accomplish this?
I'm trying this but it's not working
$services = Get-Process alcore.*
if($services.Count -qe 1){
Stop-Process -ProcessName alcore.* -force
} else {
Start-Service -displayname crk*
}
How can I do this correctly? Also should I wrap these up in a function and call the function? That seems a bit cleaner. Thanks for any help.
Cheers,
~ck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Get-Service 获取服务状态。该进程可能正在运行,但服务可能已暂停:
Use Get-Service to get the service status. The process could be running but the service might be paused: