Network Authentication when running exe from WMI

前端 未结 4 1888
一整个雨季
一整个雨季 2021-02-04 12:01

I have a C# exe that needs to be run using WMI and access a network share. However, when I access the share I get an UnauthorizedAccessException. If I run the exe directly the s

相关标签:
4条回答
  • 2021-02-04 12:08

    I know you've sorted it by using PSEXEC, which is a fantastic program, but if you did want to go back to WMI, have you tried enabling the following in your ConnectionOptions:

    • The flag EnablePrivileges
    • setting the Impersonation to ImpersonationLevel.Impersonate

    Which does the following:

    Gets or sets a value indicating whether user privileges need to be enabled for the connection operation. This property should only be used when the operation performed requires a certain user privilege to be enabled (for example, a machine restart).

    http://msdn.microsoft.com/en-us/library/system.management.connectionoptions.enableprivileges.aspx


    Gets or sets the COM impersonation level to be used for operations in this connection.

    http://msdn.microsoft.com/en-us/library/system.management.connectionoptions.impersonation.aspx

    I think they should tell your WMI to actually allow the program to have the correct credentials and thus access your network share

    0 讨论(0)
  • 2021-02-04 12:11

    You can write all you commands to batch file to the remote machine which includes net use (no need to use a drive letter) to do an authentication. Works fine that way. I am still working on an alternative.

    0 讨论(0)
  • 2021-02-04 12:22

    WMI just uses impersonation when executing the remote process, which does not give you network access. If you are ok going outside managed code, you can just map a UNC path in the remote process WMI started using whatever credentials you want. Then, you have the network access you want. I use NetUseAdd and NetUseDel from netapi32.dll to map the UNC path. See http://pinvoke.net/ for details on the using the APIs.

    0 讨论(0)
  • 2021-02-04 12:31

    Having followed the link suggested by Isalamon above (thanks) I followed Jestro's advice and have rewritten using psexec.exe (which can be downloaded from http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) instead of WMI. It feels like a bit of a kludge to do it this way, but it seems to work.

    New code for anyone who is experiencing similar problems:

    Process proc = new Process();
    proc.StartInfo.FileName = "PsExec.exe";
    proc.StartInfo.Arguments = string.Format("\\\\{0} -d -u {1}\\{2} -p {3} {4}",
                                             remoteHost,
                                             domain,
                                             username,
                                             password,
                                             commandLine);
    proc.StartInfo.CreateNoWindow = true;
    proc.StartInfo.UseShellExecute = false;
    proc.Start();
    
    0 讨论(0)
提交回复
热议问题