Not able to execute the "Import-module" - powershell

I get below error when i try to execute the command "Import-module script.ps1"
any idea why is that shows up? what need to be done to resolve the issue ?
PS C:\File> Import-module script.ps1
Import-module : The specified module 'script.ps1' was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ Import-module script.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (script.ps1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

You need to use .\ to execute scripts, commands from current directory. i.e. ipmo .\script.ps1
As a security feature, PowerShell does not run executable (native)
commands, including PowerShell scripts, unless the command is located
in a path that is listed in the Path environment variable $env:path or
unless you specify the path to the script file.
To run a script that is in the current directory, specify the full
path, or type a dot .\ to represent the current directory.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_command_precedence?view=powershell-7

Related

Activate a virtual environment in Windows Powershell with a command that is equivalent to "source"

I need to activate a virtual environment in Windows Powershell. But, the guide i am using that to proceed my project tells me to use a command like this:
source venv-slither/bin/activate
In my project directory, in first, i installed virtualenv with:
pip3 install virtualenv
After that, I setup a virtual environment in folder ./venv-slither with:
source venv-slither/bin/activate
Problem emerges here. Whenever i run the last command i approaches to this message in Powershell:
source : The term 'source' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:2
+ source venv-slither/bin/activate
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (source:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I read some guide to eliminate this. They suggest using . instead source, but by this way the same error occurs. When, my Command is:
. venv-slither/bin/activate
The message is:
. : The term 'venv-slither/bin/activate' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At line:1 char:3
+ . venv-slither/bin/activate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (venv-slither/bin/activate:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
More over, when i change slashes direct:
. venv-slither\bin\activate
This message comes up:
. : The module 'venv-slither' could not be loaded. For more information, run 'Import-Module venv-slither'.
At line:1 char:3
+ . venv-slither\bin\activate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (venv-slither\bin\activate:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoLoadModule
I have these modules in my project directory: .bin, dot, findit, source.
Please help a beginner man in working with Windows Powershell.
I think you should use like this, within Windows PS:
venv-slither/Scripts/activate.ps1
After enabling the necessary execution policy for the current user with this command:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
I would return to previous policy with this command, after you finish your work, for safety:
Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope CurrentUser
There are other ways to set the execution temporarily. For example, I can set for the current PS's session by issuing this command:
powershell.exe -ExecutionPolicy RemoteSigned
* RemoteSigned or Unrestricted, you can refer to this link to view the details which scripts are or not allowed for each type of policy:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.1
Sorry for posting too late, I've just run into your post when found the same issue, tested myself and it worked.

Powershell Loading Binary Module

I have few powershell script which I am trying to put into module. In the same module I also intend to load a c# dll for token generation. DLL uses System.Management.Automation.
#content of asr.psm1
Import-Module ".\tokengenerator\PowershellTokenGenerator.dll"
Get-ChildItem $psscriptroot\*.ps1 -Recurse | ForEach-Object { . $_.FullName }
The folder tokengenerator includes dll to generate OAuth2.0 token. How can I load powershell module and C# cmdlet under the same module. However, when I am trying to load the module I get the below error.
Import-Module D:\repo\src\aadsr\setup\asr.psm1
Import-Module : The specified module '.\tokengenerator\PowershellTokenGenerator.dll' was not loaded because no valid module file was found in any module directory. At D:\repo\src\aadsr\setup\asr.psm1:1 char:1
+ Import-Module ".\tokengenerator\PowershellTokenGenerator.dll"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (.\tokengenerato...enGenerator.dll:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Use:
Import-Module "$PSScriptRoot\tokengenerator\PowershellTokenGenerator.dll"
to ensure that the DLL is found relative to the script module's (*.psm1) location, reflected in automatic variable $PSScriptRoot.
By contrast, if you use Import-Module ".\...", the DLL is looked for relative to the current location (.), whatever it may be.

Import-Module PoshRSJob with Executionpolicy RemoteSigned

I downloaded the PoshRSJob module. When I try to import the module it always throws the following error (translated from German):
PS C:\WINDOWS\system32> ipmo "C:\Program Files\WindowsPowerShell\Modules\PoshRSJob"
ipmo : The File "C:\Program Files\WindowsPowerShell\Modules\PoshRSJob\Scripts\TabExpansion.ps1" could not be loaded. The File "C:\Program
Files\WindowsPowerShell\Modules\PoshRSJob\Scripts\TabExpansion.ps1" is not signed digitally. You can't execute this script on your system. More information: "about_Execution_Policies" unter
"http://go.microsoft.com/fwlink/?LinkID=135170"..
In Zeile:1 Zeichen:1
+ ipmo "C:\Program Files\WindowsPowerShell\Modules\PoshRSJob"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : Sicherheitsfehler: (:) [Import-Module], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand
Our ExecutionPolicy is RemoteSigned
Is there a way to get this module working in a RemoteSigned environment, without Bypassing the Executionpolicy everytime I use the Module in a Script, and without setting the ExecutionPolicy to Unrestricted?
It might be that Windows is treating the files as remote files despite being on your machine locally. Sometimes Windows will know when files have come from the internet (for example if you download through IE or Edge instead of say, Firefox). Behind the scenes, this is storing zone information in an alternate data stream associated with the file, but that can be modified or stripped off.
Right click the PSD1, PS1, PSM1 files inside the module and choose "Properties" and if there's an unblock option, make sure it's unticked.
Alternatively you could run the cmdlet Unblock-File if you have PowerShell 5.1 or greater.

Set the font type and size using the command prompt (or batch file)

I tried the solution given from: Specify the size of command prompt when executing a batch file
I ran:
powershell -command "&{set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize}"
But I get these errors, any ideas?
Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. At line:1 char:22 + &{set-executionpolicy <<<< remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize} + CategoryInfo : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
Import-Module : The specified module 'SetConsoleFont' was not loaded because no valid module file was found in any module directory . At line:1 char:50 + &{set-executionpolicy remotesigned; Import-Module <<<< SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize} + CategoryInfo : ResourceUnavailable: (SetConsoleFont:String) [Import-Module], FileNotFoundException + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
The term 'Get-ConsoleFontInfo' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spe lling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:86 + &{set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo <<<< | Format-Table -AutoSize} + CategoryInfo : ObjectNotFound: (Get-ConsoleFontInfo:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
I have put the file SetConsoleFont.psm1 in
C:\Users\Adrian\Documents\WindowsPowerShell\Modules\SetConsoleFont
You say "You're not allowed to set the execution policy" well maybe I'm not, but it's my machine so why shouldn't I? I don't want to execute these commands as Administrator, just as a user, me (Adrian)
Another comment was to try set-executionpolicy bypass process
so I tried:
powershell -command "&{set-executionpolicy bypass process; set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize}"
But got even more red errors.
I have no idea what powershell is or how to use it, I just want to change the font from a batch file without hassle!
Try set-executionpolicy bypass process instead.
Also make sure you have put the module in a module path folder such as:
[yourprofile]\Documents\WindowsPowershell\Modules
I managed to get it working but only in a PowerShell console, and I had to run it as Administrator. However this is not practical for me for the following reasons:
I wish to change the font of new window seamlessly from a batch file, which will be run by users of the software. They may not have Administrator access and so cannot execute "set-executionpolicy remotesigned" which I needed to do to get it working.
Also this has to be done in a DOS batch file, so opening up a powershell window is not an option. It only works in a PowerShell window and not with the DOS "powershell -command" option.
So a partial answer.
If you want to change Execution Policy, it should be done in an elevated prompt.
And loading the module can be done by giving absolute path. Example is below.
Import-Module c:\users\testuser\desktop\SetConsoleFont.psm1 -Verbose
and we can bypass execution policy like below.
powershell.exe -executionpolicy bypass -command "${<your code>}"
Edit: The imported module will be available only in the scope of the script block.
here it is with in {}. So whatever cmdlets and functions in side the module should be executed in sided the scriptblock.
Regards,
Kvprasoon

Does #Requires work in module scripts?

Consider the following module script:
MyWebApp.psm1:
#Requires -Version 4
#Requires -Modules WebAdministration
function Test-MyWebApp() {
return ((Get-WebApplication 'myapp') -ne $null)
}
(Export-ModuleMember omitted for simplicity. The script still works as a module without it.)
If this were a ps1 script, the #Requires comments at the top would force PowerShell to throw an error if
The version were lower than 4
The WebAdministration module could not be loaded (imported)
But if I try to import this using Import-Module, do these have any effect? The #Requires documentation just says "scripts", but it doesn't clarify whether script modules count as "scripts" or not here. What can I do instead, if not?
No, it's treated as a normal comment
No, #Requires comments are not processed when a psm1 script is executed by calling Import-Module.
If we save the script in the question as both MyWebApp.psm1 and MyWebApp.ps1 on a machine that lacks the WebAdministration module, we get the following result:
PS> .\MyWebApp.ps1
.\MyWebApp.ps1 : The script 'MyWebApp.ps1' cannot be run because the following modules that are specified by the
"#requires" statements of the script are missing: WebAdministration.
At line:1 char:1
+ .\MyWebApp.ps1
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (MyWebApp.ps1:String) [], ScriptRequiresException
+ FullyQualifiedErrorId : ScriptRequiresMissingModules
PS> Import-Module .\MyWebApp.psm1
PS>
Importing the module succeeds, and the function now exists in the current scope. But the function will fail:
PS> Test-MyWebApp
Get-WebApplication : The term 'Get-WebApplication' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At .\MyWebApp.psm1:5 char:14
+ return ((Get-WebApplication 'myapp') -Eq $null)
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WebApplication:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Even the -Version check is ignored. If we bump it up to 5 on a machine with only PowerShell 4:
PS> .\MyWebApp.ps1
.\MyWebApp.ps1 : The script 'MyWebApp.ps1' cannot be run because it contained a "#requires" statement for Windows
PowerShell 5.0. The version of Windows PowerShell that is required by the script does not match the currently running
version of Windows PowerShell 4.0.
At line:1 char:1
+ .\MyWebApp.ps1
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (MyWebApp.ps1:String) [], ScriptRequiresException
+ FullyQualifiedErrorId : ScriptRequiresUnmatchedPSVersion
PS> Import-Module .\MyWebApp.psm1
PS>
Use a Module Manifest
The only way to get the requirements validated properly is to use a module manifest. Unfortunately, this must be a separate file alongside the psm1 file. The following manifest will achieve what the #Requires comments are intended to do:
MyWebApp.psd1:
#
# Module manifest for module 'MyWebApp'
#
#{
ModuleVersion = '1.0'
PowerShellVersion = '4.0'
RequiredModules = #('WebAdministration')
RootModule = #('.\MyWebApp.psm1')
}
Importing this file gives the error we want:
PS> Import-Module .\MyWebApp.psd1
Import-Module : The required module 'WebAdministration' is not loaded. Load the module or remove the module from 'RequiredModules' in the file
'.\MyWebApp.psd1'.
At line:1 char:1
+ Import-Module .\MyWebApp.psd1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (.\MyWebApp.psd1:String) [Import-Module], MissingMemberException
+ FullyQualifiedErrorId : Modules_InvalidManifest,Microsoft.PowerShell.Commands.ImportModuleCommand
Unfortunately, you cannot declare the function in the same file. You must use a separate psd1 file and then explicitly declare the original psm1 script as the "root module." The root module is indicated as a path relative to the psd1 file.
Other attributes:
The ModuleVersion is required. It must be present.
PowerShellVersion accomplishes what #Requires -Version 4 intends.
RequiredModules accomplishes what #Requires -Modules WebAdministration intends.
Note that Test-MyWebApp is exported implicitly in both the psm1 and the psd1 file. This is normally controlled by Export-ModuleMember -Function in a psm1 file; the equivalent in a module manifest is FunctionsToExport. I find it simpler to just omit FunctionsToExport from the manifest and control what's exported using Export-ModuleMember in the psm1 script.