Modules that require Modules

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Gary Smith Gary Smith 5 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #38459
    Profile photo of Gary Smith
    Gary Smith
    Participant

    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
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    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 http://powershell.org/forums/topic/using-requires-in-a-powershell-module-psm1/

    #38465
    Profile photo of Gary Smith
    Gary Smith
    Participant

    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
    Profile photo of Gary Smith
    Gary Smith
    Participant

    That worked perfectly!
    Thanks for the pointer.

    #38498
    Profile photo of Gary Smith
    Gary Smith
    Participant

    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
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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
    Profile photo of Gary Smith
    Gary Smith
    Participant

    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.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.