Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added M365 connections to shared #2090

Open
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

iserrano76
Copy link
Contributor

Issue:
Added two functions to Shared in case any script needs a connection to M365.
One function for Exchange Online another function for AzureAD

Reason:
Unify the way we connect to M365.

Fix:
Both functions will do the following:

  • Verify if the module is installed, if not, install it after request confirmation.
  • Verify if the module is loaded, if not, load it.
  • Lastly, verify if any connection is present (we verify only one connection is present); if none, we offer to connect.
    Important: we do not disconnect at anytime we just verify if we have an active session

@iserrano76 iserrano76 requested a review from a team as a code owner May 9, 2024 14:05
Shared/Connect-M365.ps1 Outdated Show resolved Hide resolved
Shared/Connect-M365.ps1 Outdated Show resolved Hide resolved
Shared/Connect-M365.ps1 Outdated Show resolved Hide resolved
Shared/Connect-M365.ps1 Outdated Show resolved Hide resolved
Shared/Connect-M365.ps1 Outdated Show resolved Hide resolved
Shared/Connect-M365.ps1 Outdated Show resolved Hide resolved
Shared/Connect-M365.ps1 Outdated Show resolved Hide resolved
Shared/Connect-M365.ps1 Outdated Show resolved Hide resolved
Shared/Connect-M365.ps1 Outdated Show resolved Hide resolved
Shared/Connect-M365.ps1 Outdated Show resolved Hide resolved
Copy link
Contributor

@lusassl-msft lusassl-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes as there are a few things that should be changed (see comments).

Copy link
Member

@dpaulson45 dpaulson45 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address all open comments then let me know when this is ready for the next review.

Shared/Connect-M365.ps1 Outdated Show resolved Hide resolved
Shared/Connect-M365.ps1 Outdated Show resolved Hide resolved
Shared/Connect-M365.ps1 Outdated Show resolved Hide resolved
Shared/ModuleHandle.ps1 Show resolved Hide resolved
Copy link
Member

@dpaulson45 dpaulson45 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be some more things that I find later on, but here is a good starting point to address.

function Show-EXOConnection {
param (
[Parameter(Mandatory = $true)]
[Microsoft.Exchange.Management.ExoPowershellSnapin.ConnectionInformation]$Connection
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this correctly work when you have multiple sessions used with $AllowMultipleSessions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, you are sending by parameter the session that you created, just one, and that is the one that we are showing.
If you create a second one or a third one, is going to have a new call to the function that shows that information.
I think I test it when I created, but I will test it and paste some screenshots.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, $newConnection should only have 1 object in the array by the time we get here, so it should work as expected.

Shared/M365/EXOConnection.ps1 Outdated Show resolved Hide resolved
Shared/ModuleHandle.ps1 Show resolved Hide resolved
Shared/M365/GraphConnection.ps1 Outdated Show resolved Hide resolved
Shared/ModuleHandle.ps1 Outdated Show resolved Hide resolved
Shared/M365/GraphConnection.ps1 Outdated Show resolved Hide resolved
Shared/M365/GraphConnection.ps1 Show resolved Hide resolved
Shared/M365/EXOConnection.ps1 Show resolved Hide resolved
Shared/ModuleHandle.ps1 Show resolved Hide resolved
if (-not $DoNotShowConnectionDetails) {
Show-EXOConnection -Connection $newConnection
}
return $newConnection
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we returning a connection here? Trying to understand this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big reason why is trying to understand this section of code:

    if ($null -eq $connections -or $AllowMultipleSessions) {
        if ($connections.ModulePrefix -contains $Prefix) {
            Write-Host "You already have a session with the prefix $Prefix" -ForegroundColor Red
            return $null
        } else {

Copy link
Contributor Author

@iserrano76 iserrano76 Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to return the connection because you could use different connections and you can control which one is under your responsibility, which one you created and which one is not your connection.

On that part of the code, I want to create a connection only if you do not have a connection and you do not use allow multiplesessions.
In case that you use allowmultiplesession, we will verify that we do not have a duplicated prefix.

It could be useful in future in case that we want to close a created connection or the one that you are going to use.

For example, in MDO we get information from EXO connection and compare with Graph connection to confirm that we are connected to the same tenant.

@dpaulson45
Copy link
Member

We should also look into adding pester testing here as well.

@iserrano76
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@iserrano76
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@iserrano76
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Shared/M365/EXOConnection.ps1 Outdated Show resolved Hide resolved
Shared/M365/EXOConnection.ps1 Show resolved Hide resolved
function Show-EXOConnection {
param (
[Parameter(Mandatory = $true)]
[Microsoft.Exchange.Management.ExoPowershellSnapin.ConnectionInformation]$Connection
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, $newConnection should only have 1 object in the array by the time we get here, so it should work as expected.

[Parameter(Mandatory = $false)]
[switch]$DoNotShowConnectionDetails,
[Parameter(Mandatory = $false)]
[System.Version]$MinModuleVersion = 2.0.0.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a script validation here for the min version supported.

Let's also add some version information to get the module that we are currently using, just in case things change with Graph.

Shared/M365/GraphConnection.ps1 Outdated Show resolved Hide resolved
Shared/M365/GraphConnection.ps1 Outdated Show resolved Hide resolved
@iserrano76
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@iserrano76
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants