Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 14975

VB.NET, Powershell and Exchange Online O365 (Connect-ExchangeOnline)

$
0
0
I have an application that automates multiple processes in my organisation including manipulating settings in Exchange 365 Online, setting setting for new users/leavers, amongst the tasks of logging to a SQL database and Active Directory manipulation, it opens a PowerShell runspace lines up the commands and performs (invokes them).

The credentials part of the Runspace is opened with:
Code:

Dim o365credential As PSCredential = New PSCredential(TXTO365UN.Text, secureString)
                Dim o365connectionInfo As WSManConnectionInfo = New WSManConnectionInfo(New Uri("https://outlook.office365.com/powershell-liveid/"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", o365credential) With {
                .AuthenticationMechanism = AuthenticationMechanism.basic,
                .SkipCACheck = True,
                .SkipCNCheck = True,
                .MaximumConnectionRedirectionCount = 4
            }
                Dim runspace As Runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(o365connectionInfo)
                runspace.Open()

Where securestring is password

This worked fine until we've tightened our security belts to turn on Modern Authentication, this has has the seeming side effect of breaking the PowerShell commands I run within VB they now break with the following error:
Quote:

Connecting to remote server outlook.office365.com failed with the following error message : Access is denied.
Taking a step out of the VB environment and back into PowerShell and starting a new PSSession I get the same message.

I have found the command
Code:

Connect-ExchangeOnline -UserPrincipalName email@domain.com
This works in PowerShell.

My problem is how can I translate this into the VB code to do the same?

I have tried:
Code:

    Dim runspace As Runspace = RunspaceFactory.CreateRunspace()
                runspace.Open()
                Dim CMDSetMig2 As Command = New Command("Import-Module")
                CMDSetMig2.Parameters.Add("Name", "ExchangeOnlineManagement")
                Dim CMDSetMig1 As Command = New Command("Connect-ExchangeOnline")
                CMDSetMig1.Parameters.Add("UserPrincipalName", "email@domain.com")
                Using MigPileLine As Pipeline = runspace.CreatePipeline()
                    MigPileLine.Commands.Add(CMDSetMig2)
                    MigPileLine.Commands.Add(CMDSetMig1)
                    commandResults = MigPileLine.Invoke()
                    MigPileLine.[Stop]()
                    MigPileLine.Dispose()
                End Using

When it comes to Invoke time it completes the Import-Module but then falls over on the Connect-ExchangeOnline with the error:
Quote:

System.Management.Automation.CommandNotFoundException: 'The term 'Connect-ExchangeOnline' is not recognized as the name of a cmdlet, function, script file, or operable program.
If anyone has any ideas I'd be grateful to hear them please!

Viewing all articles
Browse latest Browse all 14975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>