pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
985 stars 665 forks source link

[BUG] Initialize-PnPPowerShellAuthentication fails with "The handle is invalid" #2947

Closed heinrich-ulbricht closed 3 years ago

heinrich-ulbricht commented 3 years ago

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?

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

3.25.2009.1

How did you install the PnP-PowerShell Cmdlets?

andypturner commented 3 years ago

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.

erwinvanhunen commented 3 years ago

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

RS-GHub commented 3 years ago

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!