The problem you are facing is that you need to be able to set the PSSession for all of the imported functions implicitly. To do that you need to be able run the Set-PSImplicitRemotingSession
function.
Unfortionatly that function is not being exported so you cannot access it. What you need to do to resolve this is crack open the PSM1 file and add that function to the end of $script:ExportModuleMember
. Now when you import the module that function will be abilable to set your PSSession for all of the functions.
Here is what your powershell or scripts will need to run to be able to use any of the imported modules.
Import-Module "C:\Credentials.psm1"
Import-Module "C:\ExportedPSSession.psm1"
$Cred = Import-Credential -path C:\Cred.xml
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Authentication Basic -AllowRedirection -Credential $Cred
Set-PSImplicitRemotingSession -PSSession $Session -createdByModule $True
#You can now run any of the imported functions.
Credentials.psm1 Beware! Anyone who can load the xml file can now impersonate you!
function Export-Credential($cred, $path) {
$cred = $cred | Select-Object *
$cred.password = $cred.Password | ConvertFrom-SecureString
$obj = New-Object psobject
$obj | Add-Member -MemberType NoteProperty -Name UserName -Value $cred.username
$obj | Add-Member -MemberType NoteProperty -Name Password -Value $cred.password
$obj | Export-Clixml $path
}
function Import-Credential($path) {
$obj = Import-Clixml $path
$obj.password = $obj.Password | ConvertTo-SecureString
return New-Object system.Management.Automation.PSCredential( $obj.username, $obj.password)
}