Calling Functions

Hi Guys,

Is there a simple way to call a function within a function?

I have written a module with 8 functions within it. For most functions they have to connect to either Exchange On Premise or Exchange Online.

Rather than have to put the code in each function to connect to Exchange, is there a way to make the connection to Exchange into a function then call it when needed throughout the script?

Many Thanks

If u have built a module just put the exchange connect function at the top and u can call ur connect-exchange function in any function within the module

When u ll load ur module from shell ur connect-exchange function ll be load as well so any call to this function is possible

Not sure if this is the proper way of going about it, but for a function I built to run in another script, I started the script with Import-Module and the function’s location (So something like “Import-Module C:\Function-Name.ps1”), then called the function later in the script.

To elaborate on what Cyril said, you could do something like this (bypassing params, etc. for brevity):

Function Connect-ExchangeOnline {
    # Make connection to EO

Function Connect-ExchangeOnPrem {
    # Make connection to on-prem

Function Get-LicensedUser {

    # code

Function DoSomethingOnPrem {

    # code

And so on… The key is to have the Connect-Exchange* function(s) declared before any other functions that call it/them.

Of course! why didnt I think of that. Thanks

Thanks for this too. I think Cyril was suggesting that youd connect to Exchange before defining any of the functions in the module. Then the commandlets would just be there to use. Not tried it, but sounds like either way could work