r/PowerShell • • Jan 26 '22

Solved Best way to use Microsoft Graph API without showing secret key?

I've been following along The Lazy Administrator's Guide. I'm fairly new to all this, and I am stuck with the method to connect to Graph. I have the IDs and Secret Key, but not sure how I can add it to a script without revealing what those keys are. Based on the guide, I think the best one to use is the Client Credentials, but again not sure how to "encrypt the client secret, store it in Azure Key Vault".

For context, I am learning how to use Graph API so I can send emails from a script I made in Powershell.

9 Upvotes

31 comments sorted by

View all comments

Show parent comments

2

u/[deleted] Jan 26 '22

I'm dumb lol. Ok, so I created the cert and pfx, but now I'm having an issue on how to use it. I tried to do a quick test and used a connect-exchangeonline -certificateFilePath "path to the pfx file" -AppID"appid" -Organization "that onprod one" -CertificatePassword (convertTo-SecureString -force -AsPlainText "the password I made when I exported the cert")

Then it throws me an error saying permission denied. What did you do after creating the cert to be able to use the Graph API in Powershell?

2

u/Skaixen Jan 26 '22

You're not using Graph, if your using the cmdlet, connect-exchangeonline

https://docs.microsoft.com/en-us/graph/powershell/app-only?tabs=azure-portal

2

u/[deleted] Jan 26 '22

OK, I think I understand. So how would I produce the token using the certificates so I can send an email from a powershell script located on a client endpoint that has the .pfx cert locally? I saw some posts about invoking tokens, but it was using the client secret and not a token.