I have a PowerShell script/function that works great when I use it in my PowerShell profile or manually copy/paste the function in the PowerShell window.
I'm trying to make the function accessible to other members of my team as a module. I want to have the module stored in a central place so we can all add it to our PSModulePath.
Here is a copy of the basic function:
Function Connect-O365{
$o365cred = Get-Credential
[email protected]
$session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $o365cred -Authentication Basic -AllowRedirection
Import-PSSession $session365 -AllowClobber
}
If I save this function in my PowerShell profile it works fine. I can dot source a *.ps1 script with this function in it and it works as well.
The issue is when I save the function as a *.psm1 PowerShell script module. The function runs fine but none of the exported commands from the Import-PSSession are available. I think this may have something to do with the module scope.
I'm looking for suggestions on how to get around this.
EDIT
When I create the following module and run Connect-O365 the imported cmdlets will not be available.
$scriptblock = { Function Connect-O365{ $o365cred = Get-Credential
[email protected] $session365 = New-PSSession
-ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $o365cred
-Authentication Basic -AllowRedirection Import-PSSession $session365 -AllowClobber } }
New-Module -Name "
Office 365" -ScriptBlock $scriptblock
When I import the next module without the Connect-O365 function the imported cmdlets are available.
$scriptblock = { $o365cred = Get-Credential
[email protected] $session365 = New-PSSession
-ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $o365cred
-Authentication Basic -AllowRedirection Import-PSSession $session365 -AllowClobber }
New-Module -Name "
Office 365" -ScriptBlock $scriptblock
This appears to be a scope issue of some sort, just not sure how to get around it.