SharePoint Online

SharePoint Online is a collaborative platform which is primarily used for document management and as a storage system. As with Exchange Online, it can be retrieved as a standalone service or with O365 in a bundle:

https://products.office.com/en-us/sharepoint/compare-sharepoint-plans:

In contrast to Exchange Online, you need to install the dedicated SharePoint Management Shell in advance, which can be found here:

https://www.microsoft.com/en-us/download/details.aspx?id=35588

For basic authentication with username and password, you can use the following code lines:

#Username / email address to connect to and manage SharePoint Online
$adminUPN="[email protected]"

#O365 organization name
$orgName="examplecompany"

#retrieving user credentials
$userCredential = Get-Credential -UserName $adminUPN -Message "Type the password."

#Using the sharepoint management shell cmdlet to connect to Sharepoint
Connect-SPOService -Url https://$orgName-admin.sharepoint.com -Credential $userCredential

If you have set up multifactor authentication, you will need to use the following authentication method:

#Using the sharepoint management shell cmdlet to connect to SharePoint by using multifactor authentication
Connect-SPOService -Url https://$orgName-admin.sharepoint.com

For automated scripts, though, you don't want to use the management for each operation. To accomplish this task, you can easily import the installed modules. The installation will add the directory to the PSModulePath environment variable of the machine. You can append the PSModulePath from Windows PowerShell into PowerShell Core 6.1++ easily with Add-WindowsPSModulePath.

After a reboot, you can try to import the module as follows:

#Finding the module
Get-Module -ListAvailable *online.sharepoint*

#Importing the module by name
Import-Module 'Microsoft.Online.SharePoint.PowerShell'

Right after the installation, this will not work, as PowerShell will not have loaded the module path and you will get the following error:

Import-Module : The specified module 'Microsoft.Online.SharePoint.PowerShell' was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ Import-Module 'Microsoft.Online.SharePoint.PowerShell'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (Microsoft.Online.SharePoint.PowerShell:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

To also avoid these kind of errors for updated PowerShell versions, you can import the module by path:

#Importing the module by path
Import-Module 'C:program filessharepoint online management shellMicrosoft.Online.SharePoint.PowerShell'

So an automated script could look similar to the following using these steps:

  1. Import will be processed by folder path and therefore work on all PowerShell versions.
  2. The password is stored and loaded securely.
  3. The connection is established.
  4. If the connection is successfully established, the following code will be executed:
#Importing the module by path
Import-Module 'C:program filessharepoint online management shellMicrosoft.Online.SharePoint.PowerShell'

#Username / email address to connect to and manage Sharepoint Online
$adminUPN = "[email protected]"

#O365 organization name
$orgName = "examplecompany"

#Retrieving password which was encrypted with a certificate and storing it as secure string
$password = Unprotect-CmsMessage -Content (Get-Content .PasswordDDN.txt) | ConvertTo-SecureString -AsPlainText -Force

#retrieving user credentials
$userCredential = New-Object -TypeName PSCredential $adminUPN, $password

try {
#Using the sharepoint management shell cmdlet to connect to Sharepoint by using user name and password
Connect-SPOService -Url https://$orgName-admin.sharepoint.com -Credential $userCredential
}
catch {
#Showing errors:
$errorMessage = $_.Exception.Message
$innerException = $_.Exception.InnerExceptionMessage
Write-Error "ErrorMessage: $errorMessage" + [System.Environment]::NewLine + "InnerException: innerException"
}

#Proving if connection was established correctly
if (-not $errorMessage) {
#Retrieving sites
$sites = Get-SPOSite

#Number sites
$sites.Count
}
To search for more cmdlets and examples, you can use the following link:

https://docs.microsoft.com/en-us/powershell/module/sharepoint-online/?view=sharepoint-ps
..................Content has been hidden....................

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