none
MVC and Windows Powershell

    Pertanyaan

  • I'm using Visual Studio 2015 Enterprise Edition and target framework is 4.5. I'm calling a MVC controller. I have a powershell script which resides in the same server. The dlls I'm using are
    1. System.Management.Automation v4.0 
    2. WindowsBase V4.0

    In the app pool I'm using the local admin and my application is running under the same app pool. I have checked the group policy and updated accordingly.

    When I'm trying to invoke Powershell using Runspace.open() I'm receiving the error.
    Connecting to remote server localhost failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.

    public static string Input(string Server, string Volume, string size, string region, string username, string password)
            {
                
                FileStream fileStream;
                log.Info("\n");
                log.Info("Input- Method called:");
                string _consoleOutput = string.Empty, _output = string.Empty;
                string _outputfilePath = @"c:\diskextension\Output";            
                string _powerShellScript = @"C:\diskextension\diskexterror_dev.ps1";
                
                try
                {
                    // TODO: Add delete logic here
                    string userID = "dir\\" + username.ToString();
                    string userpassword = password.ToString();
                    log.Info("username " + userID);
                    StringBuilder stringBuilder = new StringBuilder();
                    var con = new WSManConnectionInfo();
                    log.Info("Pushing username  in PSCredential- " + userID.ToString().Trim());
                    con.Credential = new PSCredential(userID.ToString().Trim(), userpassword.ToString().Trim().ToSecureString());
                    Runspace runspace = RunspaceFactory.CreateRunspace(con);
                    runspace.Open();
                    Pipeline pipeline = runspace.CreatePipeline();
                    pipeline.Commands.AddScript("Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted");
                    string _str = @"-Server " + Server + " -Volumeletter " + Volume + ": -deltasize " + size + " -Logfile " + _outputfilePath + " -region " + region + " -username " + userID + " -password " + DataEncrypterDecrypter.Decrypt(password, "sblp-3hn8-sqoy19").ToString().Trim();
                    pipeline.Commands.AddScript(_powerShellScript  + _str.ToString());
                    pipeline.Commands.Add("Out-String");
                    var results = pipeline.Invoke();
                    runspace.Close();
                    foreach (PSObject obj in results)
                    {
                        stringBuilder.AppendLine(obj.ToString());
                        stringBuilder.Append("\n");
                    }
                    
                }
    
                catch (Exception ex)
                {
                    throw ex.ToString();
                    
                }
                return stringBuilder.ToString().Trim();
            }
    Can someone let me know any noticeable errors for which I'm receiving this error.


    Senin, 09 Juli 2018 08.35

Semua Balasan

  • "Access is denied".  You cannot connect locally without using an elevated session.


    \_(ツ)_/

    Senin, 09 Juli 2018 08.51
  • It may not be elevation but is likely that the local system is not configured correctly for remote connections.


    \_(ツ)_/

    Senin, 09 Juli 2018 08.58
  • https://forums.asp.net/

    it is where you should post.

    • Disarankan sebagai Jawaban oleh jrv Senin, 09 Juli 2018 09.13
    Senin, 09 Juli 2018 09.10
  • https://forums.asp.net/

    it is where you should post.

    Yes. MVC would be best in ASP.Net forums as IIS has other security restrictions to overcome.


    \_(ツ)_/

    Senin, 09 Juli 2018 09.13
  • I'm using admin privileges and my application is also running under the same.
    I'm also using pipeline.Commands.AddScript("Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted");
    So could you please suggest what all more changes I need to do?
    Senin, 09 Juli 2018 11.51
  • All the scripts and folders are residing in the same machine which I'm trying to invoke.
    Senin, 09 Juli 2018 11.56
  • You need to modify IIS correctly to allow its security to allow this.  I do not recommend running a web site as an admin.

    Post in asp.net forum for assistance with this as noted above.


    \_(ツ)_/

    Senin, 09 Juli 2018 11.57