WMI 访问 UNC 路径 [英] WMI access to UNC paths

查看:37
本文介绍了WMI 访问 UNC 路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个服务器 C# 应用程序,它通过不同客户端计算机上的本地域网络执行 .exe 文件.我选择通过 WMI 执行此操作,并且当 .exe 路径在远程计算机的本地时工作正常.在此处和其他论坛上搜索其他主题时,我注意到 WMI 不支持 UNC 路径(这就是我的问题).

I'm developing a server C# application which executes a .exe file through a local domain network on different client computers. I've chosen to do it via WMI and works fine when the .exe path is local to the remote machine. Searching over other threads here and other forums I've noticed that WMI does not support UNC paths (here comes my problem).

当我调用下面的方法执行放置在远程电脑桌面上的 .exe 时,它​​工作正常:

When I call the method below to execute a .exe placed on the remote pc desktop, it just works fine:

var execResult = WmiExecuteRemoteProcess("XPSP3", @"C:\Documents and Settings\user1\Desktop\My_Setup.exe", @"domain\user", "mypass");

现在,当我尝试使用 UNC 路径时,我得到了退出代码 2:

Now, when I try to use UNC paths, I get the exit code 2:

var execResult = WmiExecuteRemoteProcess("XPSP3", @"\\server\shared\My_Setup.exe", @"domain\user", "mypass");

WmiExecuteRemoteProcess 方法如下所示:

public bool WmiExecuteRemoteProcess(string remoteComputerName, string arguments, string pUser, string pPassword)
{
    try
    {
        ConnectionOptions connOptions = new ConnectionOptions();
        connOptions.Username = pUser;
        connOptions.Password = pPassword;
        connOptions.Impersonation = ImpersonationLevel.Impersonate;
        connOptions.EnablePrivileges = true;

        ManagementScope manScope = new ManagementScope(string.Format(@"\\{0}\ROOT\CIMV2", remoteComputerName), connOptions);
        manScope.Connect();

        ObjectGetOptions objectGetOptions = new ObjectGetOptions();
        ManagementPath managementPath = new ManagementPath("Win32_Process");

        using (ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions))
        {
            using (ManagementBaseObject inParams = processClass.GetMethodParameters("Create"))
            {
                inParams["CommandLine"] = arguments;
                using (ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null))
                {
                    return (uint)outParams["returnValue"] == 0;
                }
            }
        }
    }
    catch (Exception ex)
    {
        Log.Error(ex.Message);
        return false;
    }
}

鉴于这种情况,我决定通过如下解析 arguments 参数来欺骗"它:

Given this situation I've decided kind of "cheat" it by parsing the arguments parameter as follows:

var args = "cmd.exe /c \"pushd \"\"\\\\server\\shared\"\" && My_Setup.exe && popd\"";
var execResult = WmiExecuteRemoteProcess("XPSP3",args,@"domain\user", "mypass");

我在这里尝试做的是使用带有命令 pushdpopd 的 cmd.exe 将 UNC 路径映射到基于网络驱动器的路径,例如"Z:\共享".这样 WMI 和 cmd.exe 都不必处理 UNC 路径.

What I try to do here is to use the cmd.exe with the commands pushd and popd to map the UNC path into a network drive-based path like "Z:\shared". This way both WMI and cmd.exe don't have to deal with the UNC path.

结果:同样,如果 .exe 是远程机器本地的,它运行得很好,但是当使用 UNC 路径时,只出现 cmd.exe 进程.也许它在内部再次抛出退出代码 2,但我无法捕捉到它,甚至将 cmd 执行的输出重定向到日志文件.

Result: again, if the .exe is local to the remote machine, it works very well, but when using a UNC path, only the cmd.exe process appears. Maybe it's internally throwing the exit code 2 again, but I'm not able to catch it, even redirecting the output of the cmd execution to a log file.

也许对这种机制有经验的人可以对此有所了解.我不想只为此开发整个服务,也不想使用 PsExec(也许这是最后的手段).

Perhaps someone experienced in this kind of mechanics can throw some light on this. I'd prefer not to develop an entire service only for this, or to use PsExec (maybe this one as a last resort).

如果我遗漏了任何信息,请告诉我.任何意见将不胜感激.

Please let me know if I'm missing any info. Any comments will be much appreciated.

问候.

我检查过,这与共享文件夹或文件的权限无关.

I checked and it's not a matter of permissions to the shared folder or file.

推荐答案

如果有人遇到这个或其他相关问题,我是这样解决的:

In case anyone faces this or other related issue, this is how I solved it:

问题不是 WMI 无法处理 UNC 路径,而是由于 Windows 中的安全限制,不允许 WMI 操作访问网络资源.如果您映射路径并不重要,它只是未经授权.在这种特殊情况下,我最终采用的解决方法是将 setup.exe 复制到远程计算机中的一个临时文件夹中,最后通过 WMI 访问其本地路径来执行它,就像我之前所做的一样.

The issue is not that WMI cannot deal with UNC paths, but WMI operations are not allowed to access network resources due to security restrictions in Windows. It doesn't matter if you map the paths, it's just not authorized. In this particular case the workaround I ended up with, was to copy the setup.exe to a temporary folder in the remote machine, and finally execute it through WMI by accessing its local path just like I was doing before.

var execResult = WmiExecuteRemoteProcess("XPSP3", @"C:\temp_folder\My_Setup.exe", @"domain\user", "mypass");

这篇关于WMI 访问 UNC 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆