Powershell Custom Module

Hello Posh Fam,

Just wondering what I may be doing wrong with the following:

I edited the $profile content to hold the following:

Write-Debug -Message ‘Installing CustomModule1’ -Debug

Import-Module E:#_Posh\Custom_Modules\CustomModule1.psm1 -Global

Write-Verbose “Module import complete”#>

My intent is to solely load the module so that in my Powershell session I am able to call the functions that live in the module.

I am having issues however as one of the functions I have is to create a PSSession to our Exchange environment.

Performing a Get-Module shows the CustomModule I imported at the start of Powershell, however when calling the function to create a New-PSSession to Exchange, the function runs fine, however none of the Exchange cmdlets are there, as if the PSSession was never created/established. Also during the function run, the progress bar shows the same amount of commands that are imported, is the same when I run the function on the Powershell session without first importing the module that contains that function.

Am I just not understanding how modules work?

Thanks in advance for any guidance,

AC

Please remove the ‘#’ in the Import-Module command, that is treating the path as a comment.

Or wrap them in quotes.

Hello All - Appreciate the replies and advices, but unfortunately the Exchange cmdlets are still not present.

After I modified the stated “Import-Module” path $profile I relaunched Powershell and after it successfully loaded, ran a Get-Command mailbox and the only returning results were the commands from the custom module. Cmdlets such as Get-Mailbox, Get-Recipient, Get-DistributionGroup…etc were not present, which leads me to believe that no cmdlets for Exchange were exported.

Here is a result of my test run.

DEBUG: Calling AD PS Drive create function

Confirm
Continue with this operation?
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"): A
VERBOSE: PS DRIVE AD Does not exist...Creating AD PsDrive
VERBOSE: Importing ActiveDirectory module
VERBOSE: Create PSDrive for Active Directory Connection

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
AD                                  ActiveDire... //RootDSE/
Authentication Successful
Importing PSSession for Exchange
WARNING: The names of some imported commands from the module 'tmp_asbcr4mb.1vw' include unapproved verbs that might
make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the
Verbose parameter. For a list of approved verbs, type Get-Verb.

Name              : tmp_asbcr4mb.1vw
Path              : C:\Users\NetworkID\AppData\Local\Temp\tmp_asbcr4mb.1vw\tmp_asbcr4mb.1vw.psm1
Description       : Implicit remoting for http://<exchangeserverhostfqdn>/PowerShell
Guid              : b95bd0bf-72f1-4b33-89f3-960fd0a36690
Version           : 1.0
ModuleBase        : C:\Users\NetworkID\AppData\Local\Temp\tmp_asbcr4mb.1vw
ModuleType        : Script
PrivateData       : {ImplicitRemoting}
AccessMode        : ReadWrite
ExportedAliases   : {}
ExportedCmdlets   : {}
ExportedFunctions : {[Add-ADPermission, Add-ADPermission], [Add-AvailabilityAddressSpace,
                    Add-AvailabilityAddressSpace], [Add-ContentFilterPhrase, Add-ContentFilterPhrase],
                    [Add-DatabaseAvailabilityGroupServer, Add-DatabaseAvailabilityGroupServer]...}
ExportedVariables : {}
NestedModules     : {}

PSSession Imported
ADServerSettings to viewEntireForest set to $true


PS C:\Windows\system32> Get-Command '*mailbox*'

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        cus_Connect-DisconnectedMailbox                    0.0        CustomModule1
Function        cus_Get-RemoteMailboxALLDC                         0.0        CustomModule1
Function        cus_Show-DisconnectedMailboxes                     0.0        CustomModule1


PS C:\Windows\system32> get-mailbox -identity NetworkID
get-mailbox : The term 'get-mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ get-mailbox -identity NetworkID
+ ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (get-mailbox:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

If I just simply load the function unto the Powershell session I am able to call the Exchange cmdlets. It is only when I call the function after importing the module the Exchange cmdlets are not present.

Is there a difference between calling the same function after it has been imported via a module vs. “loading” the function unto the Powershell session?

Thank you all again,

AC

Ensure the required CmdLets/Functions are exporting properly.

And also verify the functions under the AD PSDrive.

If all you are trying to do is load your module at the start of the session you just need to put your module in one of the defined module locations or create your own. You preferably want to put it in:

C:\Users\_user_\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules

Just make sure you put your psm1 file in a folder with the same name. Ex:

C:\Program Files\WindowsPowerShell\Modules\CustomModule\CustomModule.psm1

As far as the Exchange cmdlets go have you tried to create a session to your Exchange server without using your function?

Enter-PSSession -ComputerName '_ExchangeServer_'

If you run that do you have access to the Exchange cmdlets?

@camusicjunkie; Appreciate the information with loading the modules in the default $env:psmodule path. I will give that a try but currently I previously posted, when I create a Exchange PSSession the Exchange posh cmdlets are not being loaded. I confirmed this by checking the psdrive functions repository as suggested by @Kiran. Notet that though the PSSession shows successful (Get-PSSession), the cmdlets are nowhere to be found.

Thank you in advance for any assistance,

 

Importing PSSession for Exchange
WARNING: The names of some imported commands from the module ‘tmp_asbcr4mb.1vw’ include unapproved verbs that might
make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the
Verbose parameter. For a list of approved verbs, type Get-Verb.

Name : tmp_asbcr4mb.1vw
Path : C:\Users\NetworkID\AppData\Local\Temp\tmp_asbcr4mb.1vw\tmp_asbcr4mb.1vw.psm1
Description : Implicit remoting for http:///PowerShell
Guid : b95bd0bf-72f1-4b33-89f3-960fd0a36690
Version : 1.0
ModuleBase : C:\Users\NetworkID\AppData\Local\Temp\tmp_asbcr4mb.1vw
ModuleType : Script
PrivateData : {ImplicitRemoting}
AccessMode : ReadWrite
ExportedAliases : {}
ExportedCmdlets : {}
ExportedFunctions : {[Add-ADPermission, Add-ADPermission], [Add-AvailabilityAddressSpace,
Add-AvailabilityAddressSpace], [Add-ContentFilterPhrase, Add-ContentFilterPhrase],
[Add-DatabaseAvailabilityGroupServer, Add-DatabaseAvailabilityGroupServer]…}
ExportedVariables : {}
NestedModules : {}

PSSession Imported
ADServerSettings to viewEntireForest set to $true

PS C:\Windows\system32> Get-Command ‘mailbox

CommandType Name Version Source


Function cus_Connect-DisconnectedMailbox 0.0 CustomModule1
Function cus_Get-RemoteMailboxALLDC 0.0 CustomModule1
Function cus_Show-DisconnectedMailboxes 0.0 CustomModule1

PS C:\Windows\system32> get-mailbox -identity NetworkID
get-mailbox : The term ‘get-mailbox’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ get-mailbox -identity NetworkID
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (get-mailbox:String) , CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException