Last active
February 10, 2017 07:30
-
-
Save daniiiol/143db3e2004afe9a55c1dd3e33048940 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Lists the items with broken external links in the current language. | |
.NOTES | |
Daniel Scherrer | |
Adapted from the Broken Links Checker Report by Michael West. | |
#> | |
$database = "master" | |
$root = Get-Item -Path (@{$true="$($database):\content\home"; $false="$($database):\content"}[(Test-Path -Path "$($database):\content\home")]) | |
$props = @{ | |
Parameters = @( | |
@{Name="root"; Title="Choose the report root"; Tooltip="Only items in this branch will be returned."; Columns=9} | |
) | |
Title = "Broken External Links Report" | |
Description = "Choose the criteria for the report." | |
Width = 550 | |
Height = 300 | |
ShowHints = $true | |
} | |
$result = Read-Variable @props | |
if($result -eq "cancel"){ | |
exit | |
} | |
$items = Get-ChildItem -Path $root.ProviderPath -Recurse | |
if($items.Count -eq 0){ | |
Show-Alert "There are no items found which have broken links in the current language." | |
} else { | |
$bel_items = @(); | |
foreach($item in $items) { | |
foreach($field in $Item.Fields) { | |
if ($field.Type -eq 'General Link' -and $field.Value -like '*linktype="external"*') { | |
$found = $field.Value -match '.*url="(.*?)".*' | |
if($found) { | |
$url = $matches[1] | |
} | |
try{ | |
$response = Invoke-WebRequest $url -UseBasicParsing -method head | |
} | |
catch { | |
$statuscode = $_.Exception.Response.StatusCode.Value__ | |
if(!$statuscode) { | |
$statuscode = "Not reachable" | |
} | |
$reportItem = @{'FieldName'=$field.Name; | |
'Item'=$Item; | |
'FieldValue'=$url; | |
'StatusCode'=$statuscode; | |
} | |
$bel_items = $bel_items + $reportItem | |
} | |
} | |
} | |
} | |
$props = @{ | |
InfoTitle = "Items with broken external links" | |
InfoDescription = "Lists the items with broken external links searching the latest version in the current language." | |
PageSize = 25 | |
} | |
$bel_items | | |
Show-ListView @props -Property @{Label="Status Code"; Expression={$_.StatusCode} }, | |
@{Label="External Url"; Expression={$_.FieldValue} }, | |
@{Label="Field Name"; Expression={$_.FieldName} }, | |
@{Label="Name"; Expression={$_.Item.DisplayName} }, | |
@{Label="Path"; Expression={$_.Item.ItemPath} } | |
} | |
Close-Window |
Thank you Michael. Yes, that's a good idea!
Btw. I have not found a good solution to add custom properties to the report and enable the functionality of the oob "Open-Item"-Button in the same way. Is it possible and a good approach to reflect the properties of the report-object about a Sitecore-Item-Type or a Property with the Name "Item" (or combination)? What do you think?
Hi @daniiiol you can use the Add-Member command to enrich the objects with additional properties. use help Add-Member -Examples
for more details.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is fantastic. I'll have to give it a try! Maybe we could make the current report give you options to do both.