2

I have the following code which stops IIS Application pool by executing Powershell script:

        using (PowerShell ps = PowerShell.Create())
        {
            ps.AddScript("Set-ExecutionPolicy -Scope LocalMachine Unrestricted");
            string stopCommand = File.ReadAllText($"{scriptPath}StopIISPool.ps1");                
            ps.AddScript(stopCommand);
            ps.AddScript($"StopIISAppPool -StopScriptPath {scriptPath}IISStopCommand.ps1 -poolName {paths.IISAppPoolName}");
            ps.Invoke();

            Collection<ErrorRecord> errors = ps.Streams.Error.ReadAll();
        }

And here StopIISPool.ps1 contains the following powershell script:

    function StopIISAppPool
    {
        Param(
        [parameter(Mandatory=$true)]
        [string]$stopScriptPath,
        [parameter(Mandatory=$true)]
        [string]$poolName
        )
        process
        {
            PowerShell.exe -NoProfile -ExecutionPolicy Unrestricted -Command "&  {Start-Process PowerShell -windowstyle hidden -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -noexit -File $stopScriptPath -poolName $poolname' -Verb RunAs}";
        }
   }

And here stopScriptPath is the path of the following command which executes without errors only with elevated permissions:

    Param(
        [parameter(Mandatory=$true)]
        [string]$poolName
    )
    Stop-WebAppPool -Name $poolName;

The above C# code runs in ASP.NET Core application. When I deploy the application under IIS on Windows Server 2012 R2 and run I get the following error on ps.Invoke() line:

Start-Process is not recognized as the name of a cmdlet, function, script file, or operable program.

Could someone please explain why I get that error?

6
  • what version of powershell? type $PSVersionTable.PSVersion into powershell window Commented Dec 23, 2017 at 22:16
  • Major 5, Minor 1, Build 16299, Revision 98, This is powershell version of my Windows 10 Pro, here all the above code works just fine. It give's the error when run on Windows server 2012 R2, I guess powershell version there is older, I will write later the version. Commented Dec 23, 2017 at 22:28
  • The PowerShell version is 4.0. When I run the script from PowerShell it works, but when I execute the same script from C#, it gives me the error? Can the problem come from PowerShell version? Commented Dec 25, 2017 at 6:24
  • I have updated my powershell version but nothing changed. Commented Dec 25, 2017 at 8:27
  • in your second script IISStopCommand.ps1 does this just contain something like Stop-WebAppPool -Name "DefaultAppPool" Commented Dec 27, 2017 at 18:00

1 Answer 1

1

I have solved this issue. Actually I have found a workaround. For manipulating IIS Application pools I switched to windows Command Line instead of PowerShell. I have used the following command:

    C:\Windows\System32\inetsrv\appcmd start apppool /apppool.name:$poolName.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.