Note that there are some explanatory texts on larger screens.

plurals
  1. POImport-Pssession is not importing cmdlets when used in a custom module
    text
    copied!<p>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.</p> <p>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.</p> <p>Here is a copy of the basic function:</p> <blockquote> <p>Function Connect-O365{ $o365cred = Get-Credential username@domain.onmicrosoft.com $session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri <a href="https://ps.outlook.com/powershell/" rel="nofollow">https://ps.outlook.com/powershell/</a> -Credential $o365cred -Authentication Basic -AllowRedirection Import-PSSession $session365 -AllowClobber }</p> </blockquote> <p>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.</p> <p>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.</p> <p>I'm looking for suggestions on how to get around this.</p> <h3>EDIT</h3> <p>When I create the following module and run Connect-O365 the imported cmdlets will not be available.</p> <blockquote> <p>$scriptblock = { Function Connect-O365{ $o365cred = Get-Credential username@domain.onmicrosoft.com $session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "<a href="https://ps.outlook.com/powershell/" rel="nofollow">https://ps.outlook.com/powershell/</a>" -Credential $o365cred -Authentication Basic -AllowRedirection Import-PSSession $session365 -AllowClobber } }</p> <p>New-Module -Name "Office 365" -ScriptBlock $scriptblock</p> </blockquote> <p>When I import the next module without the Connect-O365 function the imported cmdlets are available.</p> <blockquote> <p>$scriptblock = { $o365cred = Get-Credential username@domain.onmicrosoft.com $session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "<a href="https://ps.outlook.com/powershell/" rel="nofollow">https://ps.outlook.com/powershell/</a>" -Credential $o365cred -Authentication Basic -AllowRedirection Import-PSSession $session365 -AllowClobber }</p> <p>New-Module -Name "Office 365" -ScriptBlock $scriptblock</p> </blockquote> <p>This appears to be a scope issue of some sort, just not sure how to get around it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload