private static void GetComputeSystem(String CimSessionId, String HostName)
      {
         WSManClass wsman = new WSManClass();
         IWSManConnectionOptions co = wsman.CreateConnectionOptions() as IWSManConnectionOptions;
         co.UserName = CimSessionId;
         co.Password = CimSessionId;

         IWSManSession session = (IWSManSession)
         wsman.CreateSession("https://" + HostName + "/wsman",
                             wsman.SessionFlagUseBasic() |
                             wsman.SessionFlagCredUsernamePassword() |
                             wsman.SessionFlagSkipCACheck() |
                             wsman.SessionFlagSkipCNCheck(),
                             co);
         try
         {
            String url1 = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ComputerSystem";           
            IWSManEnumerator enumerator = (IWSManEnumerator)
            session.Enumerate(url1,
                              null, null, wsman.SessionFlagUseBasic() & wsman.SessionFlagCredUsernamePassword() &
            wsman.SessionFlagSkipCACheck() & wsman.SessionFlagUseNoAuthentication());
            while (!enumerator.AtEndOfStream)
            {
               string response = enumerator.ReadItem();
               Console.Write(response);
            }
         }
         catch (System.Exception ex)
         {
            Console.Write(ex.ToString() + "\n\n" + session.Error);
         }
      }
Exemple #2
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);
                }
            }
Exemple #3
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);
        }
Exemple #4
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);
            }