Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 510
Douglas Plumley Avatar asked Nov 21 '12 22:11

Douglas Plumley


People also ask

How do I import PSSession?

To import commands, first use the New-PSSession cmdlet to create a PSSession. Then, use the Import-PSSession cmdlet to import the commands. By default, Import-PSSession imports all commands except for commands that have the same names as commands in the current session.

How do I enable the import-module in PowerShell?

To import the module into all sessions, add an Import-Module command to your PowerShell profile. To manage remote Windows computers that have PowerShell and PowerShell remoting enabled, create a PSSession on the remote computer and then use Get-Module -PSSession to get the PowerShell modules in the PSSession.

Which PowerShell feature allows you to import a list of commands from a remote computer?

For remote computers that don't have PowerShell remoting enabled, including computers that aren't running the Windows operating system, you can use the CIMSession parameter of Import-Module to import CIM modules from the remote computer. The imported commands run implicitly on the remote computer.

What is AllowClobber in PowerShell?

-AllowClobberOverrides warning messages about installation conflicts about existing commands on a computer. Overwrites existing commands that have the same name as commands being installed by a module. AllowClobber and Force can be used together in an Install-Module command.


1 Answers

With some assistance from TechNet I was able to modify the script module so it worked the way I expected.

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-Module (Import-PSSession $session365 -AllowClobber) -Global
}

TechNet Post

like image 124
Douglas Plumley Avatar answered Sep 20 '22 12:09

Douglas Plumley