0

Dear PowerShell Experts,

We have below script and working fine on SharePoint 2013 Environment but its not working on SharePoint Online. While running, we are getting error message as "Add-Content : A positional parameter cannot be found that accepts argument 'Exception : For URL: The remote server returned an error: (403) Forbidden." Can anyone please suggest us, where we need to change commands line for working on SP online Environment.

###########################################################################################################################
# Script Used for :- Get SSL Certification detail From Web site
# Parameter:- SharePoint 2013 Source site URL (SourceWebURL), ListName(SourceListName), mailing address (To) and From (From) and days To left (DaysToLeft)
# This script will check SSL expireation date and update the expiration date in List and send alert mail To team, if certificate is going To expire in 'n' no of days (DaysToLeft)  
# Below Methos Create Encrypted Password 
#(Get-Credential).Password | ConvertFrom-SecureString | Out-File "D:\test\test1\folder.txt"
#(Get-Credential).Password | ConvertFrom-SecureString | Out-File "D:\test\test1\folder1.txt"
###########################################################################################################################

Cls

$DateTimeNow = Get-Date -format "dd-MMM-yyyy HH:mm"
$StartLog="Information : *******************Script started at $DateTimeNow (24-hour clock)**********************"

Add-PSSnapin Microsoft.SharePoint.PowerShell

$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
Add-Type -Path (Resolve-Path "$PSScriptRoot\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "$PSScriptRoot\Microsoft.SharePoint.Client.Runtime.dll")

#Read Config XMl
[xml]$Xmlinput =   get-content $PSScriptRoot\Config.xml
$Item = $Xmlinput.Config
#log folder location
$Location = $PSScriptRoot
#For folder creation with dd_mm_yyyy format
$GetDate = Get-Date
$Day = $GetDate.Day
$Month = $GetDate.Month
$Year = $GetDate.year
$Date= "$($Day)-$($Month)-$($Year)"
New-Item -ItemType Directory -Force -Path $Location
$pathLocation = $Location+"\Logs" +"\$date"
New-Item -ItemType Directory -Force -Path $pathLocation
$LogFilePath = "$($pathLocation)\log_$date.csv"

Add-Content -Path $LogFilePath -Value $StartLog 

Add-Content -Path $LogFilePath -Value "Information :Microsoft.SharePoint.PowerShell PSSnapin is added with SharePoint CSOM Assemblies"


$OnlinesiteUrl=$Item.OnlineSiteURL.SiteUrl  #O365 site URl
$OnlineLoginName=$Item.OnLineLoginName.OnLineLN #O365 site Login account name

$OnPremsiteUrl=$Item.OnPremSiteURL.SiteUrl  #Onprem site URl
$OnPremLoginName=$Item.OnPremLoginName.OnPremLN #Onprem site Login account name

$ScriptTimeList=$Item.ScriptTime.ScriptTimeList #Onprem script time stamp list

$OPPasswordFilePath=$PSScriptRoot+"\"+$Item.OnPremLoginEncPassword.OPPasswordFilePath #O365 site Login account encrypted passowrd
$OLPasswordFilePath=$PSScriptRoot+"\"+$Item.OnLineLoginEncPassword.OLPasswordFilePath #Onprem site Login account encrypt passowrd

$DashBoardList=$Item.DashBoardList.dashboardlistname


$To=$Item.To.EmailTo #Email To
$From= $Item.From.EmailFrom  #Email From
$Smtp = $Item.Smtp.EmailSmtp  #Email Smtp

$SourceList=$Item.SourceList.sourceListName
$AlertTypeList=$Item.AlertTypeList.alerttypelistname
$CurrentStatus=$Item.CurrentStatus.currentstatus
$UpdateInterval=$Item.UpdateInterval.UpdationInterval
$DaysToLeft=$Item.DaysLeft.daysToLeft
 
Add-Content -Path $LogFilePath -Value "Information :All configuration details are fecthed from configuration file and assigned into global variables"


# End Get and Set Varibale from Config XMl

#SharePoint Context Objects Declaration and initialization 
$Onlinepassword =Get-Content $OLPasswordFilePath | ConvertTo-SecureString 
$OnlineCtx = New-Object Microsoft.SharePoint.Client.ClientContext($OnlinesiteUrl) 
$OnlineCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($OnlineLoginName, $Onlinepassword)
$OnlineCtx.Credentials = $OnlineCredentials
Add-Content -Path $LogFilePath -Value "Information : SharePoint online password Declaration and initialization is completed"
 

$PasswordTemp = Get-Content $OPPasswordFilePath | ConvertTo-SecureString 
$OnpremPassword=[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR( ($PasswordTemp) ))
$OnpremCtx = New-Object Microsoft.SharePoint.Client.ClientContext($OnPremsiteUrl) 
$OnpremCredentials = New-Object System.Net.NetworkCredential($OnPremLoginName, $OnpremPassword)
$OnpremCtx.Credentials = $OnpremCredentials
Add-Content -Path $LogFilePath -Value "Information : SharePoint onpremise password Declaration and initialization is completed"

Get-CertificationData  
1
  • Still on going issue. Commented Feb 15, 2021 at 17:13

1 Answer 1

0

Your script is using the on-premises PowerShell snapin rather than the SharePoint Online PowerShell module or the PnP PowerShell module. You will need to install one of these modules and use the cmdlets it provides.

1
  • Could you please help us on where(which line) we need to change on-premises Powershell lines to SharePoint Online powershell cmdlets please Commented Feb 11, 2021 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.