Connect-SPOService : The 'username' argument is invalid

Hello to everyone in Powershell.org,

Intro:
I’m searching for a way to upload files towards sharepoint from our Azure DevOps Services Pipeline, the code throughs the error “Connect-SPOService : The ‘username’ argument is invalid

The Code

$username = 'MyUser'
$password = 'MyPassword'
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force)
Connect-SPOService -Url https://<MyOrg>.sharepoint.com/teams/<MyTeam> -Credential $cred


$Files = Get-ChildItem "C:FolderName\*.*"
foreach($File in $Files){
        Add-PnPFile -Folder "Shared Documents/MyFolder" -Path $File.FullName
}

Am I using the wrong argument?
Is there a better way to authenticate towards Sharepoint (because is a Pipeline Human intervention via “prompt” dialog box isn’t an option)

Humble ask for your help community,

Hi, welcome to the forum :wave:

How are you specifying the username? I think it needs to be myuser@contoso.com rather than myuser.

You could use Azure Key Vault:

or the new SecretManagement and SecretStore modules:

Hi @matt-bloomfield thanks for taking the time to reply,

Regarding the “user value” it is an email like shown below:
$username = ‘MyUser@Email.com’
Is that what you were referring to? I mean that the user variable “value” should be in the form of an “email”?
Or is some different value I need to retrieve from a certain “Sharepoint” configuration?
As a reference that email is my corporate one and has full control under that SharePoint Space

I’m new regarding Sharepoint administration so perhaps I’m missing tons of context you already manage

Yes, your example didn’t show it as an e-mail address so I thought that might be the problem.

It might be worth removing the $ from the argumentlist parameter. I don’t think it will cause a problem but most examples show just the parentheses.

Also, for troubleshooting, it might be worth just using

Connect-SPOService -Url https://<MyOrg>.sharepoint.com/teams/<MyTeam> -Credential myuser@email.com

and using the prompt for the password to test the credentials are OK.

Hi Matt,

Readed your great explanation and placed the code int the next way:

$cred = New-Object -TypeName System.Management.Automation.PSCredential -Credential 'MyUser@Email.com'
Connect-SPOService -Url https://<MyOrg>.sharepoint.com/teams/<MyTeam> -Credential $cred


$Files = Get-ChildItem "C:FolderName\*.*"
foreach($File in $Files){
        Add-PnPFile -Folder "Shared Documents/MyFolder" -Path $File.FullName
}

New Error:
New-Object : A parameter cannot be found that matches parameter name ‘Credential’

I wonder if there is other way to pass the credential towards ‘New-Object -TypeName System.Management.Automation.PSCredential’?

Sorry, that’s not what I meant to post! I have edited it to what it should be, copied below:

Connect-SPOService -Url https://<MyOrg>.sharepoint.com/teams/<MyTeam> -Credential myuser@email.com

Hi @matt-bloomfield, you are a genius I’ve been reading testing more and, I think my best option is to use “KeyVault” why? well because I cannot be delivering credentials through a “Prompt” because this will run in a Pipeline,

Could you help me with which command sentence/syntax should I use to call the “User” and Password store in KeyVault?

Super thanks in advance!

KeyVault is something I’m aware of but have not used myself and I’ve only recently starting used Azure Devops so I’m pretty new to the whole pipeline thing too.

Have a look at this tutorial, it looks like it will take you through all the necessary steps: