Managing SharePoint Online from PowerShell Core

As the SharePoint Online API needs to be installed on the scripting machine, running it outside of Windows is not as straightforward as with Exchange. The libraries of the module are compiled for the .NET Framework, and PowerShell Core uses .NET Framework Core, so we have an incompatibility problem. Once .NET Framework Core matures, the Office 365 API should be offered compiled for this new platform.

However, we can still use the SharePoint Online API from PowerShell Core through remoting. For the sake of an example, we will implement a client/server approach, similar to the way Exchange works. We will connect from a Linux machine running PowerShell Core to a server running PowerShell and with the SharePoint Online API also installed.

The example would be the same as the previous one, if not for the problem of passing credentials across the sessions. To open a connection with SharePoint Online, we need to pass credentials to the Connect-SPOService command. The problem is that the passing credentials across the sessions is not supported.

$spAdmin = Get-Credential [email protected]

Windows PowerShell Credential Request...

Enter your credentials.
Password for user [email protected]: ****************

Unable to load DLL 'api-ms-win-security-cryptoapi-l1-1-0.dll': The specified module or one of its dependencies could not be found.
(Exception from HRESULT: 0x8007007E)
+ CategoryInfo : ResourceUnavailable: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : System.Management.Automation.Remoting.PSRemotingDataStructureException

As a workaround, you can create new credentials by passing a secure string. This method is not ideal as your credentials will be in clear text, but consider that this is just a workaround until this bug is resolved:

Invoke-Command -Session $session -ScriptBlock { `
$password = ConvertTo-SecureString "password here" -AsPlainText -Force
$user = "[email protected]"
$creds = New-Object PSCredential -ArgumentList $user , $password
$uri = 'https://mytest321-admin.sharepoint.com'
Connect-SPOService -Url $uri -Credential $creds
}

A better approach is reusing the credential-serialization function we used in Chapter 6, Script Automation. This function serialized and encrypted the password on a file. We will load the credentials through a remote session so that all the serialization and decryption occurs on the server. To make sure this occurs on the server, we use Invoke-Command:

Invoke-Command -Session $session -ScriptBlock { `
$credPaths = 'C: empmytest321_admin.txt'
$rawCreds = Get-Content $credPaths

$creds = [System.Management.Automation.PSSerializer]::Deserialize($rawCreds )

$uri = 'https://mytest321-admin.sharepoint.com'
Connect-SPOService -Url $uri -Credential $creds
}

Once the connection to SharePoint Online is established, we can import the session as done in previous chapters and manage SharePoint from a Linux console. Here is the full example:

$localServerCreds = Get-Credential admin1

Windows PowerShell credential request
Enter your credentials.
Password for user admin1: **************

$session = New-PSSession -ComputerName pc11.dev.local -Credential `
$localServerCreds -Authentication Basic

Invoke-Command -Session $session -ScriptBlock {
$credPaths = 'C: empmytest321_admin.txt'
$rawCreds = Get-Content $credPaths

$creds = [System.Management.Automation.PSSerializer]::Deserialize($rawCreds)

$uri = 'https://mytest321-admin.sharepoint.com'
Connect-SPOService -Url $uri -Credential $creds
}

Import-PSSession $session

Get-SPOSite | Select Url

Url
---
https://mytest321-my.sharepoint.com/
https://mytest321.sharepoint.com/portals/hub

Even though this type of remoting will not be necessary for Office 365 products in the long run, it is very possible that some on-premise products will not have assemblies compatible with .NET Core. As PowerShell is replaced with PowerShell Core, this scenario will become more relevant for using legacy APIs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.32.1