Exemplo n.º 1
0
        static void ExecRemoteCmd(string host, string cmd, string user, string password)
        {
            try
            {
                //Setup session & execute remote command
                IWSManEx wsman = new WSMan();
                IWSManConnectionOptions options = (IWSManConnectionOptions)wsman.CreateConnectionOptions();
                string sessionUrl = Globals.protocol + "://" + host + ":" + Globals.port + "/wsman";

                IWSManSession session = (IWSManSession)wsman.CreateSession(sessionUrl, 0, options);
                if ((user.Length > 0) && (password.Length > 0))
                {
                    options.UserName = user;
                    options.Password = password;
                    session          = (IWSManSession)wsman.CreateSession(sessionUrl, wsman.SessionFlagCredUsernamePassword(), options);
                }

                string resource   = "http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Process";
                string parameters = "<p:Create_INPUT xmlns:p=\"http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Process\"><p:CommandLine>" + cmd + "</p:CommandLine></p:Create_INPUT>";
                string response   = session.Invoke("Create", resource, parameters);

                //(Poorly) Parse XML response and print values
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(response);
                Console.WriteLine("xsi         : " + xml.DocumentElement.GetAttribute("xmlns:xsi"));
                Console.WriteLine("p           : " + xml.DocumentElement.GetAttribute("xmlns:p"));
                Console.WriteLine("cim         : " + xml.DocumentElement.GetAttribute("xmlns:cim"));
                Console.WriteLine("lang        : " + xml.DocumentElement.GetAttribute("xml:lang"));
                Console.WriteLine("ProcessId   : " + xml.DocumentElement.ChildNodes[0].InnerText);
                Console.WriteLine("ReturnValue : " + xml.DocumentElement.ChildNodes[1].InnerText);

                //Cleanup COM Object
                Marshal.ReleaseComObject(session);
                Marshal.ReleaseComObject(options);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }
        }
Exemplo n.º 2
0
        private static void Main(string[] args)
        {
            string user = "******", password = "******";
            var schema = "http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_SystemInteger?__cimnamespace=root/dcim+InstanceID=System.Embedded.1#ServerTopology.1#RackSlot";


            var wsman = new WSMan();
            var wsManFlags = wsman.SessionFlagCredUsernamePassword() | wsman.SessionFlagSkipCNCheck() |
                             wsman.SessionFlagSkipCACheck() | wsman.SessionFlagUseBasic() | wsman.SessionFlagUTF8();
            var wsManOpt = (IWSManConnectionOptions) wsman.CreateConnectionOptions();


            var wsManEndpoint = "https://co-web03-drac/wsman";
            wsManOpt.UserName = user;
            wsManOpt.Password = password;

            var wsManSession = (IWSManSession) wsman.CreateSession(wsManEndpoint, wsManFlags, wsManOpt);
            wsManSession.Timeout = 60000;
            try
            {
                if ((wsManSession.Identify()) == null)
                {
                    WriteLine("Not a WSMan Endpoint");
                    Environment.Exit(1);
                }

            }
            catch (UnauthorizedAccessException e)
            {
                WriteLine($"[Identify] : Not authroized.");
                //Console.WriteLine($"{ip}: {e.Message}\n{e.StackTrace}");
                Environment.Exit(1);
            }
            catch (COMException e)
            {
                WriteLine($"[Identify] : COM Exception.");
                //Console.WriteLine($"{ip}: {e.Message}\n{e.StackTrace}");
                Environment.Exit(1);
            }
            catch (Exception e)
            {
                if (e.Message.Contains("could not connect"))
                {
                    WriteLine($"[Identify] : Count not connect.");
                    Environment.Exit(1);
                }
                WriteLine($"[Identify] : Error ({e.GetType()}):{e.Message}\n{e.StackTrace}");
                Environment.Exit(1);
            }

            try
            {
                var wsInstance = wsManSession.Get(schema);
                WriteLine(wsInstance);
                var s = new XmlSerializer(typeof(DCIM_SystemString));
                DCIM_SystemString obj = (DCIM_SystemString)s.Deserialize(new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(wsInstance))));
                WriteLine(obj.CurrentValue);
                ReadKey();

            }
            catch (Exception e)
            {
                WriteLine($"[Query] : Error ({e.GetType()}:{e.Message}\n{e.StackTrace}");
                ReadKey();
            }
            
            /*
            while (!wsInstance.AtEndOfStream())
            {
                var curItem = wsInstance.ReadItem();

                using (var r = XmlReader.Create(new StringReader(curItem.ToString())))
                {
                    var xd = XDocument.Load(r);
                    WriteLine(xd.Document);

                }
            }
            */
            

            Environment.Exit(0);
        }
Exemplo n.º 3
0
 static void Main(string[] args)
 {
     WSMan.Execute(args);
 }