locked
Import-Pssession is not importing cmdlets when used in a custom module RRS feed

  • Question

  • 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 username@domain.onmicrosoft.com $session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUrihttps://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.

    I've posted this on www.stackoverflow.com as well but unfortunately haven't had any luck finding a solution.


    Monday, December 3, 2012 4:20 PM

Answers

  • In PowerShell sessions: Import-Module (Connect-O365)

    I can't believe it was that simple.  Thanks so much for posting this!  It led me to make the following modification to the script module.

    function Connect-O365{
    	$o365cred = Get-Credential username@domain.onmicrosoft.com
    	$session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $o365cred -Authentication Basic -AllowRedirection 
    	Import-Module (Import-PSSession $session365 -AllowClobber) -Global
    }

    Now when the Import-Pssession runs and returns a script module the Import-Module effectively "catches" that module and sets it up in the global scope....I think.  I have some coworkers testing this, I'll report back any other issues.

    Monday, December 10, 2012 6:36 AM
  • In PowerShell sessions: Import-Module (Connect-O365)

    Monday, December 10, 2012 6:15 AM

All replies

  • Hi,

    As we know there are some kind of powershell profile:

    You can have four different profiles in Windows PowerShell. The profiles are listed in load order. The most specific profiles have precedence over less specific profiles where they apply.

    • %windir%\system32\WindowsPowerShell\v1.0\profile.ps1            

      This profile applies to all users and all shells.
    • %windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1            

      This profile applies to all users, but only to the Microsoft.PowerShell shell.
    • %UserProfile%\My Documents\WindowsPowerShell\profile.ps1            

      This profile applies only to the current user, but affects all shells.
    • %UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1            

      This profile applies only to the current user and the Microsoft.PowerShell shell.

    What happen when you save it in the first path.

    In addition, please also refer to the below links:

    Windows PowerShell Profiles

    http://msdn.microsoft.com/en-us/library/windows/desktop/bb613488(v=vs.85).aspx

    Understanding the Six PowerShell Profiles

    http://blogs.technet.com/b/heyscriptingguy/archive/2012/05/21/understanding-the-six-powershell-profiles.aspx

    Regards,

    Yan Li


    Cataleya Li
    TechNet Community Support

    • Marked as answer by Yan Li_ Monday, December 10, 2012 1:50 AM
    • Unmarked as answer by Douglas Plumley Monday, December 10, 2012 3:58 AM
    Thursday, December 6, 2012 7:38 AM
  • Hi Yan Li,

    Thanks for your response, unfortunately this is not an issue with profiles, this is an issue with PowerShell script modules (*.psm1).

    When I create the following module and run Connect-O365 the cmdlets will not be available.

    $scriptblock = {
    	Function Connect-O365{
    		$o365cred = Get-Credential username@domain.onmicrosoft.com
    		$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 cmdlets are available.

    $scriptblock = {
    		$o365cred = Get-Credential username@domain.onmicrosoft.com
    		$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.

    • Proposed as answer by ztrhgf Tuesday, February 2, 2016 12:58 PM
    Monday, December 10, 2012 4:58 AM
  • In PowerShell sessions: Import-Module (Connect-O365)

    Monday, December 10, 2012 6:15 AM
  • In PowerShell sessions: Import-Module (Connect-O365)

    I can't believe it was that simple.  Thanks so much for posting this!  It led me to make the following modification to the script module.

    function Connect-O365{
    	$o365cred = Get-Credential username@domain.onmicrosoft.com
    	$session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $o365cred -Authentication Basic -AllowRedirection 
    	Import-Module (Import-PSSession $session365 -AllowClobber) -Global
    }

    Now when the Import-Pssession runs and returns a script module the Import-Module effectively "catches" that module and sets it up in the global scope....I think.  I have some coworkers testing this, I'll report back any other issues.

    Monday, December 10, 2012 6:36 AM
  • Doug,

    I wanted to thank you for providing the code snippet that does this.  I just ran into the same issue.  I found that any functions I wrote within my module could use the Exchange commands, but attempting to run anything else from the shell did not work.

    Thanks again for actually coming back with the answer rather than just saying "fixed it".

    Tuesday, April 9, 2013 1:40 AM
  • Super. thanks for this answer! I know this is an old thread but this helped me immensely when I struggled with this same issue for a script module I created. Thanks again!
    Tuesday, June 17, 2014 6:45 PM
  • This thread continues to save! I am writing a module for Skype Online, and I ran into the exact same issue. I had a connect function and it would never import the module when using the function inside the module. If I copy/pasted the entire function into the PowerShell window, it worked just fine. In case anyone is looking for the Skype solution to this:

    $SkypeOnlineSession = New-CsOnlineSession -Credential (Get-Credential)
    Import-Module (Import-PSSession -Session $SkypeOnlineSession -AllowClobber) -Global


    Thursday, April 6, 2017 9:16 PM