Esempio n. 1
0
        /// <summary>
        /// Create Session
        /// </summary>
        /// <param name="cimurl">URL of CIM schema for CIM_Fan</param>
        /// <param name="username">Username</param>
        /// <param name="password">Password</param>
        /// <returns></returns>
        private IWSManSession createWSManConnection(String cimUrl, String username, String password)
        {
            IWSManConnectionOptions co = wsman.CreateConnectionOptions() as IWSManConnectionOptions;

            co.UserName = username;
            co.Password = password;

            IWSManSession session = (IWSManSession)
                                    wsman.CreateSession(cimUrl,
                                                        wsman.SessionFlagUseBasic() |
                                                        wsman.SessionFlagCredUsernamePassword() |
                                                        wsman.SessionFlagSkipCACheck() |
                                                        wsman.SessionFlagSkipCNCheck(),
                                                        co);

            return(session);
        }
Esempio n. 2
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());
            }
        }
Esempio n. 3
0
            /// <summary>
            /// Sets up a CIM connection with the remote XenServer
            /// </summary>
            public void SetupCIMConnection(string server, string user, string pass)
            {
                m_cim_resourceURIBase = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2";
                m_wsman = new WSManClass();

                IWSManConnectionOptions cOptions = (IWSManConnectionOptions)m_wsman.CreateConnectionOptions();

                cOptions.UserName = user;
                cOptions.Password = pass;
                int iFlags = m_wsman.SessionFlagUTF8() |
                             m_wsman.SessionFlagUseBasic() |
                             m_wsman.SessionFlagCredUsernamePassword() |
                             m_wsman.SessionFlagNoEncryption();

                string hostUri = string.Format("http://{0}:5988", server);

                m_wsmanSession = (IWSManSession)m_wsman.CreateSession(hostUri, iFlags, cOptions);
                string identifyResponse = m_wsmanSession.Identify(0);

                if (!identifyResponse.Contains("Citrix"))
                {
                    throw new Exception("Unknown WS-Management Server" + identifyResponse);
                }
            }
Esempio n. 4
0
        public static Boolean Execute(string[] args)
        {
            Boolean Encryption = false;

            String Target   = "";
            String Domain   = "";
            String Username = "";
            String Password = "";
            String Command  = "";

            if (args.Length == 2)
            {
                Target  = args[0];
                Command = args[1];
            }

            else if (args.Length == 5)
            {
                try
                {
                    Target   = args[0];
                    Domain   = args[1];
                    Username = args[2];
                    Password = args[3];
                    Command  = args[4];
                }
                catch (Exception e)
                {
                    Logger.Print(Logger.STATUS.ERROR, e.Message);
                    return(false);
                }
            }
            else
            {
                Help();
                return(false);
            }

            String hostUri    = String.Format("http://{0}:5985", Target);
            String sessionURI = String.Format("http://{0}/wsman", Target);

            WSManClass wsmanClass = new WSManClass();

            IWSManConnectionOptions connectionOptions = (IWSManConnectionOptions)wsmanClass.CreateConnectionOptions();

            if (Domain != "" && Username != "" && Password != "")
            {
                connectionOptions.UserName = Domain + "\\" + Username;
                connectionOptions.Password = Password;
                Logger.Print(Logger.STATUS.INFO, "Authenticating with: " + Domain + "\\" + Username + ":" + Password);
            }
            else if (Domain == "" && Username == "" && Password == "")
            {
                Logger.Print(Logger.STATUS.INFO, "Authenticating as: " + Environment.UserDomainName + "\\" + Environment.UserName);
            }
            else
            {
                Help();
                return(false);
            }

            Logger.Print(Logger.STATUS.INFO, "Command: " + Command);

            int wsmanFlags = wsmanClass.SessionFlagUTF8() | wsmanClass.SessionFlagCredUsernamePassword();

            if (!Encryption)
            {
                // https://stackoverflow.com/questions/1469791/powershell-v2-remoting-how-do-you-enable-unencrypted-traffic
                wsmanClass.SessionFlagNoEncryption();
            }

            try
            {
                // https://docs.microsoft.com/en-us/windows/win32/api/wsmandisp/nn-wsmandisp-iwsmansession
                IWSManSession wsmanSession = (IWSManSession)wsmanClass.CreateSession(hostUri, wsmanFlags, connectionOptions);

                if (wsmanSession != null)
                {
                    // https://docs.microsoft.com/en-us/windows/win32/winrm/windows-remote-management-and-wmi
                    // https://csharp.hotexamples.com/examples/-/IWSManSession/-/php-iwsmansession-class-examples.html

                    XmlDocument xmlIdentifyResponse = new XmlDocument();

                    // https://docs.microsoft.com/en-us/windows/win32/api/wsmandisp/nf-wsmandisp-iwsmansession-identify
                    // Queries a remote computer to determine if it supports the WS-Management protocol.
                    try
                    {
                        xmlIdentifyResponse.LoadXml(wsmanSession.Identify());
                        if (!xmlIdentifyResponse.HasChildNodes)
                        {
                            Logger.Print(Logger.STATUS.INFO, "Failed to Identify() host");
                            Marshal.ReleaseComObject(wsmanSession);
                            return(false);
                        }
                        Logger.Print(Logger.STATUS.GOOD, "Successfully identified host: ");
                        foreach (XmlNode node in xmlIdentifyResponse.ChildNodes)
                        {
                            foreach (XmlNode innernodes in node.ChildNodes)
                            {
                                Logger.Print(Logger.STATUS.GOOD, innernodes.InnerText);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Print(Logger.STATUS.ERROR, "Failed to Identify() host");
                        Logger.Print(Logger.STATUS.ERROR, e.Message);
                        Marshal.ReleaseComObject(wsmanSession);
                        return(false);
                    }

                    string resourceURI = "http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Process";

                    // https://social.microsoft.com/Forums/en-US/d6ec5087-33dc-4967-8183-f8524683a3ea/using-remote-powershellwinrm-within-caspnet
                    StringBuilder parameters = new StringBuilder();
                    parameters.Append("<p:Create_INPUT ");
                    parameters.Append("xmlns:p=\"http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Process\">");
                    parameters.Append("<p:CommandLine>" + Command + "</p:CommandLine>");
                    parameters.Append("</p:Create_INPUT>");

                    Logger.Print(Logger.STATUS.INFO, "Sending the following XML: ");
                    Console.WriteLine("\n" + parameters + "\n");

                    String responseFromInvoke = wsmanSession.Invoke("Create", resourceURI, parameters.ToString(), 0);

                    if (responseFromInvoke != null)
                    {
                        Logger.Print(Logger.STATUS.GOOD, "Got a response from invoke:");
                        Console.WriteLine("\n" + responseFromInvoke + "\n");
                        XmlDocument xmlInvokeResponse = new XmlDocument();
                        try
                        {
                            xmlInvokeResponse.LoadXml(responseFromInvoke);
                            foreach (XmlNode node in xmlInvokeResponse.ChildNodes)
                            {
                                foreach (XmlNode innernodes in node.ChildNodes)
                                {
                                    String NodeName  = innernodes.Name.Replace("p:", "");
                                    String NodeValue = innernodes.InnerText;
                                    Logger.Print(Logger.STATUS.GOOD, NodeName + ": " + NodeValue);
                                    if (NodeName == "ReturnValue")
                                    {
                                        if (NodeValue == "0")
                                        {
                                            Logger.Print(Logger.STATUS.GOOD, "Process Sucessfully Started!");
                                        }
                                        else
                                        {
                                            Logger.Print(Logger.STATUS.ERROR, "Process failed to start, got error: " + NodeValue);
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Print(Logger.STATUS.ERROR, "Got an erorr whilst parsing response: " + e.Message);
                            Marshal.ReleaseComObject(wsmanSession);
                        }
                    }
                    else
                    {
                        Logger.Print(Logger.STATUS.ERROR, "Got no response, not too sure what this means...");
                        Marshal.ReleaseComObject(wsmanSession);
                        return(false);
                    }

                    Marshal.ReleaseComObject(wsmanSession);
                }
                else
                {
                    Logger.Print(Logger.STATUS.ERROR, "Failed to create session with IWSManSession");
                    Marshal.ReleaseComObject(wsmanSession);
                    ErrorMsg();
                }
            }
            finally
            {
                Marshal.ReleaseComObject(connectionOptions);
            }
            return(true);
        }