-
Notifications
You must be signed in to change notification settings - Fork 35
/
common.ps1
45 lines (37 loc) · 1014 Bytes
/
common.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function crawl {
param ([string]$url)
(Invoke-WebRequest $url -UseBasicParsing).Links |
Where-Object {
($_ | Get-Member href) -and
[uri]::IsWellFormedUriString($_.href, [System.UriKind]::RelativeOrAbsolute)
} |
ForEach-Object {
$href = [System.Net.WebUtility]::HtmlDecode($_.href)
try {
(New-Object System.Uri([uri]$url, $href)).AbsoluteUri
}
catch {
$href
}
}
}
function mkdirp {
param ([string] $dir)
New-Item -Path $dir -Type Directory -Force | Out-Null
}
function exec {
param ([scriptblock]$private:cmd)
$private:eap = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
$global:LASTEXITCODE = 0
try {
# Convert stderr in ErrorRecord objects back to strings
& $cmd 2>&1 | ForEach-Object { "$_" }
if ($LASTEXITCODE -ne 0) {
throw "Command '$cmd' exited with code $LASTEXITCODE"
}
}
finally {
$ErrorActionPreference = $eap
}
}