amateur
amateur

Reputation: 44663

powershell failing to import module

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?

Upvotes: 9

Views: 41819

Answers (2)

PlantationGator
PlantationGator

Reputation: 885

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

Upvotes: 6

Start-Automating
Start-Automating

Reputation: 8377

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.

Upvotes: 8

Related Questions