使用不同凭据从共享网络运行vbs脚本。(winforms)

时间:2015-04-17 12:34:15

标签: c# .net winforms vbscript smb

我有一个要求是执行位于共享网络驱动器中的vbscript。即:\ _SERVER1 \ shared $ \ path \ script.vbs

要连接到此共享文件夹,我需要传递凭据,即:

DOMAIN \ AdminShare 1234

此脚本必须使用本地管理员凭据运行。即:

\管理员 1234

执行exe的用户也拥有自己的凭据,即:

域\用户 1234

如何管理此方案?

我已成功连接到具有此类的适当凭据的smb:

using System;
using System.Runtime.InteropServices;
using BOOL = System.Boolean;
using DWORD = System.UInt32;
using LPWSTR = System.String;
using NET_API_STATUS = System.UInt32;

namespace blah
{
    class UNCAccess
    {
        // FROM: https://ericwijaya.wordpress.com/2013/02/06/access-remote-file-share-with-username-and-password-in-c/
        //
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        internal struct USE_INFO_2
        {
            internal LPWSTR ui2_local;
            internal LPWSTR ui2_remote;
            internal LPWSTR ui2_password;
            internal DWORD ui2_status;
            internal DWORD ui2_asg_type;
            internal DWORD ui2_refcount;
            internal DWORD ui2_usecount;
            internal LPWSTR ui2_username;
            internal LPWSTR ui2_domainname;
        }
        [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        internal static extern NET_API_STATUS NetUseAdd(
        LPWSTR UncServerName,
        DWORD Level,
        ref USE_INFO_2 Buf,
        out DWORD ParmError);
        [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        internal static extern NET_API_STATUS NetUseDel(
        LPWSTR UncServerName,
        LPWSTR UseName,
        DWORD ForceCond);
        private string sUNCPath;
        private string sUser;
        private string sPassword;
        private string sDomain;
        private int iLastError;
        public UNCAccess()
        {
        }
        public UNCAccess(string UNCPath, string User, string Domain, string Password)
        {
            login(UNCPath, User, Domain, Password);
        }
        public int LastError
        {
            get { return iLastError; }
        }
        /// <summary>
        /// Logs in to the shared network with the provided credentials
        /// </summary>
        /// <param name="UNCPath">unc</param>
        /// <param name="User">user</param>
        /// <param name="Domain">domain</param>
        /// <param name="Password">password</param>
        /// <returns>TRUE OK, ELSE FALSE</returns>
        public bool login(string UNCPath, string User, string Domain, string Password)
        {
            sUNCPath = UNCPath;
            sUser = User;
            sPassword = Password;
            sDomain = Domain;
            return NetUseWithCredentials();
        }
        private bool NetUseWithCredentials()
        {
            uint returncode;
            try
            {
                USE_INFO_2 useinfo = new USE_INFO_2();

                useinfo.ui2_remote = sUNCPath;
                useinfo.ui2_username = sUser;
                useinfo.ui2_domainname = sDomain;
                useinfo.ui2_password = sPassword;
                useinfo.ui2_asg_type = 0;
                useinfo.ui2_usecount = 1;
                uint paramErrorIndex;
                returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                iLastError = (int)returncode;
                return returncode == 0;
            }
            catch
            {
                iLastError = Marshal.GetLastWin32Error();
                return false;
            }
        }
        ///

        /// Closes the UNC share
        ///  
        /// True if closing was successful
        public bool NetUseDelete()
        {
            uint returncode;
            try
            {
                returncode = NetUseDel(null, sUNCPath, 2);
                iLastError = (int)returncode;
                return (returncode == 0);
            }
            catch
            {
                iLastError = Marshal.GetLastWin32Error();
                return false;
            }
        }
    }
}

然后我做了一个流程开始以管理员身份运行脚本:

Process p = new Process();
p.StartInfo.FileName = "cscript.exe";
p.StartInfo.WorkingDirectory = @"c:\";
p.StartInfo.Arguments = "//B //Nologo " + script.FullName
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.Verb = "runas";
p.StartInfo.UserName = localAdminAccount;
System.Security.SecureString pwd = new System.Security.SecureString();
foreach (char c in localAdminPasswd) { pwd.AppendChar(c); }
p.StartInfo.Password = pwd;

问题是,当进程以新凭据(localAdmin)开始时,我无法找到脚本(用户已更改,因此无法访问共享网络)。

我虽然是因为用户,所以我也尝试创建一个启动器来提升主应用程序执行的权限,而无需用户交互(另一个来自启动器的process.start),很好,但是同样的事情发生了(没找到)。

对此有何帮助?感谢

1 个答案:

答案 0 :(得分:0)

我已经用两个发射器解决了它,以便在进程启动时获得权限和runas,所以现在我可以使用必要的凭据运行脚本=)