Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to correctly ignore Import-Module errors in PowerShell
    primarykey
    data
    text
    <p>I'm currently having issues whilst calling Import-Module with Powershell and would be grateful for some advice.</p> <p>According to previous questions and answers here, the following error, when received whilst trying to import a module using PowerShell, can be ignored:</p> <p><strong>File skipped because it was already present from "Microsoft.PowerShell".</strong></p> <p>The problem is that it will get caught if the import command is within a try / catch statement.</p> <p>I've read a number of posts regarding this (example <a href="https://stackoverflow.com/questions/6072026/powershell-on-scom-fails-to-import-module">PowerShell on SCOM fails to import module</a>) and one did mention to try adding "-ErrorAction SilentlyContinue" to the Import-Module command, but unfortunately this makes no difference.</p> <p>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.</p> <p>Has anyone managed to successfully ignore these warnings on module import whilst wrapped in a try / catch before?</p> <p>Thanks for your time,</p> <p>Andrew</p> <pre><code>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: $_" } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload