Forum Discussion
If that checks SiteUrl value exists in array using PnP
I'm trying to add items to an SPO list only if the item is not there already:
$ListItems = Get-PnPListItem -List $List
ForEach($Hub in $HubSites)
{
$HubSiteId = $Hub.ID.GUID
ForEach($Site in $Sites)
{
$AssociatedHubSiteID = $Site.HubSiteId.Guid
If ( ($AssociatedHubSiteID -eq $HubSiteId) -and ($Hub.SiteUrl -ne $Site.Url) )
{
$ReportEntry = "" | Select-Object Hub,SiteUrl
$ReportEntry.Hub = $Hub.SiteUrl
$ReportEntry.SiteUrl = $Site.Url
$Report += $ReportEntry
#$item = $ListItems | Where-Object { $_.FieldValues.SiteUrl -eq $Site.Url }
#if ($Site.Url -ne $item[0].FieldValues.SiteUrl) {
if ($Site.Url -notin $ListItems.FieldValuesAsText.SiteUrl) {
Add-PnPListItem -List $List -Values @{"Hub" = $Hub.SiteUrl; "SiteUrl" = $Site.Url}
}
If think the issue may be with line 16? I've left in my attempts as comment outs on line 14 and 15.
12 Replies
- LainRobertsonSilver Contributor
Hi, Thomas.
If we run with your assumption that the issue is with line 16, then -notin is probably not the appropriate operator. Try the following instead:
if ($ListItems.FieldValuesAsText.SiteUrl -notcontains $Site.Url) {Some diagnostic information that would help is:
- The results from running:
$ListItems.FieldValuesAsText.SiteUrl.pstypenames;
- The results from running:
$ListItems.FieldValuesAsText.SiteUrl | Select-Object -First 5;
Basically, I'm looking for some type information and sample data given I don't work with SharePoint and cannot replicate your tests.
Cheers,
Lain- LainRobertsonSilver Contributor
As a quick clarification after having gone and fact-checked myself on what I thought I remembered about -notin (where I thought it only behaved against collections), it turns out I recalled incorrectly as it works against arrays as well. This means the suggestion I made about -notcontains likely won't make any difference.
That said, the diagnostic commands are still useful since I have no idea what the types and example data look like.
Cheers,
Lain
- CardinalNightBrass Contributor
Hi, how would enter these type checks in the script? Can you provide please. When I run one of the provided lines, then run $listItems it just shows:
Id Title GUID
-- ----- ----
280 6185d296-1919-4179-g654-02ae6bcec4d9
281 1a4efa4f-9f82-4543-b2345-26f459ec0b45
etc....
- The results from running: