问题
(Basic desire is to cascade delete files. Also, use the same concept to recurse copy files to SharePoint)
How do you pass a SharePoint connection into a PowerShell job.
If I use Connect-PNPOnline -ReturnConnection
and then pass the variable into the Job as an argument I get the following error.
System.Management.Automation.ParameterBindingException: Cannot bind parameter 'Connection'. Cannot convert the "SharePointPnP.PowerShell.Commands.Base.SPOnlineConnection" value of type "Deserialized.SharePointPnP.PowerShell.Commands.Base.SPOnlineConnection" to type "SharePointPnP.PowerShell.Commands.Base.SPOnlineConnection". ---> System.Management.Automation.PSInvalidCastException: Cannot convert the "SharePointPnP.PowerShell.Commands.Base.SPOnlineConnection" value of type "Deserialized.SharePointPnP.PowerShell.Commands.Base.SPOnlineConnection" to type "SharePointPnP.PowerShell.Commands.Base.SPOnlineConnection".
at System.Management.Automation.LanguagePrimitives.ThrowInvalidCastException(Object valueToConvert, Type resultType)
at System.Management.Automation.LanguagePrimitives.ConvertNoConversion(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable)
at System.Management.Automation.LanguagePrimitives.ConversionData`1.Invoke(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable)
at System.Management.Automation.LanguagePrimitives.ConvertTo(Object valueToConvert, Type resultType, Boolean recursion, IFormatProvider formatProvider, TypeTable backupTypeTable)
at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue)
--- End of inner exception stack trace ---
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Without passing in the argument I get the following error.
System.InvalidOperationException: No connection, please connect first
with Connect-PnPOnline at
SharePointPnP.PowerShell.Commands.PnPCmdlet.BeginProcessing() at
SharePointPnP.PowerShell.Commands.PnPWebCmdlet.BeginProcessing() at
System.Management.Automation.Cmdlet.DoBeginProcessing() at
**strong text**System.Management.Automation.CommandProcessorBase.DoBegin()
I understand that PowerShell jobs work in their own scope but I just cannot figure out how to pass in the connection information.
Here is my code snippet
Clear-Host
$z=$files.Count
Foreach($file in $files){
$jobs = @(Get-Job -State Running)
If($jobs.Count -le 15){
$null=$jobs|Wait-Job -Any
}
$SB={
$SBListName = $args[0]
$FileID = $args[1]
$FileName = $args[2]
$LogPath=$args[3]
Try{
Remove-PnPListItem -Connection $args[4] -list $SBListName -Identity $FileID -Force -ErrorAction Stop
$LogStatus = 'COMPLETE'
$LogMessage = 'File Removed'
} catch {
$LogStatus = 'ERROR'
$LogMessage = $_.exception
} Finally {
# Log action
$line = [pscustomobject]@{
'DateTime' = (Get-Date)
'Item Type'="File"
'Item ID'=$FileID
'Item Name'=$FileName
'Message' = $LogMessage
'Status' = $LogStatus
}
$line | Export-Csv -Path $LogPath -Append -NoTypeInformation
}
}
$fileName = $File.fieldValues['FileLeafRef']
$FileID = $file.Id
Write-Host "($z) - $fileName"
$null=Start-Job -Name $fileName -ScriptBlock $sb -ArgumentList $SPOListName, $FileID, $fileName, $SBLogPath, $connection
$z--
}
来源:https://stackoverflow.com/questions/57412621/powershell-jobs-with-sharepoint-connection