-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.ps1
230 lines (196 loc) · 9.16 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
function Resolve-Exception {
[CmdletBinding()]
Param (
[parameter(Mandatory = $true, Position = 0, HelpMessage = 'The exception object to handle')]
[ValidateNotNull()]
[Object] $ExceptionObject,
[parameter(Mandatory = $false, HelpMessage = 'Toogle stacktrace output')]
[switch] $HideStackTrace = $false,
[parameter(Mandatory = $false, HelpMessage = 'The error action to use')]
[String] $CustomErrorAction = 'Stop'
)
Begin {
}
Process
{
$sst = ''
$st = ''
$e = $ExceptionObject.Exception
if ($null -ne (Get-Member -InputObject $ExceptionObject -Name 'ScriptStackTrace')) {
$sst = $ExceptionObject.ScriptStackTrace
}
if ($null -ne (Get-Member -InputObject $ExceptionObject -Name 'StackTrace')) {
$st = $ExceptionObject.StackTrace
}
$msg = $e.Message
while ($e.InnerException) {
$e = $e.InnerException
$msg += "`n" + $e.Message
if ($null -ne (Get-Member -InputObject $e -Name 'ScriptStackTrace')) {
$sst += "`n" + $e.ScriptStackTrace + "`n---"
}
if ($null -ne (Get-Member -InputObject $e -Name 'StackTrace')) {
$st += "`n" + $e.StackTrace + "`n---"
}
}
if (-not ($HideStackTrace)) {
$msg += "`n---[ScriptStackTrace]---`n" + $sst + "`n---[StackTrace]---`n" + $st
}
Write-Error -Message $msg -ErrorAction $CustomErrorAction
}
End {
}
}
function Add-FileToAppDomain {
[CmdletBinding()]
param (
[parameter(Mandatory = $true, HelpMessage = 'The base path to load files from.')]
[ValidateNotNull()]
[string] $BasePath,
[parameter(Mandatory = $true, HelpMessage = 'The file to load into the AppDomain.')]
[ValidateNotNull()]
[string] $File
)
if (-not (Test-Path "$BasePath" -PathType Container))
{
Throw "[!] Can't find or access folder ${BasePath}."
}
$FileToLoad = Join-Path "${BasePath}" "$File"
if (-not (Test-Path "$FileToLoad" -PathType Leaf))
{
Throw "[!] Can't find or access file ${FileToLoad}."
}
if ( -Not ([appdomain]::currentdomain.getassemblies() |Where-Object Location -Like ${FileToLoad})) {
try {
[System.Reflection.Assembly]::LoadFrom($FileToLoad) | Out-Null
$clientVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($FileToLoad).ProductVersion
Write-Debug "[+] File ${File} loaded with version ${clientVersion} from ${BasePath}."
} catch {
Resolve-Exception -ExceptionObject $PSitem
}
}
}
function Add-NuGetDependencies {
[CmdletBinding()]
param (
[parameter(Mandatory = $true, HelpMessage = 'The Nuget packages to load.')]
[Hashtable] $NugetPackages
)
$CurrentFileName = Split-Path $PSScriptRoot -Leaf
$memstream = [IO.MemoryStream]::new([byte[]][char[]]$CurrentFileName)
$CurrentFileNameHash = (Get-FileHash -InputStream $memstream -Algorithm SHA1).Hash
# Get a unique temporary directory to store all the NuGet packages we need later
$TempWorkDir = Join-Path "$($env:TEMP)" "$CurrentFileNameHash"
if (-not (Test-Path "$TempWorkDir" -PathType Container))
{
New-Item -Path "$($env:TEMP)" -Name "$CurrentFileNameHash" -ItemType Directory
}
foreach ($dep in $NugetPackages.Keys) {
$version = $NugetPackages[$dep]
$destinationPath = Join-Path "$TempWorkDir" "${dep}.${version}"
if (-not (Test-Path "$destinationPath" -PathType Container))
{
Install-Package -Name $dep -RequiredVersion $version -Destination "$TempWorkDir" -SkipDependencies -ProviderName NuGet -Source nuget.org -Force
Write-Information "[+] Install package ${dep} with version ${version} into folder ${TempWorkDir}"
}
# Prioritise version 4.8 over 4.5
$BasePath = Join-Path (Join-Path "$destinationPath" "lib") "net48"
if (-not (Test-Path "$BasePath" -PathType Container)) {
$BasePath = Join-Path (Join-Path "$destinationPath" "lib") "net45"
}
Add-FileToAppDomain -BasePath $BasePath -File "${dep}.dll"
}
}
function Get-AutomationButton {
[CmdletBinding()]
param (
[parameter(Mandatory = $true, Position = 0, HelpMessage = 'The base automation element')]
[ValidateNotNull()]
[FlaUI.Core.AutomationElements.AutomationElement] $BaseAutomationElement,
[parameter(Mandatory = $false, HelpMessage = 'The name of the button to get')]
[string] $Name,
[parameter(Mandatory = $false, HelpMessage = 'The automation id of the button to get')]
[string] $Id,
[parameter(Mandatory = $false, HelpMessage = 'Delay in milliseconds to wait for the control')]
[int] $WaitDelay = 1200
)
return Get-AutomationElement -BaseAutomationElement $BaseAutomationElement -Name $Name -Id $Id -ControlType 'Button' -WaitDelay $WaitDelay
}
function Get-AutomationTextBox {
[CmdletBinding()]
param (
[parameter(Mandatory = $true, Position = 0, HelpMessage = 'The base automation element')]
[ValidateNotNull()]
[FlaUI.Core.AutomationElements.AutomationElement] $BaseAutomationElement,
[parameter(Mandatory = $false, HelpMessage = 'The name of the Textbox to get')]
[string] $Name,
[parameter(Mandatory = $false, HelpMessage = 'The automation id of the Textbox to get')]
[string] $Id,
[parameter(Mandatory = $false, HelpMessage = 'Delay in milliseconds to wait for the control')]
[int] $WaitDelay = 50
)
return Get-AutomationElement -BaseAutomationElement $BaseAutomationElement -Name $Name -Id $Id -ControlType 'Text' -WaitDelay $WaitDelay
}
function Get-AutomationElement {
[CmdletBinding()]
param (
[parameter(Mandatory = $true, Position = 0, HelpMessage = 'The base automation element')]
[ValidateNotNull()]
[FlaUI.Core.AutomationElements.AutomationElement] $BaseAutomationElement,
[parameter(Mandatory = $true, Position = 1, HelpMessage = 'The control type of the element to get')]
[ValidateNotNull()]
[string] $ControlType,
[parameter(Mandatory = $false, HelpMessage = 'The name of the element to get')]
[string] $Name,
[parameter(Mandatory = $false, HelpMessage = 'The automation id of the element to get')]
[string] $Id,
[parameter(Mandatory = $false, HelpMessage = 'Delay in milliseconds to wait for the control')]
[int] $WaitDelay = 250
)
$ElementCondition = $BaseAutomationElement.ConditionFactory.ByControlType($ControlType)
[FlaUI.Core.AutomationElements.AutomationElement] $FoundElement = $null
$MaxRetries = 2500
$CallStack = Get-PSCallStack
while ($true) {
if (($PSBoundParameters.ContainsKey('Id')) -and (-not ([string]::IsNullOrWhiteSpace("$Id")))) {
$FoundElement = $BaseAutomationElement.FindFirstDescendant($ElementCondition.And($BaseAutomationElement.ConditionFactory.ByAutomationId($Id)))
} else {
$FoundElement = $BaseAutomationElement.FindFirstDescendant($ElementCondition.And($BaseAutomationElement.ConditionFactory.ByName($Name)))
}
if ($null -ne $FoundElement) {
if ($FoundElement.IsAvailable -and $FoundElement.IsEnabled) {
break
} else {
Write-Debug "$($CallStack[2].ScriptName): $($CallStack[2].ScriptLineNumber):`n`tFound element has state isAvailable: $($FoundElement.IsAvailable) and isEnabled: $($FoundElement.IsEnabled). Retries to go: $($MaxRetries) - $FoundElement"
}
} elseif (0 -eq ($MaxRetries % 250)) {
Write-Debug "$($CallStack[2].ScriptName): $($CallStack[2].ScriptLineNumber):`n`tStill searching for element of type '$ControlType' with Id '$Id' or Name '$Name'."
}
Start-Sleep -Milliseconds $WaitDelay
if (0 -eq $MaxRetries) {
throw "$($CallStack[2].ScriptName): $($CallStack[2].ScriptLineNumber):`n`tCouldn't find element of type '$ControlType' with Id '$Id' or Name '$Name'."
}
$MaxRetries--
}
try {
switch ($ControlType) {
'List' { $ControlType = 'ListBox' }
'ListItem' { $ControlType = 'ListBoxItem' }
'Edit' { $ControlType = 'TextBox' }
'Pane' { $ControlType = 'TextBox' }
'Text' { $ControlType = 'TextBox' }
'Hyperlink' { $ControlType = 'AutomationElement' }
'ScrollBar' { $ControlType = 'AutomationElement' }
'MenuBar' { $ControlType = 'AutomationElement' }
'MenuItem' { $ControlType = 'AutomationElement' }
'ToolBar' { $ControlType = 'AutomationElement' }
'DataItem' { $ControlType = 'AutomationElement' }
'TabItem' { $ControlType = 'AutomationElement' }
'Window' { $ControlType = 'AutomationElement' }
}
Set-Variable -Name 'RetVal' -Scope 'Local' -Value ($FoundElement.FrameworkAutomationElement -As ("FlaUI.Core.AutomationElements.$ControlType" -As [Type]))
} catch {
Resolve-Exception -ExceptionObject $PSitem
}
return $RetVal
}