예제 #1
0
        //*********************************************************************
        ///
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <param name="directory"></param>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <returns></returns>
        ///
        //*********************************************************************

        Runspace GetOpenRunspace_old(string url, string userName, string userPassword)
        {
            var RR = new RemotingResult();

            try
            {
                if (null != _Runspace)
                {
                    if (_Runspace.RunspaceStateInfo.State == RunspaceState.Opened)
                    {
                        return(_Runspace);
                    }
                }

                var connectTo = new Uri(String.Format("{0}/wsman", url));

                //*** http://social.msdn.microsoft.com/Forums/vstudio/en-US/a0e5b23c-b605-431d-a32f-942d7c5fd843/wsmanconnectioninfo

                var ClearPassword = new char[userPassword.Length];
                var SecString     = new System.Security.SecureString();

                foreach (var c in userPassword)
                {
                    SecString.AppendChar(c);
                }

                var PsCred = new PSCredential(userName, SecString);

                var ConnectionInfo = new WSManConnectionInfo(connectTo,
                                                             "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", PsCred);
                ConnectionInfo.SkipCACheck         = true;
                ConnectionInfo.SkipCNCheck         = true;
                ConnectionInfo.SkipRevocationCheck = true;

                // http://blogs.msdn.com/b/powershell/archive/2006/04/25/583250.aspx
                //*** Can I use this? Why should I? ***

                /*using (RunspaceInvoke invoker = new RunspaceInvoke())
                 * {
                 *  invoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope Process");
                 * }*/

                _Runspace = RunspaceFactory.CreateRunspace(ConnectionInfo);
                _Runspace.Open();

                return(_Runspace);
            }
            catch (System.Management.Automation.Remoting.PSRemotingTransportException ex)
            {
                var Message = ex.Message;
                throw new FailToConnectException();
            }
            catch (Exception ex)
            {
                throw new Exception("Exception in Remoting.GetOpenRunspace() : " + ex.Message);
            }
        }
예제 #2
0
        //*********************************************************************
        ///
        /// <summary>
        ///
        /// </summary>
        /// <param name="commandList"></param>
        /// <returns></returns>
        ///
        //*********************************************************************

        public static RemotingResult Execute(List <string> commandList)
        {
            var RR = new RemotingResult();

            RR.Output = null;

            try
            {
                using (var powershell = PowerShell.Create())
                {
                    foreach (var Command in commandList)
                    {
                        if (null == Command)
                        {
                            continue;
                        }

                        if (0 == Command.Length)
                        {
                            continue;
                        }

                        powershell.AddScript(Command);
                    }

                    RR.Output = powershell.Invoke();

                    foreach (var ER in powershell.Streams.Error)
                    {
                        if (null == RR.ErrorList)
                        {
                            RR.HasErrors            = true;
                            RR.ErrorList            = new Collection <ErrorRecord>();
                            RR.ErrorDecsriptionList = new List <string>();
                        }

                        RR.ErrorList.Add(ER);

                        if (null == ER.Exception)
                        {
                            RR.ErrorDecsriptionList.Add("Unspecified Error");
                        }
                        else
                        {
                            RR.ErrorDecsriptionList.Add(ER.Exception.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception in PowershellLib.Local.Execute() : " + ex.Message);
            }

            return(RR);
        }
예제 #3
0
        //*********************************************************************
        //
        /// <summary>
        ///
        /// </summary>
        /// <param name="hostName"></param>
        /// <param name="directory"></param>
        /// <param name="commandList"></param>
        /// <returns></returns>
        ///
        // http://msdn.microsoft.com/en-us/library/windows/desktop/system.management.automation.runspaces.wsmanconnectioninfo_members(v=vs.85).aspx
        //
        //*********************************************************************

        public RemotingResult Execute(string directory, List <string> commandList)
        {
            var rr = new RemotingResult {
                Output = null
            };

            try
            {
                ServicePointManager.ServerCertificateValidationCallback +=
                    OnServerCertificateValidationCallback;

                using (var powershell = PowerShell.Create())
                {
                    powershell.Runspace = _Runspace;

                    if (null != directory)
                    {
                        if (0 < directory.Length)
                        {
                            powershell.AddScript("cd " + directory);
                        }
                    }

                    foreach (var command in commandList)
                    {
                        if (null == command)
                        {
                            continue;
                        }

                        if (0 == command.Length)
                        {
                            continue;
                        }

                        powershell.AddScript(command);
                    }

                    rr.Output = powershell.Invoke();

                    if (null != rr)
                    {
                        if (null != rr.Output)
                        {
                            rr.StringOutput = new List <string>();

                            foreach (var outObj in rr.Output)
                            {
                                if (null != outObj)
                                {
                                    rr.StringOutput.Add(outObj.ToString());
                                }
                            }
                        }
                    }

                    foreach (var er in powershell.Streams.Error)
                    {
                        if (null == rr.ErrorList)
                        {
                            rr.HasErrors            = true;
                            rr.ErrorList            = new Collection <ErrorRecord>();
                            rr.ErrorDecsriptionList = new List <string>();
                        }

                        rr.ErrorList.Add(er);

                        if (null == er.Exception)
                        {
                            rr.ErrorDecsriptionList.Add("Unspecified Error");
                        }
                        else
                        {
                            rr.ErrorDecsriptionList.Add(er.Exception.Message);
                        }
                    }

                    //_Runspace.Close();
                }
            }

            /*catch (System.Management.Automation.Remoting.PSRemotingTransportException)
             * {
             *  RR.ConnectionFailed = true;
             * }*/
            catch (Exception ex)
            {
                throw new Exception("Exception in Remoting.Execute() : " + ex.Message);
            }
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback -=
                    OnServerCertificateValidationCallback;
            }

            return(rr);
        }
예제 #4
0
        //*********************************************************************
        ///
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <returns></returns>
        ///
        /// http://technet.microsoft.com/en-us/library/dd347642.aspx
        /// http://stackoverflow.com/questions/6587426/powershell-remoting-with-ip-address-as-target
        /// http://social.msdn.microsoft.com/Forums/vstudio/en-US/a0e5b23c-b605-431d-a32f-942d7c5fd843/wsmanconnectioninfo
        ///
        //*********************************************************************

        Runspace GetOpenRunspace(string url, string userName, string userPassword)
        {
            var rr          = new RemotingResult();
            var impersonate = false;

            try
            {
                if (null != _Runspace)
                {
                    if (_Runspace.RunspaceStateInfo.State == RunspaceState.Opened)
                    {
                        return(_Runspace);
                    }
                }

                var connectTo = new Uri(String.Format("{0}/wsman", url));
                WSManConnectionInfo connectionInfo = null;

                //if (url.ToLower().Contains("https:"))
                if (url.ToLower().Contains("https:") | url.ToLower().Contains("http:"))
                {
                    if (url.ToLower().Contains("http:"))
                    {
                        AddHostToLocalTrusedHosts(url);
                    }

                    var secString = new System.Security.SecureString();

                    foreach (var c in userPassword)
                    {
                        secString.AppendChar(c);
                    }

                    var psCred = new PSCredential(userName, secString);

                    connectionInfo = new WSManConnectionInfo(connectTo,
                                                             "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", psCred);
                }
                else
                {
                    connectionInfo = new WSManConnectionInfo(connectTo);

                    if (null != userName)
                    {
                        impersonate = true;
                    }
                }

                connectionInfo.SkipCACheck         = true;
                connectionInfo.SkipCNCheck         = true;
                connectionInfo.SkipRevocationCheck = true;

                // http://blogs.msdn.com/b/powershell/archive/2006/04/25/583250.aspx
                //*** Can I use this? Why should I? ***

                /*using (RunspaceInvoke invoker = new RunspaceInvoke())
                 * {
                 *  invoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope Process");
                 * }*/

                /*if (impersonate)
                 * {
                 *  using (new CmpServiceLib.Impersonator(userName, "ImpersonateDomain", userPassword))
                 *  {
                 *      _Runspace = RunspaceFactory.CreateRunspace(connectionInfo);
                 *      _Runspace.Open();
                 *  }
                 * }
                 * else
                 * {*/
                _Runspace = RunspaceFactory.CreateRunspace(connectionInfo);
                _Runspace.Open();
                //}

                return(_Runspace);
            }
            catch (System.Management.Automation.Remoting.PSRemotingTransportException ex)
            {
                var message = ex.Message;
                throw new FailToConnectException(UnwindExceptionMessages(ex));
            }
            catch (Exception ex)
            {
                throw new Exception("Exception in Remoting.GetOpenRunspace() : " + ex.Message);
            }
        }