Use Connect-SPOService with Powershell 6 (core version)

1.3k views Asked by At

I'm trying to connect to a sharepoint environment and I want to do that with Powershell version 6. Why? Eventually, I want to put the PS commands in a .net core 3 application. And as far as I know I cannot use PS5.1 in .net core.

It is about this powershell script:

Import-Module -Force -name Microsoft.Online.SharePoint.PowerShell;
Import-Module -Force -name Microsoft.Online.SharePoint.PowerShell -DisableNameChecking;
$username = '[email protected]';
$password = 'right now';
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force);
Connect-SPOService -Url https://shootme.sharepoint.com -Credential $cred;

When I try this in the default PS 5.1 it just works fine. When I try this with PS 6.2.3, I get an error:

Connect-SPOService : The remote server returned an error: (400) Bad Request.
At line:1 char:1
+ Connect-SPOService -Url https://shootme.sharepoint.com -Credent ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Connect-SPOService], WebException
+ FullyQualifiedErrorId : System.Net.WebException,Microsoft.Online.SharePoint.PowerShell.ConnectSPOService

Does the newer Powershell have different syntax orso, of what am I doing wrong? Also, maybe there is a way to run scripts in ps 5.1 when running them in .net core?

1

There are 1 answers

2
dschwartz0815 On

Have you tried connecting manually by removing the credentials portion and letting it prompt you for a login and test if that resolves successfully?

Edit: I do know you can also call powershell from a .bat like so:

powershell -version 2 .\xyz.ps1

But not knowing what you're going for exactly makes it tough to suggest if that's even a viable option.