Modules that require Modules

Welcome Forums General PowerShell Q&A Modules that require Modules

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
2 years, 7 months ago.

  • Author
    Posts
  • #38459

    Participant
    Points: 0
    Rank: Member

    Hi,
    If I build a module that requires something like an exchange or AD module to be loaded first, what is the best practice for loading it? Should I put it in the module and unload it at the end?

  • #38463

    Participant
    Points: 0
    Rank: Member

    Best Practice would be to specify the module in the RequiredModules of the module manifest (https://msdn.microsoft.com/en-us/windows/dd878337(v=vs.71).aspx). this will ensure the the scripts calling your module also import the modules you depend on prior to successfully importing yours.
    Also see https://powershell.org/forums/topic/using-requires-in-a-powershell-module-psm1/

  • #38465

    Participant
    Points: 0
    Rank: Member

    Thanks, I think this is what I am after.
    I will do some testing today and see if it works as i think it will.

  • #38469

    Participant
    Points: 0
    Rank: Member

    That worked perfectly!
    Thanks for the pointer.

  • #38498

    Participant
    Points: 0
    Rank: Member

    This works fine for importing the AD module, but what about connecting to exchange before running a command?

    Should I create a script that connects to exchange via New-PSSession and call that in the function? I could add it to ScriptsToProcess but that would create a remote session even when I am not using it. This is the function I have, it just looks for the owner in exchange and if not found then it looks in AD.

    Function Get-Owner {
    
    Param(
    	[Parameter(Mandatory=$true)]
    	[string]
    	$object
    	) #exit Param
    
    	$UserInfo = Get-User -Identity $Object -erroraction 'silentlycontinue'
    	$UserInfo.Manager
    
    	if ([String]::IsNullOrEmpty($UserInfo.Manager))
    	{
    		$ManagedBy = Get-ADGroup -Identity $Object -Properties ManagedBy 
    		$ManagedBy.ManagedBy
    	}
    }
    
  • #38499

    Participant
    Points: 324
    Helping Hand
    Rank: Contributor

    Possibly something like this:

    Function Get-Owner {
    
        Param(
    	    [Parameter(Mandatory=$true)]
    	    [string]
    	    $object
    	) #exit Param
        begin {
            Import-Module ActiveDirectory
            $ExSession= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exserver.contoso.com/PowerShell/ -Authentication Kerberos
            Import-PSSession $ExSession
    
        } #begin
        process {
    
    	    $UserInfo = Get-User -Identity $Object -erroraction 'silentlycontinue'
    	    $UserInfo.Manager
    
    	    if ([String]::IsNullOrEmpty($UserInfo.Manager))
    	    {
    		    $ManagedBy = Get-ADGroup -Identity $Object -Properties ManagedBy 
    		    $ManagedBy.ManagedBy
    	    }
        }# process
        end {
            $ExSession | Remove-PSSession
        } # end
    }
    

    However, you would probably want to execute your AD check not in the Exchange session. So, you could move that above the AD check or possibly split into separate functions, one to check AD and the other Exchange

    $owner = Get-OwnerExchange
    if (!$owner) {
        $owner = Get-OwnerAD
    }
    
  • #38504

    Participant
    Points: 0
    Rank: Member

    Thanks. I'm still fairly new to PS so trying to get my head around best practices (Along with everything else!).
    I'm was trying to avoid running these twice as I run this really often during the day so that is why it is in one function.
    I will also look at adding some error handling as I don't like how it continues on error now.
    Apart from that at least I know this is the best way as I have heaps more to add to my module that relies on exchange connection first.

The topic ‘Modules that require Modules’ is closed to new replies.