web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Run a PowerShell scrip...
Power Automate
Unanswered

Run a PowerShell script using a service account

(0) ShareShare
ReportReport
Posted on by 84

Currently, I am looking to run a PowerShell script using Power Automate Desktop that will run through a service account. The service account is used so that if I or another member of tech leaves, the flows will live on. 

 

I am currently in the beginning stage of creating a flow that will executive a pretty complex script but the first part is connecting to specific modules without having to use MFA. The account currently does not use MFA as it is not part of out OKTA and only has an Azure/Exchange account with a password that never expires. 

 

The beginning of the script doesn't seem to connect to the required modules and goes as follows:

 

******************
#Arguments for connectivity
$User = "ServiceAccountAddressThatEndsWith.onmicrosoft.com"
$JPasswd = ConvertTo-SecureString -String "PASSWORD" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $JPasswd


#connect to AzureAD service
write-host "Connecting to Exchange module"
Connect-ExchangeOnline -Credential $credential -ShowBanner:$false

*********************

 

I have added a simple GetMailboxPermissions command to the end of this and tried to run and it does not return any Outputs. When I run this on my personal desktop, I am able to connect to the Module and it does return the get mailbox permissions command properly. The issue seems to be when being ran with PAD. 

 

Any guidance on this would be appreciated! 

I have the same question (0)
  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Are you actually making the script return anything? You need a return statement in it for it to return anything to the Output variable.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • DH2023 Profile Picture
    84 on at

    Hello,

     

    After connecting to the module, I have tried to simply return who has permissions to someone's mailbox using the following and then using the PowerShell Output and displaying it in a Display Message Box. 

    Get-MailboxPermission -Identity internal_user@test.com | Select-Object User, AccessRights

     

    This is resulting in:

    "Get-MailboxPermission : The term 'Get-MailboxPermission' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

     

    Additionally, I have tried to run a command to grant mailbox permissions to someone. Neither seem to have worked nor returned anything. 

     

    Thank you,

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Have you installed the ExchangeOnlineManagement module? https://www.powershellgallery.com/packages/ExchangeOnlineManagement/3.4.0

  • DH2023 Profile Picture
    84 on at

    Hello Agnius,

     

    Yes, I have all the required modules installed on my machine. I am finally at a stage to move all my existing working scripts over to a service account for various reasons. 

     

    My first thought as to why this is not working was that the service account doesn't have a desktop/local machine as it is a cloud account. Not sure if I would need to download PAD on a server environment to be able to interact with the account? 

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    I don't think using PAD on a server machine would change much, especially since you're connecting to Exchange Online anyway. But does this work for a different (non-service) account? If it does, it might just be some service account limitations. Also, is the service account actually licensed to use Exchange Online?

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • DH2023 Profile Picture
    84 on at

    Hello, It appears using the provided commands, I am not able to connect to the Exchange server using my own credentials. I am receiving the following error:

    Error Acquiring Token:
    XXXXXXXXX: Error validating credentials due to invalid username or password. Trace ID: XXXXXXXX Correlation ID: XXXXXXXXXXXXX Timestamp: 2023-11-16 20:46:01Z

    XXXXXXXXXX: Error validating credentials due to invalid username or password.

     

    It appears that I am able to connect using this short script when running this on my local PowerShell though. 

     

    Thanks,

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 248 Super User 2026 Season 2

#2
trice602 Profile Picture

trice602 160 Super User 2026 Season 2

#3
11manish Profile Picture

11manish 142 Super User 2026 Season 2

Last 30 days Overall leaderboard