Automate a PowerShell script that updates information on all SharePoint sites.

IniobongNkanga-8038 931 Reputation points
2025-01-09T18:32:36.0466667+00:00

Hello

Please i need your help on this issue.

My IT department has a few PowerShell scripts we are currently using to mass update various settings and user information in our SharePoint sites. Outside of running them using Task Scheduler, is there a way to automate them using Power Automate or something similar?

As a quick side question, is there a way to avoid the authentication that occurs when using Connect-SPOService?

I've tried passing in the admin credential, but receive the following error when I do:

Connect-SPOService : One or more errors occurred.

At C:\Users\Kperry\Documents\defaultopen.ps1:46 char:1

  • Connect-SPOService -URL $AdminSiteURL -Credential $cred

[Connect-SPOService], AggregateException

  • FullyQualifiedErrorId : System.AggregateException,Microsoft.Online.SharePoint.PowerShell.ConnectSPOService

The only way I've had the script succeed is by not passing a credential into the function. Which means I then have to manually select my login credential from the sign-in popup that appears. Without a solution to this, I feel that automating the script would be a bit pointless (as I will have to manually sign in when it runs).

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,191 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ling Zhou_MSFT 20,395 Reputation points Microsoft Vendor
    2025-01-10T06:35:58.2766667+00:00

    Hi @IniobongNkanga-8038,

    Thanks for reaching out to us. We are very pleased to assist you.

    First of all, there is the question about the automatic execution of PowerShell scripts, we can use Azure Automation to run your PowerShell scripts. Azure Automation provides a robust platform for automating tasks across Microsoft 365, including SharePoint.

    Since our team is primarily focused on SharePoint, we have provided some links to the use of Azure Automation for your reference and learning:

    Second, regarding not popping up the login box when using the script, there are three ways to do this.

    Please note in advance:

    • Method 1 use Connect -SPOService, but not for tenants with MFA turned on. The Connect -SPOService command has to pop up a login popup for MFA logins. User's image Reference: Connect-SPOService#example-3
    • Methods 2 and 3 use Connect-PnPOnline, which also works for tenants with MFA turned on. But I am afraid your scripts need to be changed to apply the PnP PowerShell commands.

    Method 1: Write the username and password in plaintext in the script. However, this method is not suitable for tenants with MFA enabled.

    $username = "admin@contoso.onmicrosoft.com"
    $password = "password"
    $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force)
    Connect-SPOService -Url https://contoso-admin.sharepoint.com -Credential $cred
    

    Method 2: Connect-PnPOnline using ClientID and ClientSecret

    Please refer this document: PnP PowerShell: Connect-PnPOnline using ClientID and ClientSecret.

    Note: Non-official, just for reference.

    Method 3: Connect to SharePoint Online using Azure AD App ID from PowerShell

    Please refer this document: PnP PowerShell: Connect to SharePoint Online using Azure AD App ID from PowerShell.

    Note: Non-official, just for reference.

    If you have any questions, please do not hesitate to contact me.

    Moreover, if the issue can be fixed successfully, please click "Accept Answer" so that we can better archive the case and the other community members who are suffering the same issue can benefit from it.

    Your kind contribution is much appreciated.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.