Pnp-powershell: [BUG] Initialize-PnPPowerShellAuthentication fails with "The handle is invalid"

Created on 2 Oct 2020  路  3Comments  路  Source: pnp/PnP-PowerShell

Reporting an Issue or Missing Feature

Issue

Expected behavior

I expect this code to work:

# Configure global variables
$tenant = "<tenant>.onmicrosoft.com"
$spoRoot = "https://<tenant>.sharepoint.com/"
$certificatePassword = "cool-emu2" | ConvertTo-SecureString -Force -AsPlainText

# Initialize the AAD application to access Graph and SPO
$aadApp = Initialize-PnPPowerShellAuthentication `
    -ApplicationName "Heinrich-TeamsProvisioning" `
    -Tenant $tenant `
    -Store CurrentUser `
    -CommonName "TeamsProvisioning" `
    -CertificatePassword $certificatePassword `
    -ValidYears 2 `
    -OutPath 'C:\temp\aad'

This is copied from a blog post from @PaoloPia over here: https://www.collabmagazine.com/provisioning-teams-in-microsoft-teams-using-the-pnp-provisioning-engine/

Actual behavior

I get an error message:

Initialize-PnPPowerShellAuthentica
tion : The handle is invalid.
At line:7 char:11
+ $aadApp = Initialize-PnPPowerShellAuthentication `
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Initialize-PnPPowerShellAuthentication], IOException
    + FullyQualifiedErrorId : System.IO.IOException,PnP.PowerShell.Commands.Base.InitializePowerShellAuthentication

image

(Note: that I am asked for my credentials and the certificate is created on my machine in c:\temp\aad.)

Steps to reproduce behavior

Run above code snippet.

Which version of the PnP-PowerShell Cmdlets are you using?

  • [ ] PnP PowerShell for SharePoint 2013
  • [ ] PnP PowerShell for SharePoint 2016
  • [ ] PnP PowerShell for SharePoint 2019
  • [x] PnP PowerShell for SharePoint Online

What is the version of the Cmdlet module you are running?

3.25.2009.1

How did you install the PnP-PowerShell Cmdlets?

  • [ ] MSI Installed downloaded from GitHub
  • [x] Installed through the PowerShell Gallery with Install-Module
  • [ ] Other means
bug

All 3 comments

I'm also seeing this issue. Same version.

It does appear to have created the app registration and the certificate ok so expect it will work.

This issue occurs when running a script from the PowerShell ISE. We have just released a new version of PnP PowerShell (classic/for windows) which fixes this issue. We strongly advice you to move to VSCode for PowerShell Scripts as the PowerShell ISE is not actively maintained anymore : https://docs.microsoft.com/en-us/powershell/scripting/windows-powershell/ise/introducing-the-windows-powershell-ise?view=powershell-7#:~:text=The%20PowerShell%20ISE%20is%20no,in%20PowerShell%20v6%20and%20beyond

Hi All.
The script needs to be run in Windows PowerShell, not in PowerShell ISE.
Had the same situation and when run in PS - it finished without errors and created the app.
Best!

Was this page helpful?
0 / 5 - 0 ratings