0

I created a powershell script to connect to my sharepoint site online. Having enabled two-factor authentication, I set a password for the app for authentication and used it in the Connect-PnpOnline command.

$securePassword = ConvertTo-SecureString "myappapassword" -AsPlainText -Force
$credentials = New-Object PSCredential ("my@username.com", $securePassword)

Connect-PnPOnline -Url $TenantSiteURL$SiteRelativeURL -Credentials $credentials

On the local computer I don't get any errors, but when I try to run it in Azure Function I get:

 Error validating credentials due to invalid username or password
blacky99
  • 115
  • 1
  • 12
  • You can refer to [Web Login for Multi Factor Authentication](https://pnp.github.io/powershell/cmdlets/Connect-PnPOnline.html#web-login-for-multi-factor-authentication) and [Interactive for Multi Factor Authentication](https://pnp.github.io/powershell/cmdlets/Connect-PnPOnline.html#interactive-for-multi-factor-authentication) – Ecstasy Jan 05 '22 at 09:57
  • 1
    I cannot use web login because the script is hosted in azure function. Running the script on the local machine all methods work (both with credentials and weblogin) – blacky99 Jan 05 '22 at 10:12
  • You can refer to a similar GitHub issue: [Cannot connect with credentials](https://github.com/pnp/powershell/issues/61) – Ecstasy Jan 05 '22 at 10:20
  • Thanks for the info. I have read the discussion carefully but couldn't quite figure out how to fix the problem. Checking I have the latest version of PnpOnline. How can I authenticate my app? I have read that there is a way to use Managed Identity but how can I? – blacky99 Jan 05 '22 at 10:47

1 Answers1

0

Below are the few workaround to solve the above issue:

  • Please make sure that you have uploaded your pnp powershell module to Azure function . By Navigating to Azure function >Advance tool>Kudu console> wwwroot folder > Create new folder copy and paste your modules that have in your local.

  • Add your service account user name and password in configuration of Azure function.

  • Try to replace the following cmd in your cmdlt:

$Credential = New-Object System.Management.Automation.PSCredential($serviceAccountEmail, $SecurePassword)

  • To use managed identity you can refer this Blog

For more information please refer the below links :

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15
  • I get: ERROR: A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Exception : Type : System.Management.Automation.Host.HostException ErrorRecord : Exception : Type : System.Management.Automation.ParentContainsErrorRecordException – blacky99 Jan 11 '22 at 15:09
  • Hello @blacky99 , For the above error could you please refer this [SO THREAD](https://stackoverflow.com/questions/24290813/how-can-i-execute-scripts-in-a-code-created-powershell-shell-that-has-write-host) and [MS DOC](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.host.promptingexception?view=powershellsdk-7.0.0) – AjayKumarGhose Jan 12 '22 at 05:07