1

I need to catch the follow error that is currently being outputted:

ERROR ( hresult:80070425, message:Command execution failed.) The service cannot accept control messages at this time.

From my PowerShell script snippet, which is not catching the error currently:

Try{
    appcmd start apppool /apppool.name:DefaultAppPool
}Catch{
    #$ErrorMessage = $_.Exception.Message
    #$FailedItem = $_.Exception.ItemName
    Write-Host "AppPool cannot start." -BackgroundColor Red
}

Am I missing something? I would like the error message to be as specific as possible. If it helps, this is IIS8.5. I've referenced these links: 1 | 2 | 3 | 4 | 5 |

Community
  • 1
  • 1
George
  • 6,006
  • 6
  • 48
  • 68
  • Possible duplicate of [Try/catch does not seem to have an effect](https://stackoverflow.com/questions/1142211/try-catch-does-not-seem-to-have-an-effect) – Michael Freidgeim Jul 15 '17 at 13:29

1 Answers1

3

The error message you get probably comes from the appcmd thus is not a exception which you can catch. You may check the $global:LastExitCode to verfiy whether the call was successfull.

However, there is also a WebAdministration module which has a Start-WebAppPool cmdlet:

Import-Module WebAdministration
Start-WebAppPool -Name 'DefaultAppPool'
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • thanks for the explanation. Is this cmdlet able to return a result as appcmd? On appcmd, I'm also attempting to throw some custom errors to see if any works. – George Nov 16 '16 at 08:29
  • I am not familiar with appcmd but the `Start-WebAppPool` also outputs an error message **and** you can use a *try-catch* to catch any exceptions. But whether the message is similar to the one from appcmd I can't tell you. – Martin Brandl Nov 16 '16 at 08:38