SharePoint Online Powershell Connection (MFA) inside PS Job

Brass Contributor

Hello,

 

I am trying to figure out how to pass a PNP connection to a PowerShell job.

 

When using Connect-PNPOnline -Url "URL" -UseWebLogin nothing it return so the connection cannot be passed around.

I have some PNP cmdlets running inside Start-Job ScriptBlock. 

So instead of running Connect-PNPOnline inside the scriptblock, how can I pass in the connection?

 

1 Reply

@Frederick Wilson 

Use -returnconnection and save the result as something.

$MyWebConnection = connect-pnpOnline -url -interactive -returnconnection

Then you can use the connection switch to 'force' the use of this connection.

$web = get-pnpWeb -connection $MyWebConnection

 

You can pass them to functions as parameters too:

function Do-StuffInAWeb {
    param(
        [Parameter(Mandatory=$true)]   [PnP.PowerShell.Commands.Base.PnPConnection]$PnPWebConnection,    
        [Parameter(Mandatory=$true)]   [string]$ReportPath =$(throw "-ReportPath parameter is required.")
    )