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

%3CLINGO-SUB%20id%3D%22lingo-sub-1106405%22%20slang%3D%22en-US%22%3EConnect-PnPOnline%20%3A%20Not%20working%20if%20I%20execute%20it%20through%20a%20.ps1%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1106405%22%20slang%3D%22en-US%22%3E%3CP%3EAll%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20.ps1%20file%20that%20has%20this%20cmdlet%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EConnect-PnPOnline%20%E2%80%93Url%20%24siteURL%20%E2%80%93Credentials%20(Get-Credential)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20getting%20the%20below%20error%20after%20executing%20it%3C%2FP%3E%3CP%3E%3CFONT%20face%3D%22courier%20new%2Ccourier%22%3EA%20positional%20parameter%20cannot%20be%20found%20that%20accepts%20argument%20'System.Management.Automation.PSCredential'.%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20face%3D%22courier%20new%2Ccourier%22%3E.....%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20face%3D%22courier%20new%2Ccourier%22%3E%2B%20CategoryInfo%20%3A%20InvalidArgument%3A%20(%3A)%20%5BConnect-PnPOnline%5D%2C%20ParameterBindingException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20PositionalParameterNotFound%2CSharePointPnP.PowerShell.Commands.Base.Connec%3CBR%20%2F%3EtOnline%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20if%20I%20am%20executing%20the%20cmdlet%20directly(i.e.%2C%20typing%20the%20command)%20in%20the%20shell%20then%20there%20are%20no%20errors.%20It%20connects%20to%20the%20site.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20with%20below%20code%20too%20in%20.ps1%20file%2C%20failed%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%24encpassword%20%3D%20convertto-securestring%20-String%20%24password%20-AsPlainText%20-Force%0A%24cred%20%3D%20New-Object%20-typename%20System.Management.Automation.PSCredential(%24username%2C%20%24encpassword)%0AConnect-PnPOnline%20%E2%80%93Url%20%24siteURL%20%E2%80%93Credentials%20%24cred%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20is%20going%20on%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EVersion%20check%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EName%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Version%0A----%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-------%0ASharePointPnPPowerShellOnline%202.27.1806.1%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1106405%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%20Commands%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
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