I have a simple script that is getting hung up. This is supposed to check if the module i’m trying to use is already loaded. Then if it is say it’s loaded and continue, if not then load it and continue. After that i’m trying to use the powershell commands that are loaded in the module.
#check if module is already loaded
$module = Get-module -name "Get-RemoteProgram"
If (($module.Name) -match ("Get-RemoteProgram")){
write-host "module Get-RemoteProgram is loaded"}
else{
#import Get-RemotePrograms module
import-module "C:\scripts\Active Directory\Get-RemoteProgram.ps1"}
$computer = Read-Host -Prompt 'Input the computer name'
Get-RemoteProgram -ComputerName $computer | sort Programname
It works sometimes, but not all the time. This is a nice little module I found to help search remote machines to see what programs are installed.
So the script does load the module. The first time i open powershell and run all is good. If i’m open still, which the module is loaded, it will say it’s loaded. But when I run the command Get-RemoteProgram I get the error that the term is not recognized. Then a note about how it exists, but windowsPowershell does not load commands from the current location by default.
It says to add .\ to the beginning but that doesn’t work either.
Get-RemoteProgram.ps1 is the module to load, while inside it adds the command Get-RemmoteProgram. So First you load the module, then you use that command.
This seems like it is an environmental issue as it’s trying to load the command from elsewhere when it’s all in the current directory the module is in.
Well, you’ve got a bunch of stuff happening, largely because you’re not following the rules :).
The error you’re getting (cannot run from the current location) is because running “Get-RemoteProgram” is attempting to run Get-RemoteProgram.ps1, not an in-memory command. And Get-RemoteProgram.ps1 isn’t a module. I can tell, because it’s a .ps1 and not a .psm1. Now, I don’t know what you mean by “…it adds the command,” but I can guess that Get-RemoteProgram.ps1 contains a function, Get-RemoteProgram, which is fine, except that the function is scoped to the script.
So.
If Get-RemoteProgram.ps1 indeed contains a function named Get-RemoteProgram, then you should:
A) Rename Get-RemoteProgram.ps1 to a proper module filename, like RemoteProgramTools.psm1.
B) Put the .psm1 file into the proper location for modules (e.g., PSModulePath), such as /Program Files/WindowsPowerShell/Modules/RemoteProgramTools/RemoteProgramTools.psm1.
C) Simply run Get-RemoteProgram and let PowerShell do what it’s supposed to do.
The structure you’ve set up is not only fighting the shell’s native patterns, it’s setting up scope and naming-priority issues that are making the problem more complex to solve. This is a case where doing it the way the shell wants you to will pretty much immediately make all problems go away, with zero coding on your part. The shell is -designed- to do what you want to do, here, you just have to follow its rules. You shouldn’t have to hand-code what you’re doing.
I’ve tried what you suggest by putting both a ps1 and psm1 into any folder I have seen in PSModulePath, and so far it doesn’t load unless I import-module.
I have in the past tried to put powershell modules in the environmental folders (inside folders and out) and never once have they loaded when I open powershell. Not sure why since everything I read says the same thing Don suggests. Which makes me write my own scripts in the long run.