Пример #1
0
        private void addIPPort()
        {
            var conn = new ConnectionOptions
            {
                EnablePrivileges = true,
                Impersonation    = ImpersonationLevel.Impersonate
            };

            var mPath = new ManagementPath("Win32_TCPIPPrinterPort");

            var mScope = new ManagementScope(@"\\.\root\cimv2", conn)
            {
                Options =
                {
                    EnablePrivileges = true,
                    Impersonation    = ImpersonationLevel.Impersonate
                }
            };

            var mPort = new ManagementClass(mScope, mPath, null).CreateInstance();

            var remotePort = 9100;

            try
            {
                if (IP != null && IP.Contains(":"))
                {
                    var arIP = IP.Split(':');
                    if (arIP.Length == 2)
                    {
                        remotePort = int.Parse(arIP[1]);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(LogName, "Could not parse port from IP");
                Log.Error(LogName, ex);
            }

            mPort.SetPropertyValue("Name", Port);
            mPort.SetPropertyValue("Protocol", 1);
            mPort.SetPropertyValue("HostAddress", IP);
            mPort.SetPropertyValue("PortNumber", remotePort);
            mPort.SetPropertyValue("SNMPEnabled", false);

            var put = new PutOptions
            {
                UseAmendedQualifiers = true,
                Type = PutType.UpdateOrCreate
            };

            mPort.Put(put);
        }
Пример #2
0
        public static IVirtualSystemSettingData CreateVirtualSystemSettingData(IComputerSystem host)
        {
            ManagementObject instance = new ManagementClass(((IWMICommon)host).Scope, new ManagementPath("Msvm_VirtualSystemSettingData"), (ObjectGetOptions)null).CreateInstance();

            instance.SetPropertyValue("BootOrder", (object)new ushort[4]
            {
                (ushort)2,
                (ushort)1,
                (ushort)0,
                (ushort)3
            });
            return((IVirtualSystemSettingData) new VirtualSystemSettingData(instance));
        }
Пример #3
0
    public static int Mainx(string[] args)
    {
        observer = new ManagementOperationObserver();
        MyHandler handler = new MyHandler();

        observer.ObjectPut += new ObjectPutEventHandler(handler.Done);
        newClass            = new ManagementClass(
            "root/default",
            String.Empty,
            null);
        newClass.SetPropertyValue("__Class", "Classsc99");
        newClass.Put(observer);
        ii = 1;
        // For the purpose of this sample, we keep the main
        // thread alive until the asynchronous operation is finished.
        while (!handler.IsComplete)
        {
            System.Threading.Thread.Sleep(500);
//            MessageBox.Show("hi");
        }
        Console.WriteLine("Path = " + handler.ReturnedArgs.Path.Path.ToString());
        return(0);
    }
Пример #4
0
        public Boolean install(long maxWait)
        {
            long waited = 0L;
            int  port   = 9100;

            if (isComplete())
            {
                if (maxWait > 0)
                {
                    //if ( strFile.Length == 0 || File.Exists(strFile))
                    //{
                    if (strIP != null && strIP.Length > 0)
                    {
                        if (strIP.Contains(":"))
                        {
                            String[] arIP = strIP.Split(new char[] { ':' });
                            if (arIP.Length == 2)
                            {
                                strIP = arIP[0];
                                try
                                {
                                    port = Int32.Parse(arIP[1]);
                                }
                                catch
                                {
                                }
                            }
                        }

                        ConnectionOptions conn = new ConnectionOptions();
                        conn.EnablePrivileges = true;
                        conn.Impersonation    = ImpersonationLevel.Impersonate;

                        ManagementPath mPath = new ManagementPath("Win32_TCPIPPrinterPort");

                        ManagementScope mScope = new ManagementScope(@"\\.\root\cimv2", conn);
                        mScope.Options.EnablePrivileges = true;
                        mScope.Options.Impersonation    = ImpersonationLevel.Impersonate;

                        ManagementObject mPort = new ManagementClass(mScope, mPath, null).CreateInstance();

                        mPort.SetPropertyValue("Name", "IP_" + strIP);
                        mPort.SetPropertyValue("Protocol", 1);
                        mPort.SetPropertyValue("HostAddress", strIP);
                        mPort.SetPropertyValue("PortNumber", port);
                        mPort.SetPropertyValue("SNMPEnabled", false);

                        PutOptions put = new PutOptions();
                        put.UseAmendedQualifiers = true;
                        put.Type = PutType.UpdateOrCreate;
                        mPort.Put(put);
                    }

                    Process proc;
                    Boolean blIsIPP = false;

                    if (strPort.Trim().ToLower().StartsWith("ipp://"))
                    {
                        // iprint installation
                        proc = new Process();
                        proc.StartInfo.FileName       = @"c:\windows\system32\iprntcmd.exe";
                        proc.StartInfo.Arguments      = " -a no-gui \"" + strPort + "\"";
                        proc.StartInfo.CreateNoWindow = true;
                        proc.StartInfo.WindowStyle    = ProcessWindowStyle.Hidden;
                        proc.Start();
                        blIsIPP = true;
                    }
                    else if (strAlias.Trim().ToLower().StartsWith("\\\\"))
                    {
                        // Add per machine printer connection
                        proc = Process.Start("rundll32.exe", " printui.dll,PrintUIEntry /ga /n " + strAlias);
                        proc.WaitForExit(120000);
                        // Add printer network connection, download the drivers from the print server
                        proc = Process.Start("rundll32.exe", " printui.dll,PrintUIEntry /in /n " + strAlias);
                        proc.WaitForExit(120000);
                        bounceSpooler();
                    }
                    else
                    {
                        // Normal Installation
                        proc = Process.Start("rundll32.exe", getInstallArguments());
                    }

                    while (!proc.HasExited)
                    {
                        sleep(100);
                        waited += 100;
                        if (waited > maxWait)
                        {
                            proc.Kill();
                            strError = "Max install time exceeded (" + maxWait + ")";
                            return(false);
                        }
                    }

                    if (blIsIPP)
                    {
                        strError = "IPP Return codes unknown";
                        return(true);
                    }

                    if (proc.ExitCode == 0)
                    {
                        strError = "";
                        return(true);
                    }
                    //}
                    //else
                    //strError = "Printer Definition file was not found!";
                    //strError = "Strfile:" + strFile + ".";
                }
                else
                {
                    strError = "Max wait time was less than zero!";
                }
            }
            else
            {
                strError = "Printer information is incomplete";
            }
            return(false);
        }
Пример #5
0
        public override void Add()
        {
            Log.Entry(LogName, "Attempting to add printer:");
            Log.Entry(LogName, string.Format("--> Name = {0}", Name));
            Log.Entry(LogName, string.Format("--> IP = {0}", IP));
            Log.Entry(LogName, string.Format("--> Port = {0}", Port));

            if (string.IsNullOrEmpty(IP) || !Name.StartsWith("\\\\"))
            {
                return;
            }

            if (IP.Contains(":"))
            {
                var arIP = IP.Split(':');
                if (arIP.Length == 2)
                {
                    IP   = arIP[0];
                    Port = arIP[1];
                }
            }

            var conn = new ConnectionOptions
            {
                EnablePrivileges = true,
                Impersonation    = ImpersonationLevel.Impersonate
            };

            var mPath = new ManagementPath("Win32_TCPIPPrinterPort");

            var mScope = new ManagementScope(@"\\.\root\cimv2", conn)
            {
                Options =
                {
                    EnablePrivileges = true,
                    Impersonation    = ImpersonationLevel.Impersonate
                }
            };

            var mPort = new ManagementClass(mScope, mPath, null).CreateInstance();

            if (mPort != null)
            {
                mPort.SetPropertyValue("Name", "IP_" + IP);
                mPort.SetPropertyValue("Protocol", 1);
                mPort.SetPropertyValue("HostAddress", IP);
                mPort.SetPropertyValue("PortNumber", Port);
                mPort.SetPropertyValue("SNMPEnabled", false);

                var put = new PutOptions
                {
                    UseAmendedQualifiers = true,
                    Type = PutType.UpdateOrCreate
                };
                mPort.Put(put);
            }

            if (!Name.StartsWith("\\\\"))
            {
                return;
            }

            // Add per machine printer connection
            var proc = Process.Start("rundll32.exe", " printui.dll,PrintUIEntry /ga /n " + Name);

            if (proc != null)
            {
                proc.WaitForExit(120000);
            }
            // Add printer network connection, download the drivers from the print server
            proc = Process.Start("rundll32.exe", " printui.dll,PrintUIEntry /in /n " + Name);
            if (proc != null)
            {
                proc.WaitForExit(120000);
            }
        }
Пример #6
0
        /// <summary>
        /// 设置组拥有的用户
        /// </summary>
        /// <param name="groupName"></param>
        /// <param name="userNames"></param>
        /// <param name="operTypes"></param>
        /// <returns></returns>
        public static bool SetGroupMembers(string groupName, string userNames, string operTypes)
        {
            try
            {
                List <string> listMembersName           = GetGroupMembersName(groupName);
                List <ManagementBaseObject> listMembers = new List <ManagementBaseObject>();

                string[] names = userNames.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                if (operTypes == "添加成员")
                {
                    foreach (string name in names)
                    {
                        if (!listMembersName.Contains(name))
                        {
                            listMembersName.Add(name);
                        }
                    }
                }
                else if (operTypes == "删除成员")
                {
                    foreach (string name in names)
                    {
                        listMembersName.Remove(name);
                    }
                }

                foreach (string name in listMembersName)
                {
                    ManagementObject account;
                    try
                    {
                        // 先判断是否是组
                        account = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null).CreateInstance();
                        GetGroupMembersName(name);
                    }
                    catch
                    {
                        // 如果不是组就判断是用户
                        account = new ManagementClass("root\\VisualSVN", "VisualSVN_User", null).CreateInstance();
                    }
                    if (account != null)
                    {
                        account.SetPropertyValue("Name", name);
                        listMembers.Add(account as ManagementBaseObject);
                    }
                }
                var group = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null);
                ManagementObject instance = group.CreateInstance();
                if (instance != null)
                {
                    instance.SetPropertyValue("Name", groupName.Trim());

                    ManagementBaseObject @params = instance.GetMethodParameters("SetMembers");

                    @params["Members"] = listMembers.ToArray();

                    instance.InvokeMethod("SetMembers", @params, null);
                }
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }