It's hard to know what is the real problem here, without more information. Perhaps you're not running your application as an administrator and therefore aren't allowed to add windows features?
However, PowerShell differs between terminating errors (which would block the execution and throw an exception, which should make your code enter the catch statement) and non-terminating errors (which are just written to the error stream and will not enter your catch statement).
You can read more about this if you run Get-Help Write-Error
, Get-Help about_Throw
and Get-Help about_Try_Catch_Finally
.
I'm guessing your powershell command results in a non-terminating error. To find out whether a non terminating error has occured or not, you could check the powershell.HadErrors
property and to get the error messages you can read the powershell.Streams.Error
property.
This should probably help you in finding out what errors are occuring and hopefully help you solve your problem.