Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly ignore Import-Module errors in PowerShell

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: $_" 
}
like image 405
AndrewB Avatar asked May 08 '12 12:05

AndrewB


People also ask

How do you ignore an error in PowerShell and let it continue?

If there are special commands you want to ignore you can use -erroraction 'silentlycontinue' which will basically ignore all error messages generated by that command. You can also use the Ignore value (in PowerShell 3+): Unlike SilentlyContinue, Ignore does not add the error message to the $Error automatic variable.

How do I hide an error in PowerShell?

If you need to suppress an error, you can use the ErrorAction switch to suppress an error for a single cmdlet or an ErrorAction preference variable to suppress errors globally.

How do I enable the import-Module in PowerShell?

To import the module into all sessions, add an Import-Module command to your PowerShell profile. To manage remote Windows computers that have PowerShell and PowerShell remoting enabled, create a PSSession on the remote computer and then use Get-Module -PSSession to get the PowerShell modules in the PSSession.

What are you doing when you use the import-Module cmdlet?

Description. The Import-Module cmdlet adds one or more modules to the current session. Starting in PowerShell 3.0, installed modules are automatically imported to the session when you use any commands or providers in the module.


1 Answers

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
}
like image 110
David Brabant Avatar answered Oct 04 '22 03:10

David Brabant