Connect-PnPOnline : Not working if I execute it through a .ps1 file

Brass Contributor

All, 

 

I have a .ps1 file that has this cmdlet

 

Connect-PnPOnline –Url $siteURL –Credentials (Get-Credential)

 

 

I'm getting the below error after executing it

A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'.

.....

+ CategoryInfo : InvalidArgument: (:) [Connect-PnPOnline], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,SharePointPnP.PowerShell.Commands.Base.Connec
tOnline

 

But if I am executing the cmdlet directly(i.e., typing the command) in the shell then there are no errors. It connects to the site. 

 

I tried with below code too in .ps1 file, failed

 

$encpassword = convertto-securestring -String $password -AsPlainText -Force
$cred = New-Object -typename System.Management.Automation.PSCredential($username, $encpassword)
Connect-PnPOnline –Url $siteURL –Credentials $cred

 

 

What is going on?

 

Version check

Name                          Version
----                          -------
SharePointPnPPowerShellOnline 2.27.1806.1
0 Replies