1

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 = 'admin@shootme.com';
$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?

Rub3s
  • 359
  • 2
  • 13
  • 1
    You may need to look at the compatibility of the libraries required for that module with PowerShell Core. My guess is within the inner exceptions, something will be missing that is required for that cmdlet to function as you had expected in the Desktop version of PowerShell. – Ash Dec 17 '19 at 16:03
  • @Ash Maybe that is the case, but the documentation is really bad. No idea what microsoft wants with ps 6. – Rub3s Dec 19 '19 at 11:04
  • 1
    If you are using C# you could target .Net Standard and use the [Sharepoint libraries](https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code) to create your own cmdlets. – Ash Dec 19 '19 at 11:26
  • @Ash I'm not sure how to target .Net Standard (targeting .net core 3.1 right now), but I actually already tried that solution: I got the exact same error. (400) Bad Request. I think that has to do with this: https://sharepoint.uservoice.com/forums/329220-sharepoint-dev-platform/suggestions/16585795-support-net-core-with-csom?page=1&per_page=20 (currently no .net core support for CSOM library) – Rub3s Dec 19 '19 at 13:36
  • If you create a .NET Standard app and then change it to target a compatible version of .NET Framework, such as 4.7.2, you can use older libraries - https://learn.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support – Ash Dec 19 '19 at 15:43
  • @Ash Bit late comment from me: I'm targeting .Net Core 3.1 and am probably using .Net Standard 2.1. That is incompatible with .Net Framework according to that table. – Rub3s Jan 06 '20 at 10:19
  • You would need to target .NET Standard 2.0. The table indicates the minimum version of .Net Core or Framework that will support that level of .Net Standard i.e. .Net Core 2.0 onwards supports .Net Standard 2.0, this allowing you to reference .Net Framework libraries. – Ash Jan 06 '20 at 11:13
  • @Ash I searched around a bit, but targeting .Net Standard x.x seems to be only possible when creating a code library and not for a web app: https://stackoverflow.com/questions/46621164/how-do-i-target-net-standard-2-0-in-a-freshly-created-net-core-2-0-web-app . I could create a second project of course, a code lib that targets .net core 2.0. Or maybe I still go for the api-way. Or maybe I wait till Microsoft updates their Sharepoint libraries. Don't know yet. – Rub3s Jan 06 '20 at 14:58

1 Answers1

1

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.

dschwartz0815
  • 127
  • 2
  • 7
  • Yes, I tried removing the credentials portion. Then I get the error that System.Windows.Forms is missing. It probably wants to launch a .net framework modal for credential input, so that is never going to work in ps 6.2.3. – Rub3s Dec 18 '19 at 09:15
  • 1
    And running powershell from a .bat file: The base application will be a .net core 3 website with c#, so I rather run the Powershell from c#. I'm thinking about a separate .net framework api now that can run Powershell 5.1. – Rub3s Dec 18 '19 at 09:18