简体   繁体   English

阻止用户使用 PowerShell 打开已经存在的程序

[英]Stop user from opening an already program using PowerShell

I want to stop the user from running another instance of an already running program/service in Windows using PowerShell.我想阻止用户使用 PowerShell 在 Windows 中运行另一个已经运行的程序/服务实例。
Eg: I have notepad opened, then for minute's time period I want to disable the option to open notepad, since it already is running.例如:我打开了记事本,然后在几分钟的时间内我想禁用打开记事本的选项,因为它已经在运行。

As of now, I can detect if the program is open or not, and if not I may have it opened for user (code attached).到目前为止,我可以检测程序是否打开,如果没有,我可以为用户打开它(附上代码)。

$processName = Get-Process notepad -ErrorAction SilentlyContinue

if ( $processName ) {
    Write-Host 'Process is already running!'
    #stop another instance of notepad to be opened, since it is already running
}
else {
    $userChoice = Read-Host 'Process is not running, should I start it? (Y/N)  '

    if ($userChoice -eq 'Y') {
        Start-Process notepad
    }
    else {
        Write-Host 'No Problem!'
    }
}


But, how can I disable the option for the user to open another instance of the same?但是,如何禁用用户打开另一个相同实例的选项?

Any lead for the same would be helpful.任何相同的线索都会有所帮助。

Since Windows doesn't have such a feature that prevents launching multiple copies of an executable, you have two options:由于 Windows 没有阻止启动多个可执行文件副本的功能,因此您有两个选择:

  1. poll process list every now and then.不时轮询进程列表。 Terminate extra instances of the application终止应用程序的额外实例
  2. create a wrapper to the application and use a mutex to prevent multiple copies为应用程序创建一个包装器并使用互斥锁来防止多个副本

The first option has its caveats.第一个选项有其警告。 If additional copies are launched, it takes on the average half the polling interval to detect those.如果启动额外的副本,平均需要一半的轮询间隔来检测这些副本。 What's more, which of the processes are to be terminated?更重要的是,哪些进程将被终止? The eldest?最年长的? The youngest?最小的? Some other criteria?其他一些标准?

The second one can be circumvented easily by just launching the application itself.只需启动应用程序本身即可轻松绕过第二个。

The only real solution is to implement a single-instance feature in the application itself.唯一真正的解决方案是在应用程序本身中实现单实例功能。 Games often do this.游戏经常这样做。 For business software, be wary that the users will hate you, if there is a single reason why running multiple instances would be of use.对于商业软件,请注意用户会讨厌您,如果运行多个实例的原因只有一个。 Yes, especially if that use case would be absurd .是的,特别是如果那个用例是荒谬的

As an example of a mutex-based launcher, consider the following function作为基于互斥锁的启动器的示例,请考虑以下函数

function Test-Mutex {
    $mtx = new-object System.Threading.Mutex($false, "SingleInstanceAppLauncher")

    if(-not $mtx.WaitOne(0, $false)) {
        write-host "Mutex already acquired, will not launch second instance!"
        read-host "Any key"
        return
    }
    write-host "Running instance #1"
    read-host "Any key"
    # Do stuff
}

As like the solution 2 caveat, any user can work around the limit by just executing the do suff part.就像解决方案 2 的警告一样,任何用户都可以通过执行do suff部分来绕过限制。 Remember, the wrapper prevents launching multiple instances of the wrapper , not about the do stuff .请记住,包装防止启动包装的多个实例,而不是对做的东西

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM