Building, integrating, or customizing apps and workflows within Microsoft Teams using developer tools and APIs
Please understand that our forum is a public platform, and we will modify your question to cover the personal information in the description. Kindly ensure that you hide these personal or organization information next time you post error or some information to protect personal data.
Dear @Daniel Browne,
Welcome to Microsoft Q&A Forum!
Thank you for your detailed explanation and for sharing your observations. I understand how confusing this behavior can be, especially when the same script works in one scenario but fails in another.
Based on Microsoft’s official documentation for application‑based authentication in the Teams PowerShell module, I would like to clarify how authorization is evaluated and what may cause the 40301 (“You are not authorized to perform this action”) error you are seeing.
1.How authorization works with app‑only authentication:
When using application‑based (certificate) authentication, authorization is not determined by the local account that runs PowerShell, but by the Microsoft Entra application (service principal) that is used to connect via Connect‑MicrosoftTeams.
2.According to Microsoft’s documentation:
- The Teams PowerShell session uses the directory roles assigned to the application (for example, Teams Administrator or Global Administrator) to configure RBAC.
- Required Microsoft Graph API permissions must also be assigned and admin‑consented, such as
Organization.Read.Allfor *-Cs cmdlets. - If these roles or permissions are missing or incomplete, write operations (such as
New‑CsTeamsMeetingPolicyorSet‑CsTeamsMeetingPolicy) will be blocked, even though some read‑only commands may still work.
Official reference: Application-based authentication in Teams PowerShell Module
3.Why this is important to verify:
If any of the required roles or permissions described in the article were not assigned, the behavior you would typically see is:
- Get‑Cs* cmdlets may succeed
- New‑Cs* / Set‑Cs* cmdlets return 40301 (Forbidden)
- This is expected when the application is not fully authorized.
4.How this applies to your case:
Since you mentioned that the same application, certificate, and script succeed when run in a one‑shot PowerShell process, this strongly suggests that:
- The application does have the required roles and permissions assigned correctly
- The local account running PowerShell is not a factor in authorization
For completeness, I would still recommend confirming that the application configuration fully matches the requirements in the Microsoft article, in particular:
- The application is assigned a Teams Administrator (or Global Administrator) directory role
-
Organization.Read.Allis present under Microsoft Graph API permissions and admin consent has been granted - No unnecessary permissions (such as “Skype and Teams Tenant Admin API”) were added, as this may cause failures
Finally, I would like to give you a summary:
- Authorization is evaluated entirely based on the application’s roles and permissions in Microsoft Entra ID.
- Missing or incomplete application permissions can result in 40301 errors for policy creation or update commands.
- Since your command succeeds in a fresh PowerShell session, this indicates the application configuration is likely correct, and the remaining difference is related to how the session is being executed.
Also, I found a partially similar behavior that you can consult as well: https://help.duo.com/s/article/3893?language=en_US
Note: This information is provided as a convenience to you. This site is not controlled by Microsoft, and Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please ensure that you fully understand the risks before using any suggestions from the above link.
I hope this information can give you some insights in this case and you can also take a look in the answer of Q&A Assist AI to see if they can give you any helpful information.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.