none
MVC and Windows Powershell RRS feed

  • Question

  • 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.


    Monday, July 9, 2018 8:35 AM

All replies

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


    \_(ツ)_/

    Monday, July 9, 2018 8:51 AM
  • It may not be elevation but is likely that the local system is not configured correctly for remote connections.


    \_(ツ)_/

    Monday, July 9, 2018 8:58 AM
  • https://forums.asp.net/

    it is where you should post.

    • Proposed as answer by jrv Monday, July 9, 2018 9:13 AM
    Monday, July 9, 2018 9:10 AM
  • 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.


    \_(ツ)_/

    Monday, July 9, 2018 9:13 AM
  • 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?
    Monday, July 9, 2018 11:51 AM
  • All the scripts and folders are residing in the same machine which I'm trying to invoke.
    Monday, July 9, 2018 11:56 AM
  • 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.


    \_(ツ)_/

    Monday, July 9, 2018 11:57 AM