c# - EventWaitHandle "No handle of the given name exists"Vagrant 远程虚拟机 C# PowerShell

标签 c# powershell vagrant powershell-remoting

我有一些代码在 Vagrant Hyper-V 虚拟机上运行,​​该虚拟机会等待联系后才启动。我试图告诉 EventWaitHandler 继续通过远程 PowerShell session 。等待的代码是C#,激活的代码是PowerShell。这是等待的代码:

var users = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
var rule = new EventWaitHandleAccessRule(users, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Allow);
var security = new EventWaitHandleSecurity();
security.AddAccessRule(rule);
bool created;

var wh = new EventWaitHandle(false, EventResetMode.AutoReset, "EventName", out created, security);
Console.WriteLine("Waiting to be started...");

wh.WaitOne();

我遇到的问题是,当我尝试通过远程 powershell session 获取 eventwaithandler 时,它告诉我它不存在。以下是获取等待处理程序的 powershell 代码:

Enter-PSSession -ComputerName MyVm
# This works unless it is run in a remote session
$Mutex = [System.Threading.EventWaitHandle]::OpenExisting("EventName")

当我在远程 session 上运行此代码时,出现以下错误:

Exception calling "OpenExisting" with "1" argument(s): "No handle of the given name exists."

然后它给我失败的代码,它指向上面的“OpenExisting”行。

只是想添加一个注释,它在同一台机器上运行得很好,所以我知道它应该工作,但不能通过远程 session 。

任何帮助/指示将不胜感激。

谢谢

最佳答案

找到答案,我想将其发布在这里:

我唯一需要做的就是将“global\”放在进程名称前面 像这样初始化(C#):

var wh = new EventWaitHandle(false, EventResetMode.AutoReset, "Global\\EventName", out created, security);

并通过 PowerShell 访问它,如下所示:

$Mutex = [System.Threading.EventWaitHandle]::OpenExisting("Global\EventName")

即使是远程 session ,也能发挥作用......

关于c# - EventWaitHandle "No handle of the given name exists"Vagrant 远程虚拟机 C# PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38254870/

相关文章:

powershell - 用于将samaccountname返回到静态变量的Powershell脚本

powershell - 来自 Active Directory 的关于密码到期日期的矛盾值

powershell - 使用非字符串作为 $env : variable

php - 更新 chef php cookbook 的 php 版本

android - 从 vagrant 机器连接到 android 模拟器

c# - 如何解释 MP3 文件中的原始 pcm 数据

c# - DotLiquid,一些初学者问题/观察

c# - 从控制台 C# 添加列表项

返回多个文件的 C# MVC ActionResult

apache - Vagrant 1.2.2 未运行 Puppet Manifest