PowerShell 스크립트: 끊어진 링크에 대한 문서 스캔
11966 단어 dotnetscriptingpowershell
다음은 문서 저장소에서 실행할 수 있는 작은 PowerShell 스크립트이며 적절한 리디렉션 또는 HTTP StatusCode 200에서 확인되지 않는 URL을 알려줍니다.
Param(
[Parameter(Mandatory=$true)]
[string] $DocsRootPath,
[string] $FileFilter = "*.md"
)
Param(
[Parameter(Mandatory=$true)]
[string] $DocsRootPath
)
# use as ./CrawlDocsForBrokenLinks -DocsRootPath path/to/docs
# Url Regex specifically for Markdown keeping []() [][] into account
$UrlRegex = '((?:https?):\/\/[a-z0-9\.:].*?(?=[\s\]\[\)]))|((?:https?):\/\/[a-z0-9\.:].*?(?=[\s\]\[\)]))';
Get-ChildItem -Path $DocsRootPath -File -Recurse -Filter "*.md" `
| Select-String -Pattern $UrlRegex -AllMatches `
| ForEach-Object {
[Microsoft.PowerShell.Commands.MatchInfo]$MatchInfo = $PSItem;
$MatchInfo.Matches `
| Where-Object { $_.Value.StartsWith('http://') -or $_.Value.StartsWith('https://') } `
| ForEach-Object {
$Value = $PSItem.Value;
$Value = $Value.Trim('"').Trim("'");
try {
$Response = Invoke-WebRequest `
-Uri $Value `
-UseBasicParsing `
-ErrorAction SilentlyContinue;
}
catch {
$Response = $PSItem.Exception.Response;
Write-Output "$([int]$Response.StatusCode) - $($MatchInfo.Path):$($MatchInfo.LineNumber) ($($Value))";
}
};
};
코드는 다음을 수행합니다.
코드를
CrawlDocsForBrokenLinks.ps1
라는 파일에 저장한 다음 PowerShell 셸을 열고 다음과 같이 호출하여 사용할 수 있습니다../CrawlDocsForBrokenLinks.ps1 -DocsRootPath path/to/docs
# Output looks like this
# 404 - C:\Users\nswimberghe\source\repos\azure-docs\README.md:22 (https://marketplace.visualstudio.com/items?itemName=docsmsft)
# 404 - C:\Users\nswimberghe\source\repos\azure-docs\ThirdPartyNotices.md:3 (https://creativecommons.org/licenses/by/4)
# 0 - C:\Users\nswimberghe\source\repos\azure-docs\articles\best-practices-availability-paired-regions.md:90 (https://github.com/uglide/azure-content/blob/master/articles/resiliency/resiliency-technical-guidance)
# 0 - C:\Users\nswimberghe\source\repos\azure-docs\articles\cloud-services-php-create-web-role.md:166 (http://127.0.0.1:81)
# 0 - C:\Users\nswimberghe\source\repos\azure-docs\articles\cloud-services-php-create-web-role.md:170 (http://127.0.0.1:81)
# 0 - C:\Users\nswimberghe\source\repos\azure-docs\articles\nodejs-use-node-modules-azure-apps.md:27 (https://github.com/woloski/nodeonazure-blog/blob/master/articles/startup-task-to-run-npm-in-azure)
# 0 - C:\Users\nswimberghe\source\repos\azure-docs\articles\partner-twilio-java-phone-call-example.md:175 (http://localhost:8080/TwilioCloud/callform)
# 0 - C:\Users\nswimberghe\source\repos\azure-docs\articles\partner-twilio-nodejs-how-to-use-voice-sms.md:134 (https://CHANGE_ME.azurewebsites.net/outbound_call)
# 404 - C:\Users\nswimberghe\source\repos\azure-docs\articles\partner-twilio-nodejs-how-to-use-voice-sms.md:244 (https://www.twilio.com/blog/2013/04/introduction-to-twilio-client-with-node-js)
# 404 - C:\Users\nswimberghe\source\repos\azure-docs\articles\partner-twilio-nodejs-how-to-use-voice-sms.md:246 (https://www.twilio.com/blog/2012/09/building-a-real-time-sms-voting-app-part-1-node-js-couchdb)
# 404 - C:\Users\nswimberghe\source\repos\azure-docs\articles\partner-twilio-nodejs-how-to-use-voice-sms.md:247 (https://www.twilio.com/blog/2013/06/pair-programming-in-the-browser-with-twilio)
# 0 - C:\Users\nswimberghe\source\repos\azure-docs\articles\partner-twilio-php-how-to-use-voice-sms.md:97 (https://github.com/twilio/twilio-php/blob/master/README)
# 404 - C:\Users\nswimberghe\source\repos\azure-docs\articles\partner-twilio-php-how-to-use-voice-sms.md:145 (http://readthedocs.org/docs/twilio-php/en/latest/usage/rest)
# 0 - C:\Users\nswimberghe\source\repos\azure-docs\articles\partner-twilio-php-how-to-use-voice-sms.md:255 (https://github.com/twilio/twilio-php/blob/master/README)
# 0 - C:\Users\nswimberghe\source\repos\azure-docs\articles\partner-twilio-php-make-phone-call.md:26 (https://github.com/twilio/twilio-php/blob/master/README)
다음과 같이 출력을 파일로 리디렉션할 수 있습니다.
./CrawlDocsForBrokenLinks.ps1 -DocsRootPath path/to/docs > brokenlinks.log
VSCode로 로그 파일을 열 때 ctrl+path:linenumber 조합을 클릭하면 VSCode가 파일을 열고 커서를 올바른 줄 번호에 놓습니다!
이를 통해 문서에서 작동하는 URL을 더 쉽게 유지 관리할 수 있기를 바랍니다. 행운을 빕니다!
Reference
이 문제에 관하여(PowerShell 스크립트: 끊어진 링크에 대한 문서 스캔), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/swimburger/powershell-script-scan-documentation-for-broken-links-10la텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)