grahamr975 / EWS-Office365-Contact-Sync

Uses Exchange Web Services to synchronize a Global Address List in Office 365 to a user's mailbox
MIT License
94 stars 21 forks source link

Cannot convert the "System.Management.Automation.PSCredential #66

Closed S-Mellado closed 1 year ago

S-Mellado commented 1 year ago

I have problem when I execute .bat, the error message said Cannot convert the "System.Management.Automation.PSCredential" value of type "System.Management.Automation.PSCredential" to type "System.Security.SecureString". I think my problem is with the certificates in my .bat (certificatepath and certificatepasswordpath):

image

How can I generate or from where I generate the .pfx and .cred? I use the Create-SecureCertificatePassword.ps1, but... I have a mistake.

image

Thanks for your support!

Manuel-Yotta commented 1 year ago

Hi, I have the same problem. How can I fix it? Thanks for your support!

S-Mellado commented 1 year ago

Hi, review Get-GALContacts.ps1 script and in the line Connect-ExchangeOnline -ConnectionUri $ConnectionUri -CertificateFilePath $CertificatePath -CertificatePassword $CertificatePassword -AppId $ClientID -Organization $ExchangeOrg add this parameter -UseRPSSession like this: Connect-ExchangeOnline -UseRPSSession -ConnectionUri $ConnectionUri -CertificateFilePath $CertificatePath -CertificatePassword $CertificatePassword -AppId $ClientID -Organization $ExchangeOrg

Let me know if all is ok.

Manuel-Yotta commented 1 year ago

Hi and thanks, unfortunately it does not change

immagine

S-Mellado commented 1 year ago

Do you review the permission on the azure app? image

Manuel-Yotta commented 1 year ago

Yes:

immagine

S-Mellado commented 1 year ago

Try this. Change the .bat by powershell. Forget .bat. Open powershell and call to your ps1. See txt Script_Contacts_To_One_Mailbox.txt

Manuel-Yotta commented 1 year ago

Try this. Change the .bat by powershell. Forget .bat. Open powershell and call to your ps1, like this:

$CertificatePath = "C:\Admin\Scripts\Contacts_New\Certificados\Certificate.pfx" $CertificatePasswordPath = "C:\Admin\Scripts\Contacts_New\Getting Started\SecureCertificatePassword.xml" $ClientID = "CLientID" (write your client id) $FolderContacName = "FolderName" $LogPath = "%~dp0Logs" $ExchangeOrg = "contoso.onmicrosoft.com" function Get-Buzon { param ( MailBox=( Write-Host ' ' Write-Host '* SCRIPT CONTACTOS ** ' Write-Host ' ' Write-Host 'Script para desplegar los contactos de la compañía sobre el buzón de un usuario ' Write-Host 'Se guardarán en una carpeta de los contactos del usuario llamada Deoleo Mobile' Write-Host ' ' Write-Host '*** ' Write-Host ' ' Write-Host 'Introduzca el buzón del usuario: '-ForegroundColor Yellow -NoNewLine Read-Host ) ) $MailBox } $MailBox = Get-Buzon

C:\Admin\Scripts\Contacts_New\EWSContactSync.ps1 -CertificatePath $CertificatePath -CertificatePasswordPath $CertificatePasswordPath -ClientID $ClientID -FolderName $FolderContacName -LogPath $LogPath -MailboxList $MailBox -ExchangeOrg $ExchangeOrg -ModernAuth**

I'm sorry but I didn't understand how to proceed

grahamr975 commented 1 year ago

@Manuel-Yotta Can you try to change your system language to English so see if this resolves your issue? I've not tested the script in other languages.

Manuel-Yotta commented 1 year ago

@Manuel-Yotta Can you try to change your system language to English so see if this resolves your issue? I've not tested the script in other languages.

It is the same

immagine

grahamr975 commented 1 year ago

@Manuel-Yotta @S-Mellado

It looks like you exported your certificate password using "Get-Credential". You need to instead use "Read-Host -AsSecureString" to export the password. See below for an example. I think this will resolve your issue.

# interactively prompt for the certificate password as a secure string
$SecureString = Read-Host -AsSecureString

# export the secret string object to a file
$SecureString | Export-Clixml .\SecureCertificatePassword.cred
Manuel-Yotta commented 1 year ago

# interactively prompt for the certificate password as a secure string

perfect, I confirm that this solution is decisive. Thanks so much everyone for the support and congratulations again for the script!!!

grahamr975 commented 1 year ago

Reported as solved, closing issue...