Running the following lines of PowerShell code (on a WIN 2008 R2 server, with administrator rights):
try {
Import-Module webadministration -ErrorAction Stop
}
catch {
"ERROR : $( $Error[ 0 ].ToString() )"
}
Will throw an error onto the console, and will not actually pass it to the catch block:
Process should have elevated status to access iis configuration data
The above error is displayed as is except the color is red on the PowerShell console. To be clear - There is no other text being displayed except for the above Error. The $Error[ 0 ] is not populated as well.
It almost seems like the Error did not generate from the PowerShell script, but from an external process/script.
Is there any way to suppress this error? Even after supplying the ErrorAction preference in the try-catch block, the error still displays.
Additionally, The following will NOT work:
Import-Module webadministration -ErrorAction Stop
Import-Module webadministration | Out-Null
Import-Module webadministration 2>&1 | Out-Null
$null = Import-Module webadministration
Import-Module webadministration -ErrorAction SilentlyContinue
$ErrorActionPreference = 'SilentlyContinue'
Import-Module webadministration
Of note, the Import-Module webadministration
command works after loading the second time in the same PowerShell session! Meaning,
# First Go
Import-Module webadministration
# error thrown on first go
# Second Go
Import-Module webadministraion
# Second go works! It loads the 'webadministration' module