Example #1
0
        public static int Main(string[] args)
        {
            ManagementPath path = new ManagementPath("\\\\products1\\root\\cimv2:Win32_Something.A=10,B='haha'");

            Console.WriteLine(path.ToString());
            Console.WriteLine("Path[" + path.Path + "]");
            Console.WriteLine("Server[" + path.Server + "]");
            Console.WriteLine("ClassName[" + path.ClassName + "]");
            Console.WriteLine("RelativePath[" + path.RelativePath + "]");
            Console.WriteLine("NamespacePath[" + path.NamespacePath + "]");

            ManagementPath path2 = new ManagementPath();

            path2.ClassName = "Fred";
            Console.WriteLine("Path[" + path2.Path + "]");
            path2.SetAsSingletion();
            Console.WriteLine("Path[" + path2.Path + "]");

            path2.SetAsClass();
            Console.WriteLine("Path[" + path2.Path + "]");

            return(0);
        }
        /// <summary>Connect method invoked by WinCs. </summary>
        /// <param name="taskId">Database assigned task Id.</param>
        /// <param name="connectionParameterSets">List of credential sets</param>
        /// <param name="tftpDispatcher">Tftp Listener</param>
        /// <returns>Operation results.</returns>
        public ConnectionScriptResults Connect(
            long taskId,
            IDictionary <string, string>[] connectionParameterSets,
            string tftpPath,
            string tftpPath_login,
            string tftpPath_password,
            ITftpDispatcher tftpDispatcher)
        {
            Stopwatch executionTimer = Stopwatch.StartNew();

            m_taskId = taskId.ToString();
            ConnectionScriptResults result = null;

            Lib.Logger.TraceEvent(TraceEventType.Start,
                                  0,
                                  "Task Id {0}: Connection script WindowsOracleRemoteExecConnectionScript.",
                                  m_taskId);

            if (null == connectionParameterSets)
            {
                Lib.Logger.TraceEvent(TraceEventType.Error,
                                      0,
                                      "Task Id {0}: Null credential set passed to WindowsOracleRemoteExecConnectionScript.",
                                      m_taskId);
                result = new ConnectionScriptResults(null,
                                                     ResultCodes.RC_NULL_PARAMETER_SET,
                                                     0,
                                                     -1);
            }
            else
            {
                try {
                    ResultCodes resultCode = ResultCodes.RC_HOST_CONNECT_FAILED;
                    Lib.Logger.TraceEvent(TraceEventType.Information,
                                          0,
                                          "Task Id {0}: Executing WindowsOracleRemoteExecConnectionScript with {1} credential sets.",
                                          m_taskId,
                                          connectionParameterSets.Length.ToString());

                    //
                    // Loop to process credential sets until a successful
                    // WMI connection is made.
                    for (int i = 0;
                         connectionParameterSets.Length > i;
                         ++i)
                    {
                        ConnectionOptions co = new ConnectionOptions();
                        co.Impersonation  = ImpersonationLevel.Impersonate;
                        co.Authentication = AuthenticationLevel.Packet;
                        //co.Timeout = Lib.WmiConnectTimeout;

                        string userName = connectionParameterSets[i][@"userName"];
                        string password = connectionParameterSets[i][@"password"];

                        if (null != userName && null != password && !("." == userName && "." == password))
                        {
                            co.Username = userName;
                            co.Password = password;
                        }

                        Lib.Logger.TraceEvent(TraceEventType.Information,
                                              0,
                                              "Task Id {0}: Processing credential set {1}: user=\"{2}\".",
                                              m_taskId,
                                              i.ToString(),
                                              (null == co.Username) ? String.Empty : co.Username);

                        Stopwatch      sw = new Stopwatch();
                        ManagementPath mp = null;

                        try {
                            string server = connectionParameterSets[i][@"hostName"];

                            //
                            // Create a scope for the cimv2 namespace.
                            mp               = new ManagementPath();
                            mp.Server        = server;
                            mp.NamespacePath = ManagementScopeNames.CIMV;

                            ManagementScope cimvScope = new ManagementScope(mp, co);
                            Lib.Logger.TraceEvent(TraceEventType.Information,
                                                  0,
                                                  "Task Id {0}: Attempting connection to namespace {1}.",
                                                  m_taskId,
                                                  mp.ToString());
                            sw.Start();
                            cimvScope.Connect();
                            sw.Stop();
                            Debug.Assert(cimvScope.IsConnected);
                            Lib.Logger.TraceEvent(TraceEventType.Information,
                                                  0,
                                                  "Task Id {0}: Connect to {1} succeeded. Elapsed time {2}.",
                                                  m_taskId,
                                                  mp.ToString(),
                                                  sw.Elapsed.ToString());

                            //
                            // Create a scope for the default namespace.
                            mp               = new ManagementPath();
                            mp.Server        = server;
                            mp.NamespacePath = ManagementScopeNames.DEFAULT;

                            ManagementScope defaultScope = new ManagementScope(mp, co);
                            Lib.Logger.TraceEvent(TraceEventType.Information,
                                                  0,
                                                  "Task Id {0}: Attempting connection to namespace {1}.",
                                                  m_taskId,
                                                  mp.ToString());
                            sw.Reset();
                            defaultScope.Connect();
                            sw.Stop();
                            Debug.Assert(defaultScope.IsConnected);
                            Lib.Logger.TraceEvent(TraceEventType.Information,
                                                  0,
                                                  "Task Id {0}: Connect to {1} succeeded. Elapsed time {2}.",
                                                  m_taskId,
                                                  mp.ToString(),
                                                  sw.Elapsed.ToString());

                            Dictionary <string, object> connectionDic = new Dictionary <string, object>();

                            foreach (KeyValuePair <string, string> kvp in connectionParameterSets[i])
                            {
                                connectionDic.Add(kvp.Key, kvp.Value);
                            }

                            connectionDic[@"cimv2"]   = cimvScope;
                            connectionDic[@"default"] = defaultScope;

                            string oracleHome     = null;
                            string schemaName     = null;
                            string schemaPassword = null;
                            string tempDir        = null;

                            resultCode = ValidateScriptParameters(connectionDic,
                                                                  ref oracleHome,
                                                                  ref schemaName,
                                                                  ref schemaPassword);

                            if (ResultCodes.RC_SUCCESS == resultCode)
                            {
                                resultCode = ValidateTemporaryDirectory(cimvScope,
                                                                        connectionParameterSets[i],
                                                                        connectionDic,
                                                                        ref tempDir);
                            }

                            if (ResultCodes.RC_SUCCESS == resultCode)
                            {
                                string batchFile = BuildBatchFile(tempDir,
                                                                  oracleHome,
                                                                  schemaName,
                                                                  schemaPassword);
                                resultCode = ExecuteRemoteProcess(cimvScope,
                                                                  batchFile,
                                                                  schemaName,
                                                                  connectionDic,
                                                                  tftpPath,
                                                                  tftpPath_login,
                                                                  tftpPath_password,
                                                                  tftpDispatcher);
                            }

                            if (ResultCodes.RC_SUCCESS == resultCode)
                            {
                                result = new ConnectionScriptResults(connectionDic,
                                                                     ResultCodes.RC_SUCCESS,
                                                                     0,
                                                                     i);
                                break;
                            }
                        } catch (ManagementException me) {
                            if (ManagementStatus.AccessDenied == me.ErrorCode)
                            {
                                Lib.Logger.TraceEvent(TraceEventType.Error,
                                                      0,
                                                      "Task Id {0}: Connect to {1} failed.  Elapsed time {2}.\nWMI Error Code {3}",
                                                      m_taskId,
                                                      mp.ToString(),
                                                      sw.Elapsed.ToString(),
                                                      me.ErrorCode.ToString());
                                resultCode = ResultCodes.RC_LOGIN_FAILED;
                            }
                            else
                            {
                                //
                                // If we received a management exception that is *not*
                                // access denied, then there is no point in attempting
                                // additional logins with different user names/passwords.
                                Lib.LogManagementException(m_taskId,
                                                           sw,
                                                           String.Format("Connect to {0} failed.", mp.ToString()),
                                                           me);
                                break;
                            }
                        } catch (UnauthorizedAccessException uae) {
                            Lib.Logger.TraceEvent(TraceEventType.Error,
                                                  0,
                                                  "Task Id {0}: Connect to {1} failed.  Elapsed time {2}.\nMessage: {3}.",
                                                  m_taskId,
                                                  mp.ToString(),
                                                  sw.Elapsed.ToString(),
                                                  uae.Message);
                            resultCode = ResultCodes.RC_LOGIN_FAILED;
                        } catch (COMException ce) {
                            if (0x800706BA == (UInt32)ce.ErrorCode)
                            {
                                Lib.Logger.TraceEvent(TraceEventType.Error,
                                                      0,
                                                      "Task Id {0}: Connect to {1} failed.  Elapsed time {2}.\nCOM Exception {3}.\n" +
                                                      "WMI port (135) may be closed or DCOM may not be properly configured",
                                                      m_taskId,
                                                      mp.ToString(),
                                                      sw.Elapsed.ToString(),
                                                      ce.Message);
                            }
                            else
                            {
                                Lib.LogException(m_taskId,
                                                 sw,
                                                 String.Format("Connect to {0} failed", mp.ToString()),
                                                 ce);
                            }

                            break;
                        } catch (Exception ex) {
                            Lib.LogException(m_taskId,
                                             sw,
                                             String.Format("Connect to {0} failed", mp.ToString()),
                                             ex);
                            break;
                        }
                    }

                    //
                    // Connect failed after all credentials attempted.
                    if (null == result)
                    {
                        result = new ConnectionScriptResults(null,
                                                             resultCode,
                                                             0,
                                                             connectionParameterSets.Length);
                    }
                } catch (Exception e) {
                    Lib.Logger.TraceEvent(TraceEventType.Error,
                                          0,
                                          "Task Id {0}: Unhandled exception in WindowsOracleRemoteExecConnectionScript.\n{1}",
                                          m_taskId,
                                          e.ToString());

                    //
                    // This is really an unanticipated fail safe.  We're
                    // going to report that *no* credentials were tried, which
                    // actually may not be true...
                    result = new ConnectionScriptResults(null,
                                                         ResultCodes.RC_PROCESSING_EXCEPTION,
                                                         0,
                                                         -1);
                }
            }

            Debug.Assert(null != result);
            Lib.Logger.TraceEvent(TraceEventType.Stop,
                                  0,
                                  "Task Id {0}: Connection script WindowsOracleRemoteExecConnectionScript.  Elapsed time {1}.  Result code {2}.",
                                  m_taskId,
                                  executionTimer.Elapsed.ToString(),
                                  result.ResultCode.ToString());
            return(result);
        }
Example #3
0
        /// <summary>
        /// Connect method invoked by WinCs.
        /// </summary>
        ///
        /// <param name="taskId">Database assigned task Id.</param>
        /// <param name="connectionParameterSets">List of credential sets to use for connecting to the
        ///     remote database server.</param>
        /// <param name="tftpDispatcher">TFTP transfer request listener for dispatching TFTP transfer
        ///     requests.</param>
        ///
        /// <returns>Operation results.</returns>
        public ConnectionScriptResults Connect(
            long taskId,
            IDictionary <string, string>[]   connectionParameterSets,
            string tftpPath,
            string tftpPath_login,
            string tftpPath_password,
            ITftpDispatcher tftpDispatcher)
        {
            Stopwatch executionTimer = Stopwatch.StartNew();
            string    taskIdString   = taskId.ToString();

            Lib.Logger.TraceEvent(TraceEventType.Start,
                                  0,
                                  "Task Id {0}: Connection script ConnWincsScript.",
                                  taskIdString);
            ConnectionScriptResults result = null;

            if (null == connectionParameterSets)
            {
                Lib.Logger.TraceEvent(TraceEventType.Error,
                                      0,
                                      "Task Id {0}: Null credential set passed to ConnWincsScript",
                                      taskIdString);
                result = new ConnectionScriptResults(null,
                                                     ResultCodes.RC_NULL_PARAMETER_SET,
                                                     0,
                                                     -1);
            }
            else
            {
                try {
                    ResultCodes resultCode = ResultCodes.RC_HOST_CONNECT_FAILED;
                    Lib.Logger.TraceEvent(TraceEventType.Information,
                                          0,
                                          "Task Id {0}: Executing ConnWincsScript with {1} credential sets.",
                                          taskIdString,
                                          connectionParameterSets.Length.ToString());
                    //IDictionary<string, string>[] orderedConnectionParameterSets = this.reorderCredentials(connectionParameterSets);
                    List <int> orderedConnections = this.reorderCredentials(connectionParameterSets);
                    //
                    // Loop to process credential sets until a successful
                    // WMI connection is made.
                    for (int j = 0;
                         orderedConnections.Count > j;
                         ++j)
                    {
                        ConnectionOptions co = new ConnectionOptions();
                        co.Impersonation    = ImpersonationLevel.Impersonate;
                        co.Authentication   = AuthenticationLevel.Packet;
                        co.Timeout          = Lib.WmiConnectTimeout;
                        co.EnablePrivileges = true;  // @todo configuration

                        int    i        = orderedConnections[j];
                        string userName = connectionParameterSets[i][@"userName"];
                        string password = connectionParameterSets[i][@"password"];

                        if (null != userName && null != password && !("." == userName && "." == password))
                        {
                            co.Username = userName;
                            co.Password = password;
                        }

                        Lib.Logger.TraceEvent(TraceEventType.Information,
                                              0,
                                              "Task Id {0}: Processing credential set {1}: user=\"{2}\".",
                                              taskIdString,
                                              i.ToString(),
                                              userName);

                        if (string.IsNullOrEmpty(userName))
                        {
                            Lib.Logger.TraceEvent(TraceEventType.Information,
                                                  0,
                                                  "Task Id {0}: Unexpected Error: Receiving null username at Credential set {1}: user=\"{2}\".",
                                                  taskIdString,
                                                  i.ToString(),
                                                  userName);
                            continue;
                        }

                        Stopwatch      sw = new Stopwatch();
                        ManagementPath mp = null;

                        try {
                            string server = connectionParameterSets[i][@"address"];
                            string domain = string.Empty;
                            if (connectionParameterSets[i].ContainsKey(@"osWkgrpDomain"))
                            {
                                domain = connectionParameterSets[i][@"osWkgrpDomain"];
                                if (domain == @"__BDNA_DEFAULT__")
                                {
                                    domain = "";
                                }
                            }
                            Lib.Logger.TraceEvent(TraceEventType.Information,
                                                  0,
                                                  "Task Id {0}: Attempting connection to computer in domain {1}.",
                                                  taskIdString,
                                                  domain);

                            //
                            // Create a scope for the cimv2 namespace.
                            mp               = new ManagementPath();
                            mp.Server        = server;
                            mp.NamespacePath = ManagementScopeNames.CIMV;

                            ManagementScope cimvScope = new ManagementScope(mp, co);
                            Lib.Logger.TraceEvent(TraceEventType.Information,
                                                  0,
                                                  "Task Id {0}: Attempting connection to namespace {1}.",
                                                  taskIdString,
                                                  mp.ToString());
                            sw.Start();
                            cimvScope.Connect();
                            sw.Stop();
                            Debug.Assert(cimvScope.IsConnected);
                            Lib.Logger.TraceEvent(TraceEventType.Information,
                                                  0,
                                                  "Task Id {0}: Connect to {1} succeeded. Elapsed time {2}.",
                                                  taskIdString,
                                                  mp.ToString(),
                                                  sw.Elapsed.ToString());

                            //
                            // Create a scope for the default namespace.
                            mp               = new ManagementPath();
                            mp.Server        = server;
                            mp.NamespacePath = ManagementScopeNames.DEFAULT;

                            ManagementScope defaultScope = new ManagementScope(mp, co);
                            Lib.Logger.TraceEvent(TraceEventType.Information,
                                                  0,
                                                  "Task Id {0}: Attempting connection to namespace {1}.",
                                                  taskIdString,
                                                  mp.ToString());
                            sw.Reset();
                            defaultScope.Connect();
                            sw.Stop();
                            Debug.Assert(defaultScope.IsConnected);
                            Lib.Logger.TraceEvent(TraceEventType.Information,
                                                  0,
                                                  "Task Id {0}: Connect to {1} succeeded. Elapsed time {2}.",
                                                  taskIdString,
                                                  mp.ToString(),
                                                  sw.Elapsed.ToString());

                            ManagementScope tsScope = null;
                            try {
                                //
                                // Create a scope for the TerminalServices namespace.
                                mp                = new ManagementPath();
                                mp.Server         = server;
                                mp.NamespacePath  = @"root\CIMV2\TerminalServices";
                                co.Authentication = AuthenticationLevel.PacketPrivacy;
                                tsScope           = new ManagementScope(mp, co);
                                Lib.Logger.TraceEvent(TraceEventType.Information,
                                                      0,
                                                      "Task Id {0}: Attempting connection to namespace {1}.",
                                                      taskIdString,
                                                      mp.ToString());
                                sw.Reset();
                                tsScope.Connect();
                                sw.Stop();
                                Lib.Logger.TraceEvent(TraceEventType.Information,
                                                      0,
                                                      "Task Id {0}: Connect to {1} succeeded. Elapsed time {2}.",
                                                      taskIdString,
                                                      mp.ToString(),
                                                      sw.Elapsed.ToString());
                            } catch (Exception ex) {
                                tsScope = null;
                                Lib.LogException(taskIdString,
                                                 sw,
                                                 String.Format("Connect to {0} failed", mp.ToString()),
                                                 ex);
                            }


                            ManagementScope virtualizScope = null;
                            try   {
                                //
                                // Create a scope for the virtualization namespace.
                                mp                = new ManagementPath();
                                mp.Server         = server;
                                mp.NamespacePath  = @"root\virtualization";
                                co.Authentication = AuthenticationLevel.PacketPrivacy;
                                virtualizScope    = new ManagementScope(mp, co);
                                Lib.Logger.TraceEvent(TraceEventType.Information,
                                                      0,
                                                      "Task Id {0}: Attempting connection to namespace {1}.",
                                                      taskIdString,
                                                      mp.ToString());
                                sw.Reset();
                                virtualizScope.Connect();
                                sw.Stop();
                                Lib.Logger.TraceEvent(TraceEventType.Information,
                                                      0,
                                                      "Task Id {0}: Connect to {1} succeeded. Elapsed time {2}.",
                                                      taskIdString,
                                                      mp.ToString(),
                                                      sw.Elapsed.ToString());
                            }   catch (Exception ex)   {
                                virtualizScope = null;
                                Lib.LogException(taskIdString,
                                                 sw,
                                                 String.Format("Connect to {0} failed", mp.ToString()),
                                                 ex);
                            }

                            ManagementScope virtualizv2Scope = null;
                            try   {
                                //
                                // Create a scope for the virtualization\v2 namespace.
                                mp                = new ManagementPath();
                                mp.Server         = server;
                                mp.NamespacePath  = @"root\virtualization\v2";
                                co.Authentication = AuthenticationLevel.PacketPrivacy;
                                virtualizv2Scope  = new ManagementScope(mp, co);
                                Lib.Logger.TraceEvent(TraceEventType.Information,
                                                      0,
                                                      "Task Id {0}: Attempting connection to namespace {1}.",
                                                      taskIdString,
                                                      mp.ToString());
                                sw.Reset();
                                virtualizv2Scope.Connect();
                                sw.Stop();
                                Lib.Logger.TraceEvent(TraceEventType.Information,
                                                      0,
                                                      "Task Id {0}: Connect to {1} succeeded. Elapsed time {2}.",
                                                      taskIdString,
                                                      mp.ToString(),
                                                      sw.Elapsed.ToString());
                            }   catch (Exception ex)   {
                                virtualizv2Scope = null;
                                Lib.LogException(taskIdString,
                                                 sw,
                                                 String.Format("Connect to {0} failed", mp.ToString()),
                                                 ex);
                            }

                            //
                            // We have everything we need.  Create a dictionary
                            // to return as the "connection" and get out of
                            // loop.
                            Dictionary <string, object> connectionDic = new Dictionary <string, object>();
                            foreach (KeyValuePair <string, string> kvp in connectionParameterSets[i])
                            {
                                connectionDic.Add(kvp.Key, kvp.Value);
                            }
                            connectionDic[@"cimv2"]   = cimvScope;
                            connectionDic[@"default"] = defaultScope;
                            if (tsScope != null)
                            {
                                connectionDic[@"TerminalServices"] = tsScope;
                            }
                            if (virtualizScope != null)
                            {
                                connectionDic[@"virtualization"] = virtualizScope;
                            }
                            if (virtualizv2Scope != null)
                            {
                                connectionDic[@"v2"] = virtualizv2Scope;
                            }
                            result = new ConnectionScriptResults(connectionDic,
                                                                 ResultCodes.RC_SUCCESS,
                                                                 0,
                                                                 i);
                            break;
                        } catch (ManagementException me) {
                            if (ManagementStatus.AccessDenied == me.ErrorCode)
                            {
                                Lib.Logger.TraceEvent(TraceEventType.Error,
                                                      0,
                                                      "Task Id {0}: Connect to {1} failed.  Elapsed time {2}.\nWMI Error Code {3}",
                                                      taskIdString,
                                                      mp.ToString(),
                                                      sw.Elapsed.ToString(),
                                                      me.ErrorCode.ToString());
                                resultCode = ResultCodes.RC_LOGIN_FAILED;
                            }
                            else
                            {
                                Lib.LogManagementException(taskIdString,
                                                           sw,
                                                           String.Format("Connect to {0} failed.", mp.ToString()),
                                                           me);
                                //break ;
                            }
                        } catch (UnauthorizedAccessException uae) {
                            Lib.Logger.TraceEvent(TraceEventType.Error,
                                                  0,
                                                  "Task Id {0}: Connect to {1} failed.  Elapsed time {2}.\nMessage: {3}.",
                                                  taskIdString,
                                                  mp.ToString(),
                                                  sw.Elapsed.ToString(),
                                                  uae.Message);
                            resultCode = ResultCodes.RC_LOGIN_FAILED;
                        } catch (COMException ce) {
                            if (0x800706BA == (UInt32)ce.ErrorCode)
                            {
                                Lib.Logger.TraceEvent(TraceEventType.Error,
                                                      0,
                                                      "Task Id {0}: Connect to {1} failed.  Elapsed time {2}.\nCOM Exception {3}.\n" +
                                                      "WMI port (135) may be closed or DCOM may not be properly configured",
                                                      taskIdString,
                                                      mp.ToString(),
                                                      sw.Elapsed.ToString(),
                                                      ce.Message);
                            }
                            else if (0x80040154 == (UInt32)ce.ErrorCode)
                            {
                                Lib.Logger.TraceEvent(TraceEventType.Error,
                                                      0,
                                                      "Task Id {0}: Connect to {1} failed.  Elapsed time {2}.\nCOM Exception {3}.\n" +
                                                      "WMI Management Class not registered. WMI provider not found on the remote machine.",
                                                      taskIdString,
                                                      mp.ToString(),
                                                      sw.Elapsed.ToString(),
                                                      ce.Message);
                            }
                            else
                            {
                                Lib.LogException(taskIdString,
                                                 sw,
                                                 String.Format("Connect to {0} failed", mp.ToString()),
                                                 ce);
                            }
                        } catch (Exception ex) {
                            Lib.LogException(taskIdString,
                                             sw,
                                             String.Format("Connect to {0} failed", mp.ToString()),
                                             ex);
                        }
                    }

                    //
                    // Connect failed after all credentials attempted.
                    if (null == result)
                    {
                        result = new ConnectionScriptResults(null,
                                                             resultCode,
                                                             0,
                                                             connectionParameterSets.Length);
                    }
                } catch (Exception e) {
                    Lib.Logger.TraceEvent(TraceEventType.Error,
                                          0,
                                          "Task Id {0}: Unhandled exception in ConnWincsScript.\n{1}",
                                          taskIdString,
                                          e.ToString());

                    //
                    // This is really an unanticipated fail safe.  We're
                    // going to report that *no* credentials were tried, which
                    // actually may not be true...
                    result = new ConnectionScriptResults(null,
                                                         ResultCodes.RC_PROCESSING_EXCEPTION,
                                                         0,
                                                         -1);
                }
            }

            Debug.Assert(null != result);
            Lib.Logger.TraceEvent(TraceEventType.Stop,
                                  0,
                                  "Task Id {0}: Connection script ConnWincsScript.  Elapsed time {1}.  Result code {2}.",
                                  taskIdString,
                                  executionTimer.Elapsed.ToString(),
                                  result.ResultCode.ToString());
            return(result);
        }