I have a PowerShell script that creates an Office 365 group and configures the associated SharePoint site. During the creation process it tries to add a link on the site's quick launch using the following code
$ctx = Get-PnPContext;
$finalizeLinkAdded = $false;
try {
$web = $ctx.Web
$navColl = $web.Navigation.QuickLaunch
$ctx.Load($navColl) ;
$ctx.ExecuteQuery();
$newNavNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
$newNavNode.Title = "Link Title"
$newNavNode.Url = $link
$newNavNode.AsLastNode = $true
$navColl.Add($newNavNode) | out-null;
$web.Update();
$ctx.ExecuteQuery();
$finalizeLinkAdded = $true;
}
catch {
write-host "$((get-date).toString()) Error adding navigation link $($_.toString())";
}
This works well when I'm runnig the script from a PowerShell console. However, this script is run by creating a job
$job = Start-Job -InitializationScript { d:\powershell\engagements\assemblies.ps1}
-ScriptBlock { d:\powershell\create-group.ps1 }
When I do it this way, I get this strange error.
Cannot convert argument "parameters", with value:
"Microsoft.SharePoint.Client.NavigationNodeCreationInformation", for "Add" to type
"Microsoft.SharePoint.Client.NavigationNodeCreationInformation":
"Cannot convert the "Microsoft.SharePoint.Client.NavigationNodeCreationInformation"
value of type "Microsoft.SharePoint.Client.NavigationNodeCreationInformation" to
type "Microsoft.SharePoint.Client.NavigationNodeCreationInformation"."
I don't understand why this doesn't work when the script is run within a start-job scriptblock. What am I doing wrong?