Can't load powershell module from explicit path - powershell

I can't recall if this stopped working at some point or has always been this way. When I develop powershell modules I would like to be able to load them explicitly from my local repo directory to make sure everything is working properly. MSDN indicates this should work, however I get the error:
"The specified module .\SomeModule was not loaded because no valid module file was found in any
module directory. FileNotFoundException"
I would expect this is because it can't find the psm1 file in the directory but I can't understand why. I can load the module by referencing the psm1 file directly but this excludes anything being loaded by the manifest. I can also copy the module to one of the standard module paths and it will load correctly. This is what I've been doing as a work around but I'd love to get this figured out. TIA
**Update: Ran process monitor while running the import command. Interesting results. Seems like it may just be a powershell bug.

You can load the module by referencing the module manifest file (.psd1) directly, which will then ensure any dependencies are loaded.

Related

Using Microsoft.Data.SqlClient in PowerShell

I would like to use the Microsoft.Data.SqlClient namespace/ objects in a PowerShell script.
Two (click1, click2) Github posts provide a way to load the correct dll's, but the solutions don't seem to work anymore.
E.g. result of the first solution while copying the next files:
(Packages copied from .nuget/packages folder)
Microsoft.Data.SqlClient.dll
Microsoft.Data.SqlClient.SNI.x64.dll
Microsoft.Identity.Client.dll
Result: Could not load file or assembly 'System.Runtime, Version=6.0.0.0'
In addition, I've tried to create a Dummy Console App -> Added the Microsoft.Data.SqlClient Nuget package -> built the project and copied all dll's to the same folder as the PS script.
As soon as I start the script (using the 'Add-Type -Path' construction), it results in errors, such as 'couldn't load file or assembly - wrong version...' (this is strange, because the folder contains all dll's...)
Could you provide an alternative solution/ steps in order to use the described package in a PS script?

Is it possible to set Powershell module name/version without using a manifest?

I can see that without a manifest the default module name is set to the .psm1 filename.
But is there a way to set it within the module's code?
Also I would like to set the version in code.
If you are importing a binary module then the module version is pulled from the assembly metadata. However the name is simply the DLL's filename. For a script module (.psm1), you can't provide this info to PowerShell other than via a module manifest as Ansgar has already pointed out.
BTW you can get at the Name and Version properties of the module from within the module ($MyInvocation.MyCommand.ScriptBlock.Module.Name) but those properties are read-only.

Is there a way to determine the directory the file being executed resides in?

And if so, is there way that asdf can import a symbol that is calculated in runtime.
I'm trying to to specify the directory on which the project resides so the test runner can find the input files and also when I run from the repl.
Yes, system-relative-pathname and system-source-directory are your friends. At least if you execute from source.
It depends on what you mean by "execution".
If your code is executed when your file is loaded, take a look at Variable *LOAD-PATHNAME*, *LOAD-TRUENAME*.
If you need the current working directory, asdf has getcwd and chdir.

PowerShell module changes not visible

I'm confused as to how PowerShell modules work.
I have downloaded and copied a module from a blogger. I've unblocked and extracted the .zip to %USERPROFILE%\Documents\WindowsPowerShell\Modules\SomeModule
In this folder is a .NET assembly that the module uses, but doesn't not contain compiled CmdLets. Instead, the commands are functions in a .psm1 file and a .psd1 file describes the manifest.
If I open PowerShell, the functions are available and work but I want to add my own function, so I have added it, however I cannot see it. I've restarted all PowerShell instances, removed the module and imported it again.
As a test, I renamed an existing, working function. Interestingly, after remove and import the function disappears instead of adopting its new name. If I rename it back (just a single character change) and remove/import then it appears again.
I use help blahblah to list all commands in a set, since they all have the same prefix. The manifest exports all (*) functions. Clearly I don't understand how these type of script modules work, the functions are all listed even after I run Remove-Module! I've written a compiled module before in C# and that worked as expected.
What's going on? Why does renaming a function cause it to vanish? Thanks.
Found it. This line appears in some stuff I overlooked in the .psm1 file.
Export-ModuleMember X, Y, Z
So, I guess the manifest can overrule this or replace the need for it in a script? Who knows. Anyway, hope this helps someone.

Perl error trying to load Bugzilla

My knowledge of Perl is nil but I was trying to install Bugzilla and everything seem to be going along find until I went to load Bugzilla and I got this error followed by the html of the page trying to be loaded.
dmake could not be found on the PATH. Please invoke it using the full pathname:
C:\Perl64\site\bin\dmake.exe
or put the Perl\site\bin directory on the PATH with:
path C:\Perl64\site\bin;%PATH%
I tried running the command and also added the pathname manually into the Path Environment Varaible but I'm still receiving the error. It could be something as simple as I'm not putting the pathname correctly into Path.
Any help would be much appreciated.
Embarrassingly for me, restarting the computer did the trick. I suspect the Path Environment Variables hadn't taken effect so a restart was required to fix this.