代码之家  ›  专栏  ›  技术社区  ›  iusmar

SiteCore PowerShell报告不返回任何结果

  •  0
  • iusmar  · 技术社区  · 6 年前

    我创建了一个报表,如果我从PowerShellISE手动运行它,它会生成我期望的项目列表,但是当我从报表工具运行它时,它不会返回任何结果。

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

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

    源代码:

    $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:object$all items需要一段时间来填充,SiteCore客户机不会等待后端读取所有项目,因此生成报告时,$global:guityitems始终为空。

    1 回复  |  直到 6 年前
        1
  •  0
  •   iusmar    6 年前

    我找到了解决方案:使用过滤器。而且它也能正常工作。

    $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"}; }
    }