Author Posts

May 1, 2016 at 2:43 pm

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?

May 1, 2016 at 3:32 pm

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/

May 1, 2016 at 3:44 pm

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.

May 1, 2016 at 10:18 pm

That worked perfectly!
Thanks for the pointer.

May 2, 2016 at 4:34 pm

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
	}
}

May 2, 2016 at 5:03 pm

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
}

May 2, 2016 at 6:14 pm

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.