Powershell cannot import module

I work 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 I get an error message:

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

Any idea of ​​a problem?

+6
source share
2 answers

The most likely case is that your module is installed in a personal location, and not in a system location. If you run it inside a scheduled task or install it for a specific user (and run it like someone else), you need to make sure that the module is in the β€œright” place.

  $env:PSModulePath 

Will show the current paths to the module. Must 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 place the module there. If you want to be thorough, you can create a new directory, change the PSModulePath (outside PowerShell so that it binds from one PowerShell instance to the next) to include this directory. This is the "official" way.

In a personal note, since you are probably using the very old TaskScheduler module that I wrote in PowerShellPack, I'm sorry that my installer put them in user directories, not global directories. Although user directories are commonplace, global directories should have been an option.

+7
source

I had the same problem and my module was in the right place and everything was named according to the expected agreement. After some disappointment, I realized the problem: the window into which I was trying to import the module was launched before I created the module. When I launched a new Powershell instance, it booted. Therefore, I hope this can help someone else who has this problem and cannot understand why.

You can also add the location of 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 
+4
source

Source: https://habr.com/ru/post/947339/


All Articles