# Office 365 – Using Import-PSSession from separate module

Recently I’ve been working on a little code allowing me to connect to Office 365. It’s not a really big deal,…

blog

28 Oct 2018 pl

![Office 365 – Using Import-PSSession from separate module](/wp-content/uploads/2018/10/img_5bd62f27ae36b.png)

Recently I've been working on a little code allowing me to **connect** to **Office 365. **It's not a really big deal, and there's a lot of options around. I wanted something of my own and something that outputs stuff I want it to output. All was going fine until I decided to move the same code that was working correctly into a separate module. Even thou commands **Connect-WinExchange** or **Connect-WinAzureAD** were executing successfully none of the commands from those sessions worked. No **Get-Mailbox, **no **Get-MailContact**. It turns out that being in another module actually puts a scope on it and therefore it's not provided when used outside of that module. I've considered two workarounds for this.

### 💡 Workaround One

Use **Import-PSSession** in your main module and keep all other code in your “connectivity” module.

```powershell
Import-PSSession -Session $Session -AllowClobber -DisableNameChecking -Prefix $Prefix -Verbose:$false
```

And this basically works as expected. The only thing is you can't have **Connect-WinExchange** as a standalone command. You need to use it just to build connection.

```powershell
$Session = Connect-WinExchange -Credentials $Credentials
Import-PSSession -Session $Session -AllowClobber -DisableNameChecking -Prefix $Prefix -Verbose:$false
```

If you can live with it.. it's good solution.

### 💡 Workaround Two

Other workaround is to use **Import-Module**. While it doesn't come as solution at first sight you can actually do something like this

```powershell
Import-Module (Import-PSSession -Session $Session -AllowClobber -DisableNameChecking -Verbose:$false) -Global
```

Therefore your function would look like this (more or less)

```powershell
function Connect-WinExchange {
    [CmdletBinding()]
    param(
        [string] $SessionName = 'Exchange',
    )
    # Your Code Goes Here
    Import-Module (Import-PSSession -Session $Session -AllowClobber -DisableNameChecking -Verbose:$false) -Global
}
```

[!\[Import-PSSession\](/wp-content/uploads/2018/10/img_5bd62f27ae36b.png)](/wp-content/uploads/2018/10/img_5bd62f27ae36b.png)

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:7F3C7A210181D8B436678C597A4416AEF27B176CC646E0BA4366850453ED4F93 -->
