0

I'm trying to run a powershell script with the Windows task scheduler. The script works perfectly when I run it manually. But receive the following error when executed (with highest privileges) in the Windows task scheduler:

Error while connecting to site 'https://####':The term 'Connect-PnPOnline' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

I've reinstalled the Pnp PowerShell assembly from here: https://github.com/SharePoint/PnP-PowerShell/releases and verified if the modules are installed correctly with:

Get-Module SharepointPNPPowershellOnline
1
  • import-module in the beginning of the script? As i presume that you do not use your own account to run the scheduled task. Jun 23, 2020 at 10:20

2 Answers 2

1

Try to execute the same script from the PowerShell ISE or PowerShell command prompt.... if it works it's fine, then some problems on windows task scheduler. And if it doesn't work then PnP PowerShell model did not get installed properly, then try to reinstall the PnP PowerShell module again.

There are many ways we can install the PnP PowerShell for SharePoint online - the simplest way to run the below command in your machine:

(New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/sharepoint/PnP-PowerShell/master/Samples/Modules.Install/Install-SharePointPnPPowerShell.ps1')

For details prerequisites and installation refer to the below article:

Office 365: Getting started with SharePoint PnP PowerShell – installation

2
  • The question is clearly not about how to install the PnP PowerShell module. Jun 23, 2020 at 10:21
  • @Christoffer - It looks strange to see downvoting, if we install the PnP PowerShell module properly we don't need to include the "import-module" in the beginning of the script - I do execute plenty of PnP scripts on daily basis, I don't include "import-module", still it works. That is the reason I have added how to install the PnP PowerShell module.
    – SP 2022
    Jun 23, 2020 at 10:41
0

This is more than likely an issue with the version of PNP.Powershell that you are using. The first thing you want to do is issue this command: Get-module -ListAvailable I had the same issue as well, when I downgraded to version 1.12, It solved the issue. So in a nutshell, run the following commands (one at a time) Uninstall-Module -Name PnP.PowerShell -AllVersions -Force Then run Install-Module PnP.PowerShell -RequiredVersion 1.12 -Force

Also, look at the way you are trying to run the command as well, Refer to this link: Working with a basic task

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.