-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.ps1
110 lines (93 loc) · 3.4 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Install PowerHTML on demand
If (-not (Get-Module -ErrorAction Ignore -ListAvailable PowerHTML)) {
Write-Verbose "Installing PowerHTML module for the current user..."
Install-Module PowerHTML -Scope CurrentUser -ErrorAction Stop
}
Import-Module PowerHTML -Scope Global -ErrorAction Stop
# Ensures that Invoke-WebRequest uses TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# common request headers
$global:headers = @{
"Content-Type"="text/xml; charset=utf-8";
"Accept-Encoding"="gzip"
}
Function global:GetAgent{
# Choose a random user-agent
$agents = [Microsoft.PowerShell.Commands.PSUserAgent]
$x=(Get-random -Minimum 1 -Maximum $agents.GetProperties().count)
return $agents.GetProperties()[$x-1]
}
Function global:SetPageHeader {
param (
[string] $html
)
$htmlDom = ConvertFrom-Html([System.Web.HttpUtility]::HtmlDecode($page))
$tab = $htmlDom.SelectSingleNode('//table')
$tab.id = "news-table"
$root = $htmlDom.SelectSingleNode('//head')
$nodes = @('<link rel="shortcut icon" href="res/favicon.ico"/>'
'<link rel="stylesheet" href="res/dark.css" media="(prefers-color-scheme: dark)" />'
'<link rel="stylesheet" href="res/light.css" media="(prefers-color-scheme: light)" />'
'<meta http-equiv="refresh" content="120"/>'
'<meta charset="utf-8"/>')
foreach($entry in $nodes){
$node = [HtmlAgilityPack.HtmlNode]::CreateNode($entry)
$root.childNodes.Add($node)
}
return $htmlDom.OuterHtml
}
Function global:WritePage {
param (
[string] $html,
[string] $path
)
$html | Out-File -FilePath $path -Encoding utf8
Write-Output $html
}
## used for parallell processing with old powershell-versions
Function global:Worker($Script, $List, $params = $null, $maxTreads = 8){
$sessionstate = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$UserFunctions = @( Get-ChildItem function:\)
if($UserFunctions.count -gt 0) {
foreach ($FunctionDef in $UserFunctions) {
if($FunctionDef.Name -ne $MyInvocation.MyCommand){
$sessionstate.Commands.Add((New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList $FunctionDef.Name, $FunctionDef.ScriptBlock))
}
}
}
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $maxTreads, $sessionstate, $Host)
$RunspacePool.Open()
$Jobs = @()
foreach($entry in $List) {
$Job = [powershell]::Create().AddScript($Script).AddArgument($entry).AddArgument($params)
$Job.RunspacePool = $RunspacePool
$Jobs += New-Object PSObject -Property @{
RunNum = $_
Pipe = $Job
Result = $Job.BeginInvoke()
}
}
Do {
Start-Sleep -Milliseconds 100
} While ( $Jobs.Result.IsCompleted -contains $false)
$Results = @()
ForEach ($Job in $Jobs)
{ #collect results here
$jobError = $Job.Pipe.InvocationStateInfo.Reason.ErrorRecord.Exception
try {
$pmg = $Job.Pipe.EndInvoke($Job.Result)
$Results += $pmg
}
catch {
$jobError = $_.exception
}
if ($jobError) {
Write-Host $jobError.Message -ForegroundColor Red
}
$Job.Pipe.Dispose()
}
$RunspacePool.Close()
$RunspacePool.Dispose()
return $Results
}
$global:agent = global:GetAgent