module check then module import

This topic contains 10 replies, has 3 voices, and was last updated by  Jeff Scharfenberg 12 months ago.

  • Author
    Posts
  • #60250

    Jeff Scharfenberg
    Participant

    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.

    Any advice would help.

    thanks,

  • #60253

    Don Jones
    Keymaster

    Get-RemoteProgram is an odd module name.

    Anyway, why not just load it, and suppress the Warning that occurs when you try to load an already-loaded module?

    Or why not just put the thing where it's supposed to be (PSModulePath), and let PowerShell worry about all of this for you?

  • #60255

    Ron
    Participant

    This assumes that the module is available to import. (Untested)

    If (!(Get-module "Get-RemoteProgram")) {
      Import-Module "C:\scripts\Active Directory\Get-RemoteProgram.ps1"}
    }
  • #60258

    Jeff Scharfenberg
    Participant

    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.

  • #60259

    Don Jones
    Keymaster

    Yeah, so maybe we should clarify stuff, because possibly I don't understand what's happening.

    Is Get-RemoteProgram.ps1 a proper module? In other words, does it contain a function called Get-RemoteProgram? Or is Get-RemoteProgram.ps1 a script?

  • #60262

    Jeff Scharfenberg
    Participant

    Sorry my bad.

    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.

  • #60265

    Don Jones
    Keymaster

    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.

  • #60268

    Jeff Scharfenberg
    Participant

    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.

  • #60271

    Ron
    Participant

    What powershell version? I see a lot of solutions here that assume 3.0+. Some of us are stuck with 2.0 in an environment we don't control.

  • #60276

    Jeff Scharfenberg
    Participant

    PSVersion 5.0.

    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.

  • #60279

    Jeff Scharfenberg
    Participant

    And now for some reason it seems to be sticking when I open powershell. Ooooook. I'll reboot later and see if it sticks.

    thanks guys!

You must be logged in to reply to this topic.