Sitecore Powershell 报告没有 return 任何结果

Sitecore Powershell reports does not return any results

我创建了一个报告,如果我从 Powershell ISE 手动 运行 它,它会生成我期望的项目列表,但是当我从报告工具 运行 它 returns 没有结果。

脚本会抓取所有项目版本和语言,大约有 80,000 个项目,这需要一段时间。

有没有办法在生成所有项目的列表之前添加延迟,或者有任何其他解决方法?

源代码:

$RichTextContentID = "";
$internalLinkPattern = '<a href="~\/link\.aspx\?_id=(?<sitecoreid>[a-zA-Z\d]{32})&amp;_z=z">';
$literatureTemplate = "";
$global:guiltyItems = @();

function Process-RichText
{
    param(  [Parameter(Mandatory = $true)] [Sitecore.Data.Fields.Field]$field,
            [Parameter(Mandatory = $true)] [string]$pattern,
            [Parameter(Mandatory = $true)] [Sitecore.Data.Items.Item]$item)

    $allMatches = [System.Text.RegularExpressions.Regex]::Matches($field.Value,$pattern);
    foreach ($match in $allMatches)
    {
        $currentItem = Get-Item master -Id ([Sitecore.Data.ID]::Parse($match.Groups["sitecoreid"].Value)).Guid;

        if ($currentItem.Template.Id -eq $literatureTemplate)
        {   
            if ($global:guiltyItems -notcontains $item)
            {
                $global:guiltyItems += $item;
            }
        }
    }
}

$allitems = Get-Item master -Query "/sitecore/content/MyWebsiteTree//*" -Language * -Version *; 
foreach ($item in $allItems) {
    foreach ($field in $item.Fields)
    {
        if ($field.Id -eq $RichTextContentID -and ($field.Value -match $internalLinkPattern))
        {
           Process-RichText $field $internalLinkPattern $item;
        }
    }
}


if ($global:guiltyItems.Count -eq 0) {
        Show-Alert "Did not find any items to match your condition.";
    } 
else {
    $props = @{
        Title = ""
        InfoDescription = ""
        PageSize = 50
    };

    ($global:guiltyItems) |
        Show-ListView @props -Property @{ Label="Item name"; Expression={$_.Name}; },
            @{ Label="ID"; Expression={$_.ID}; },
            @{ Label="Display name"; Expression={$_.DisplayName}; },
            @{ Label="Language"; Expression={$_.Language}; },
            @{ Label="Version"; Expression={$_.Version}; },
            @{ Label="Path"; Expression={$_.ItemPath}; },
            @{ Label="Created"; Expression={$_.__Created}; },
            @{ Label="Created by"; Expression={$_."__Created by"}; },
            @{ Label="Updated"; Expression={$_.__Updated}; },
            @{ Label="Updated by"; Expression={$_."__Updated by"}; }
}

Close-Window;

谢谢

LE:对象 $allitems 需要一段时间才能填充,并且 sitecore 客户端不会等待后端读取所有项目,因此当我生成报告时,$global:guiltyItems 始终为空。

我找到了解决办法:使用过滤器。它按预期工作。

$RichTextContentID = "";
$internalLinkPattern = '<a href="~\/link\.aspx\?_id=(?<sitecoreid>[a-zA-Z\d]{32})&amp;_z=z">';
$literatureTemplateID = "";

$root = Get-Item -Path "master:/sitecore/content/MyWebsite";

filter Where-HasLiterature{
    param([Parameter(Mandatory=$TRUE,ValueFromPipeline=$TRUE)][Sitecore.Data.Items.Item]$item)

    if($item)
    {
        foreach ($field in $item.Fields)
        {
            if ($field.Id -eq $RichTextContentID -and ($field.Value -match $internalLinkPattern))
            {
                $allMatches = [System.Text.RegularExpressions.Regex]::Matches($field.Value,$internalLinkPattern);

                foreach ($match in $allMatches)
                    {
                        $guiltyItem = Get-Item "master:" -Id ([Sitecore.Data.ID]::Parse($match.Groups["sitecoreid"].Value)).Guid;
                        $guiltyItemTemplate = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($guiltyItem);

                        if ($guiltyItem -ne $null -and $guiltyItemTemplate.DescendsFromOrEquals($literatureTemplateID) )
                        {
                            $item;
                        }
                    }   

            }

        }
    }
}

$items = Get-ChildItem -Path $root.ProviderPath -Recurse | Where-HasLiterature

if ($items.Count -eq 0) 
{
    Show-Alert "Did not find any items to match your condition.";
} 
else 
{
    $props = @{
        Title = ""
        InfoDescription = ""
        PageSize = 50
    }

    $items | Show-ListView @props -Property @{ Label="Item name"; Expression={$_.Name}; },
            @{ Label="ID"; Expression={$_.ID}; },
            @{ Label="Display name"; Expression={$_.DisplayName}; },
            @{ Label="Language"; Expression={$_.Language}; },
            @{ Label="Version"; Expression={$_.Version}; },
            @{ Label="Path"; Expression={$_.ItemPath}; },
            @{ Label="Created"; Expression={$_.__Created}; },
            @{ Label="Created by"; Expression={$_."__Created by"}; },
            @{ Label="Updated"; Expression={$_.__Updated}; },
            @{ Label="Updated by"; Expression={$_."__Updated by"}; }
}