Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell failing to import module

Tags:

powershell

I am working with powershell on a server and want to use it to stop and start a task in the task scheduler. I run this command "Import-Module TaskScheduler" but get an error:

Import-Module : The specified module 'TaskScheduler' was not loaded because no valid module file was found in any module directory.

Any idea of the problem?

like image 977
amateur Avatar asked Jun 14 '13 21:06

amateur


People also ask

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.

How do I manually import a PowerShell module?

To install PowerShell modules manually, you first need to determine your current PowerShell module directory path, download your new module to that path, and invoke the import-module command to let windows know it's there.

How do I permanently import a PowerShell module?

To import PowerShell modules permanently you can copy them creating a folder on C:\Windows\system32\WindowsPowerShell\v1. 0\Modules\ with the name of your script, then inside you can put all your psm1 or ps1 files.

What is AllowClobber in PowerShell?

-AllowClobberOverrides warning messages about installation conflicts about existing commands on a computer. Overwrites existing commands that have the same name as commands being installed by a module. AllowClobber and Force can be used together in an Install-Module command.


2 Answers

The most likely case is that you've got your module installed to a personal location, and not a system location. If you're running it inside of a scheduled task, or have it installed for a particular user (and are running as someone else), then you'll need to make sure that the module is in the "right" location.

   $env:PSModulePath 

Will show the current module paths. There should be at least 2. One will be in your user directory, and the other will be in $pshome\Modules.

If you want to be lazy, you can put a module there. If you want to be thorough, you can create a new directory, change PSModulePath (outside of PowerShell, so it sticks from one PowerShell instance to the next) to include this directory. This is the "official" way.

On a personal note, since you're probably using the very old TaskScheduler module I wrote in the PowerShellPack, I'm sorry that my installer drops them into user directories, and not global directories. While user directories are the common case, global directories should have been an option.

like image 144
Start-Automating Avatar answered Oct 16 '22 16:10

Start-Automating


I had the same issue and my module was in the correct location, and everything was named according to the expected convention. After a bit of frustration I figured out the issue: the window that I was trying to import the module in was launched before I created the module. When I launched a new instance of Powershell, it loaded. So hopefully this might help someone else who is having this same issue and can't figure out why.

You can also add the location of the Powershell modules to the module path:

$env:PSModulePath=$env:PSModulePath + ";" + "F:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules"

Or you can add logic to an existing script:

$module_path = $env:PSModulePath
if (-not($module_path -match "F:\\Program Files (x86)\\Microsoft SQL Server\\110\\Tools\\PowerShell\\Modules")) {
    if (Test-Path("F:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules")) {
        $env:PSModulePath=$env:PSModulePath + ";" + "F:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules"
    }
    else {
        write-host "sqlps not in default location - this can cause errors" -foregroundcolor yellow
    }
}
import-module "sqlps" -DisableNameChecking
like image 33
PlantationGator Avatar answered Oct 16 '22 16:10

PlantationGator