0

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
Vish
  • 466
  • 2
  • 12
  • Have you tried silently continue error action preference – Isaac Oct 14 '20 at 10:49
  • Yes. Added to the original question. – Vish Oct 14 '20 at 10:51
  • @Vish you are using the windows server 2008 which is outdated. if possible use the latest os. also provide a detailed error screenshot.make sure you are running the PowerShell as administrator. I tested all the script on win2008 r2 which is running well without any error. you can see the [image](https://i.imgur.com/l57YjyS.png) refer this link https://blogs.iis.net/ma_khan/troubleshooting-iis-powershell-module-on-windows-7-rc – Jalpa Panchal Oct 16 '20 at 06:51
  • Actually, I have provided the error as it is, there is nothing else to provide, a screenshot will show exactly the same error text, but in red. The script is being run using the administrator account. It is not possible to use anything but win2k8r2. – Vish Oct 16 '20 at 11:38
  • @Vish could you please share which update you are using? – Jalpa Panchal Oct 21 '20 at 09:41

0 Answers0