How to correctly ignore Import-Module errors in PowerShell
Asked Answered
V

1

8

I'm currently having issues whilst calling Import-Module with Powershell and would be grateful for some advice.

According to previous questions and answers here, the following error, when received whilst trying to import a module using PowerShell, can be ignored:

File skipped because it was already present from "Microsoft.PowerShell".

The problem is that it will get caught if the import command is within a try / catch statement.

I've read a number of posts regarding this (example PowerShell on SCOM fails to import module) and one did mention to try adding "-ErrorAction SilentlyContinue" to the Import-Module command, but unfortunately this makes no difference.

Below is the code I'm currently using to test the issue which should give you a better understanding of what I am trying to achieve.

Has anyone managed to successfully ignore these warnings on module import whilst wrapped in a try / catch before?

Thanks for your time,

Andrew

function load_module($name)
{
    if (-not(Get-Module -Name $name))
    {
        if (Get-Module -ListAvailable | Where-Object { $_.name -eq $name })
        {
            Import-Module $name  

            return $true
        }
        else
        {   
            return $false
        }
    }
    else
    {
        return $true
    }
}

$moduleName = "ActiveDirectory"

try 
{
    if (load_module $moduleName)
    {
        Write-Host "Loaded $moduleName"
    }
    else
    {
        Write-Host "Failed to load $moduleName"
    }
}
catch 
{
    Write-Host "Exception caught: $_" 
}
Vickyvico answered 8/5, 2012 at 12:16 Comment(6)
Do you get the error you mention when you load the AD module with your test code, cause I don'tAnatola
Hi Shay, yes, running "Import-Module ActiveDirectory" on its own will give me this error.Vickyvico
Do you get "loaded"? Import-Module ActiveDirectory; Write-Host "loaded"Anatola
I do get "loaded" if I try that, after multiple lines of the above error.Vickyvico
So, the error is not a terminating error which means that the try/catch has no value. Can you ignore the warning with: Import-Module ActiveDirectory -WarningAction SilentlyContinue ?Anatola
That's unfortunately gets handled the same as -ErrorAction SilentlyContinue, in that it will still get picked up in the catch.Vickyvico
C
9
function Load-Module
{
    param (
        [parameter(Mandatory = $true)][string] $name
    )

    $retVal = $true

    if (!(Get-Module -Name $name))
    {
        $retVal = Get-Module -ListAvailable | where { $_.Name -eq $name }

        if ($retVal)
        {
            try
            {
                Import-Module $name -ErrorAction SilentlyContinue
            }

            catch
            {
                $retVal = $false
            }
        }
    }

    return $retVal
}
Culberson answered 8/5, 2012 at 14:12 Comment(1)
Apart from missing the $ on retVal in the catch, this is getting closer. Adding a try / catch around the import module will hide the error and prevent it being caught in the main try / catch. Thanks. We only want it to hide the error if it's the specific "File skipped because it was already present" message though but this can be performed by checking the exception message.Vickyvico

© 2022 - 2024 — McMap. All rights reserved.