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: $_" 
}
6
  • Do you get the error you mention when you load the AD module with your test code, cause I don't Commented May 8, 2012 at 12:33
  • Hi Shay, yes, running "Import-Module ActiveDirectory" on its own will give me this error. Commented May 8, 2012 at 12:37
  • Do you get "loaded"? Import-Module ActiveDirectory; Write-Host "loaded" Commented May 8, 2012 at 12:43
  • I do get "loaded" if I try that, after multiple lines of the above error. Commented May 8, 2012 at 12:51
  • 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 ? Commented May 8, 2012 at 12:53

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.