Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public WqlConnectionManager Connect()
        {
            System.Diagnostics.StackFrame sf;
            sf = new System.Diagnostics.StackFrame();
            string methodName = sf.GetMethod().Name;
            string userName;
            WqlConnectionManager     connection  = new WqlConnectionManager();
            SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();

            try
            {
                _log.Write(_className, methodName, _serverCode, "Starting the function " + methodName);
                _log.Write(_className, methodName, _serverCode, "WQL connection in progress : " + _serverName + " | Site code : " + _serverCode);
                connection = new WqlConnectionManager(namedValues);
                if (!string.IsNullOrEmpty(_sccmUserDomain) & !string.IsNullOrEmpty(_sccmUser) & !string.IsNullOrEmpty(_sccmUserPassword))
                {
                    userName = _sccmUserDomain + "\\" + _sccmUser;
                    _log.Write(_className, methodName, _serverCode, "Associated user : "******"WQL connection on " + _serverName + " OK");

                return(connection);
            }
            catch (SmsException ex)
            {
                _log.Write(_className, methodName, "Exception", "WARNING : General connection failed");
                _log.Write(_className, methodName, "Exception", ex.Message.ToString());
                connection.Close();
                connection.Dispose();
                throw new Exception("WARNING : " + ex.Message.ToString());
            }
            catch (UnauthorizedAccessException ex)
            {
                _log.Write(_className, methodName, "Exception", "WARNING : Authentication failed");
                _log.Write(_className, methodName, "Exception", ex.Message.ToString());
                connection.Close();
                connection.Dispose();
                throw new Exception("WARNING : " + ex.Message.ToString());
            }
            catch (Exception ex)
            {
                _log.Write(_className, methodName, "Exception", "WARNING : DCOM Connection failed");
                _log.Write(_className, methodName, "Exception", ex.Message.ToString());
                connection.Close();
                connection.Dispose();
                throw new Exception("WARNING : " + ex.Message.ToString());
            }
            finally
            {
                _log.Write(_className, methodName, _serverCode, "End of the function " + methodName);
            }
        }
Exemple #2
0
        public void Execute(IActivityRequest request, IActivityResponse response)
        {
            SCCMServer = settings.SCCMSERVER;
            userName   = settings.UserName;
            password   = settings.Password;

            String objID = request.Inputs["Package ID"].AsString();

            //Setup WQL Connection and WMI Management Scope
            WqlConnectionManager connection = CMInterop.connectSCCMServer(SCCMServer, userName, password);

            try
            {
                String[] propertyNameChoices = CMInterop.getSCCMObjectPropertyNames(connection, "SMS_TaskSequencePackage");
                foreach (String propertyName in propertyNameChoices)
                {
                    if ((request.Inputs.Contains(propertyName + " : Property Type")) && (request.Inputs.Contains(propertyName + " : Property Value")))
                    {
                        CMInterop.modifySCCMTaskSequencePackage(connection, objID, request.Inputs[(propertyName + " : Property Type")].AsString(), propertyName, request.Inputs[(propertyName + " : Property Value")].AsString());
                    }
                }

                IResultObject col = CMInterop.getSCCMTaskSequencePackage(connection, "PackageID LIKE '" + objID + "'");
                if (col != null)
                {
                    response.WithFiltering().PublishRange(getObjects(col));
                }
                response.Publish("Number of Task Sequence Packages", ObjCount);
            }
            finally
            {
                connection.Close();
                connection.Dispose();
            }
        }
Exemple #3
0
        /// <summary>
        ///
        /// </summary>
        public Boolean Disconnect(WqlConnectionManager wqlConnection)
        {
            System.Diagnostics.StackFrame sf;
            sf = new System.Diagnostics.StackFrame();
            string methodName = sf.GetMethod().Name;

            try
            {
                _log.Write(_className, methodName, _serverCode, "Starting the function " + methodName);
                _log.Write(_className, methodName, _serverCode, "Closing WQL connection " + _serverName);
                wqlConnection.Close();
                wqlConnection.Dispose();
                _log.Write(_className, methodName, _serverCode, "WQL Connection Opening " + _serverName + " OK");
                return(true);
            }
            catch (Exception ex)
            {
                _log.Write(_className, methodName, "Exception", "WARNING : Error closing WQL connection");
                _log.Write(_className, methodName, "Exception", ex.Message.ToString());
                throw new Exception("WARNING : " + ex.Message.ToString());
            }
            finally
            {
                _log.Write(_className, methodName, _serverCode, "End of the function " + methodName);
            }
        }
Exemple #4
0
        /// <summary>
        ///     The main entry point for the wrapper. Uncomment the snippets you wish to run here.
        /// </summary>
        public void Run()
        {
            var computer = "";
            var userName = "";
            var password = "";

            Console.WriteLine("Site server you want to connect to (press Return for this computer): ");
            computer = Console.ReadLine();
            Console.WriteLine();

            if (string.IsNullOrEmpty(computer) || computer == ".")
            {
                computer = Dns.GetHostName();
                userName = "";
                password = "";
            }
            else
            {
                Console.WriteLine("Please enter the user name (press Return for current user): ");
                userName = Console.ReadLine();

                if (!string.IsNullOrEmpty(userName))
                {
                    Console.WriteLine("Please enter your password: ");
                    password = ReturnPassword();
                }
            }

            // Make connection to provider.
            WqlConnectionManager wqlConnection = Connect(computer, userName, password);

            if (wqlConnection == null)
            {
                return;
            }

            //
            // Call snippets - uncomment required snippets.
            //

            //
            // Fundamentals
            //
            //IResultObjectExecuteQueryDisposalV1(wqlConnection);
            //IResultObjectExecuteQueryDisposalV2(wqlConnection);
            //IResultObjectDisposalOfReturnValue(wqlConnection);
            //LazyPropertyFromQuery(wqlConnection);

            // Disconnect
            wqlConnection.Close();
            wqlConnection.Dispose();
        }
        private void addCMDeviceVariable(
            int strResourceID,
            string strVariableName,
            string strVariableValue)
        {

            // Connect to CM Site
            SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
            WqlConnectionManager connection = new WqlConnectionManager(namedValues);
            connection.Connect("CM01");

            // Get the computer settings.
            IResultObject computerSettings = null;

            IResultObject computerSettingsQuery = connection.QueryProcessor.ExecuteQuery(
                "Select * from SMS_MachineSettings where ResourceId = '" + strResourceID + "'");

            foreach (IResultObject settings in computerSettingsQuery)
            {
                settings.Get();
                computerSettings = settings;
            }

            if (computerSettings == null) // It does not exist, so create it.
            {
                computerSettings = connection.CreateInstance(@"SMS_MachineSettings");
                computerSettings["ResourceID"].IntegerValue = strResourceID;
                computerSettings["SourceSite"].StringValue = "PS1";
                computerSettings.Put();
                computerSettings.Get();
            }

            // Create the computer variable.
            List<IResultObject> computerVariables = computerSettings.GetArrayItems("MachineVariables");
            IResultObject computerVariable = connection.CreateEmbeddedObjectInstance("SMS_MachineVariable");
            computerVariable["Name"].StringValue = strVariableName;
            computerVariable["Value"].StringValue = strVariableValue;
            computerVariable["IsMasked"].BooleanValue = false;

            // Add the computer variable to the computer settings.
            computerVariables.Add(computerVariable);
            computerSettings.SetArrayItems("MachineVariables", computerVariables);
            computerSettings.Put();

            
            // Close Connection
            connection.Dispose();
        }
        private int getResourceId(string strComputerName)
        {
            // Connect to CM Site
            SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
            WqlConnectionManager connection = new WqlConnectionManager(namedValues);
            connection.Connect("CM01");

            // Get Computer Name
            IResultObject ComputerResourceIDQuery = connection.QueryProcessor.ExecuteQuery(
                "Select ResourceID from SMS_R_System where name = '" + strComputerName + "' AND AgentName = 'Manual Machine Entry'");

            foreach (IResultObject ComputerResourceInfo in ComputerResourceIDQuery)
            {
                ComputerResourceInfo.Get();
                ComputerResourceID = ComputerResourceInfo["ResourceId"].IntegerValue;
            }

            connection.Dispose();

            return ComputerResourceID;

        }
Exemple #7
0
 /// <summary>
 /// Implementing the Dispose Method to fullfill the IDisposable Interface.
 /// This allows the use of the using-Clause
 /// </summary>
 public void Dispose()
 {
     _wqlConnectionManager.Dispose();
 }