0
votes

I have the following function called from a WinForms application. The function is located in the Main form itself. when I run the program (with elevated privileges), the script is called, but the Application Pool does not get created.

Several other methods that call PowerShell scripts run fine, especially those that call custom modules that I built myself. Scripts that I have difficulty with (like the one below) are those that modify items in Windows itself, such as IIS or creating Registry entries.

public void PSSetupIIS(string baseDirectory, IISInfo iisInfo)
{
    var results = new List<string>();
    using (Runspace runspace = RunspaceFactory.CreateRunspace())
    {
        runspace.Open();
        using (PowerShell psInstance = PowerShell.Create())
        {
            psInstance.Runspace = runspace;
            psInstance.AddCommand("Import-Module").AddArgument("Web-Administration");
            psInstance.AddStatement();

            var addWebAppPool = new Command("New-WebAppPool");
            addWebAppPool.Parameters.Add("Name", iisInfo.AppPoolName);
            addWebAppPool.Parameters.Add("Verbose");
            addWebAppPool.MergeMyResults(PipelineResultTypes.All, PipelineResultTypes.Output);
            psInstance.Commands.AddCommand(addWebAppPool);
            psInstance.Commands.AddCommand("Out-String");
            psInstance.AddStatement();

            results.AddRange(psInstance.Invoke<string>());
        }
        foreach (var result in results)
        {
            this.AppendOutput(result);
        }
        runspace.Close();
    }
    this.AppendOutput("IIS Configuration Complete.");
}

I have attempted writing a script and using psInstance.AddScript and get the same results (though when running that same script in an elevated PowerShell instance it works fine). I have also attempted to have the runspace prompt for credentials, but that did not work at all.

This method is part of a much larger project to use PowerShell to install our company's web-application. It is a complex installation with multiple configuration files, and had long grown beyond the ability to maintain simply with Wix or InstallShield. I use the WinForms project to create a PSD1 data source that is consumed by all of the necessary scripts.

I have a working PowerShell script that completes the installation nicely when run from the PowerShell console consuming the generated PSD1 file. The attempt to run from the UI is just to smooth out the installer's user experience.

I'm using PowerShell and C# to try and keep with technologies that are accessible to both our development team and those who will be using the project, as well as try to follow the general example set by Microsoft. I understand that Server Manager uses PowerShell under the hood.

Any guidance?

1
Though you found the answer, did you check Microsoft.Web.Administration API, which is more suitable for a C# program to consume?Lex Li
No, I did not. I will look into it. Thanks.Kerry

1 Answers

0
votes

The answer turned out to be that I needed to create a manifest for the winforms application. The first key was in this question on Microsoft Developer Network, with additional follow up in the Manifest Generation in Visual Studio page for additional guidance.