-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathHelpersCommon.psm1
More file actions
624 lines (543 loc) · 17 KB
/
HelpersCommon.psm1
File metadata and controls
624 lines (543 loc) · 17 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
function Wait-UntilTrue
{
[CmdletBinding()]
param (
[ScriptBlock]$sb,
[int]$TimeoutInMilliseconds = 10000,
[int]$IntervalInMilliseconds = 1000
)
# Get the current time
$startTime = [DateTime]::Now
# Loop until the script block evaluates to true
while (-not ($sb.Invoke())) {
# If the timeout period has passed, return false
if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds) {
return $false
}
# Wait
Start-Sleep -Milliseconds $intervalInMilliseconds > $null
}
return $true
}
function Wait-FileToBePresent
{
[CmdletBinding()]
param (
[string]$File,
[int]$TimeoutInSeconds = 10,
[int]$IntervalInMilliseconds = 100
)
return Wait-UntilTrue -sb { Test-Path $File } -TimeoutInMilliseconds ($TimeoutInSeconds*1000) -IntervalInMilliseconds $IntervalInMilliseconds
}
function Test-IsElevated
{
$IsElevated = $false
if ( $IsWindows ) {
# on Windows we can determine whether we're executing in an
# elevated context
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$windowsPrincipal = New-Object 'Security.Principal.WindowsPrincipal' $identity
if ($windowsPrincipal.IsInRole("Administrators") -eq 1)
{
$IsElevated = $true
}
}
else {
# on Linux, tests run via sudo will generally report "root" for whoami
if ( (whoami) -match "root" ) {
$IsElevated = $true
}
}
return $IsElevated
}
function Get-RandomFileName
{
[System.IO.Path]::GetFileNameWithoutExtension([IO.Path]::GetRandomFileName())
}
#
# Testhook setting functions
# note these manipulate private data in the PowerShell engine which will
# enable us to not actually alter the system or mock returned data
#
$SCRIPT:TesthookType = [system.management.automation.internal.internaltesthooks]
function Test-TesthookIsSet
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingEmptyCatchBlock", '')] # , Justification = "an error message is not appropriate for this function")]
param (
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory=$true)]
$testhookName
)
try {
return ${Script:TesthookType}.GetField($testhookName, "NonPublic,Static").GetValue($null)
}
catch {
# fall through
}
return $false
}
function Enable-Testhook
{
param (
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory=$true)]
$testhookName
)
${Script:TesthookType}::SetTestHook($testhookName, $true)
}
function Disable-Testhook
{
param (
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory=$true)]
$testhookName
)
${Script:TesthookType}::SetTestHook($testhookName, $false)
}
function Set-TesthookResult
{
param (
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory=$true)]
$testhookName,
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory=$true)]
$value
)
${Script:TesthookType}::SetTestHook($testhookName, $value)
}
function Add-TestDynamicType
{
param()
Add-Type -TypeDefinition @'
using System.Collections.Generic;
using System.Dynamic;
public class TestDynamic : DynamicObject
{
private static readonly string[] s_dynamicMemberNames = new string[] { "FooProp", "BarProp", "FooMethod", "SerialNumber" };
private static int s_lastSerialNumber;
private readonly int _serialNumber;
public TestDynamic()
{
_serialNumber = ++s_lastSerialNumber;
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return s_dynamicMemberNames;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = null;
if (binder.Name == "FooProp")
{
result = 123;
return true;
}
else if (binder.Name == "BarProp")
{
result = 456;
return true;
}
else if (binder.Name == "SerialNumber")
{
result = _serialNumber;
return true;
}
else if (binder.Name == "HiddenProp")
{
// Not presented in GetDynamicMemberNames
result = 789;
return true;
}
return false;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
result = null;
if (binder.Name == "FooMethod")
{
result = "yes";
return true;
}
else if (binder.Name == "HiddenMethod")
{
// Not presented in GetDynamicMemberNames
result = _serialNumber;
return true;
}
return false;
}
}
'@
}
# Upload an artifact in VSTS
# On other systems will just log where the file was placed
function Send-VstsLogFile {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification = "needed for VSO")]
param (
[parameter(Mandatory,ParameterSetName='contents')]
[string[]]
$Contents,
[parameter(Mandatory,ParameterSetName='contents')]
[string]
$LogName,
[parameter(Mandatory,ParameterSetName='path')]
[ValidateScript({Test-Path -Path $_})]
[string]
$Path
)
$logFolder = Join-Path -Path $PWD -ChildPath 'logfile'
if(!(Test-Path -Path $logFolder))
{
$null = New-Item -Path $logFolder -ItemType Directory
if($IsMacOS -or $IsLinux)
{
$null = chmod a+rw $logFolder
}
}
$newName = ([System.Io.Path]::GetRandomFileName() + "-$LogName.txt")
if($Contents)
{
$logFile = Join-Path -Path $logFolder -ChildPath $newName
$Contents | Out-File -path $logFile -Encoding ascii
}
else
{
$logFile = Join-Path -Path $logFolder -ChildPath $newName
Copy-Item -Path $Path -Destination $logFile
}
Write-Host "##vso[artifact.upload containerfolder=$newName;artifactname=$newName]$logFile"
Write-Verbose "Log file captured as $newName" -Verbose
}
# Tests if the Linux or macOS user is root
function Test-IsRoot
{
if($IsLinux -or $IsMacOS)
{
$uid = &id -u
if($uid -eq 0)
{
return $true
}
}
return $false
}
# Tests if we are running is a VSTS Linux Build
function Test-IsVstsLinux
{
return ($env:TF_BUILD -and $IsLinux)
}
# Tests if we are running is a VSTS Linux Build
function Test-IsVstsWindows
{
return ($env:TF_BUILD -and $IsWindows)
}
# this function wraps native command Execution
# for more information, read https://mnaoumov.wordpress.com/2015/01/11/execution-of-external-commands-in-powershell-done-right/
function Start-NativeExecution
{
param(
[scriptblock]$sb,
[switch]$IgnoreExitcode,
[switch]$VerboseOutputOnError
)
$backupEAP = $script:ErrorActionPreference
$script:ErrorActionPreference = "Continue"
try {
if($VerboseOutputOnError.IsPresent)
{
$output = & $sb 2>&1
}
else
{
& $sb
}
# note, if $sb doesn't have a native invocation, $LASTEXITCODE will
# point to the obsolete value
if ($LASTEXITCODE -ne 0 -and -not $IgnoreExitcode) {
if($VerboseOutputOnError.IsPresent -and $output)
{
$output | Out-String | Write-Verbose -Verbose
}
# Get caller location for easier debugging
$caller = Get-PSCallStack -ErrorAction SilentlyContinue
if($caller)
{
$callerLocationParts = $caller[1].Location -split ":\s*line\s*"
$callerFile = $callerLocationParts[0]
$callerLine = $callerLocationParts[1]
$errorMessage = "Execution of {$sb} by ${callerFile}: line $callerLine failed with exit code $LASTEXITCODE"
throw $errorMessage
}
throw "Execution of {$sb} failed with exit code $LASTEXITCODE"
}
} finally {
$script:ErrorActionPreference = $backupEAP
}
}
# Creates a new random hex string for use with things like test certificate passwords
function New-RandomHexString
{
param([int]$Length = 10)
$random = [Random]::new()
return ((1..$Length).ForEach{ '{0:x}' -f $random.Next(0xf) }) -join ''
}
$script:CanWriteToPsHome = $null
function Test-CanWriteToPsHome
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingEmptyCatchBlock', '', Justification = "an error message is not appropriate for this function")]
param ()
if ($null -ne $script:CanWriteToPsHome) {
return $script:CanWriteToPsHome
}
$script:CanWriteToPsHome = $false
try {
$testFileName = Join-Path $PSHOME (New-Guid).Guid
$null = New-Item -ItemType File -Path $testFileName -ErrorAction Stop
$script:CanWriteToPsHome = $true
Remove-Item -Path $testFileName -ErrorAction SilentlyContinue
}
catch {
; # do nothing
}
$script:CanWriteToPsHome
}
# Creates a password meeting Windows complexity rules
function New-ComplexPassword
{
$numbers = "0123456789"
$lowercase = "abcdefghijklmnopqrstuvwxyz"
$uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$symbols = "~!@#$%^&*_-+=``|\(){}[]:;`"'<>,.?/"
$password = [string]::Empty
# Windows password complexity rule requires minimum 8 characters and using at least 3 of the
# buckets above, so we just pick one from each bucket twice.
# https://learn.microsoft.com/windows/security/threat-protection/security-policy-settings/password-must-meet-complexity-requirements
1..2 | ForEach-Object {
$Password += $numbers[(Get-Random $numbers.Length)] + $lowercase[(Get-Random $lowercase.Length)] +
$uppercase[(Get-Random $uppercase.Length)] + $symbols[(Get-Random $symbols.Length)]
}
$password
}
# return a specific string with regard to platform information
function Get-PlatformInfo {
if ( $IsWindows ) {
return @{Platform = "windows"; Version = '' }
}
if ( $IsMacOS ) {
return @{Platform = "macos"; Version = sw_vers -productversion }
}
if ( $IsLinux ) {
$osrelease = Get-Content /etc/os-release | ConvertFrom-StringData
if ( -not [string]::IsNullOrEmpty($osrelease.ID) ) {
$versionId = if (-not $osrelease.Version_ID ) {
''
} else {
$osrelease.Version_ID.trim('"')
}
$platform = $osrelease.ID.trim('"')
return @{Platform = $platform; Version = $versionId }
}
return @{ Platform = "linux"; version = "unknown" }
}
}
# return true if WsMan is supported on the current platform
function Get-WsManSupport {
$platformInfo = Get-PlatformInfo
if (
($platformInfo.Platform -eq 'centos' -and $platformInfo.Version -eq '7')
) {
return $true
}
return $false
}
function Test-IsWindowsArm64 {
return $IsWindows -and [System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture -eq [System.Runtime.InteropServices.Architecture]::Arm64
}
function Test-IsWinWow64 {
return $IsWindows -and [System.Environment]::Is64BitOperatingSystem -and -not [System.Environment]::Is64BitProcess
}
function Test-IsPreview
{
param(
[parameter(Mandatory)]
[string]
$Version,
[switch]$IsLTS
)
if ($IsLTS.IsPresent) {
## If we are building a LTS package, then never consider it preview.
return $false
}
return $Version -like '*-*'
}
<#
.Synopsis
Tests if a version is a Release Candidate
.EXAMPLE
Test-IsReleaseCandidate -version '6.1.0-sometthing' # returns false
Test-IsReleaseCandidate -version '6.1.0-rc.1' # returns true
Test-IsReleaseCandidate -version '6.1.0' # returns false
#>
function Test-IsReleaseCandidate
{
param(
[parameter(Mandatory)]
[string]
$Version
)
if ($Version -like '*-rc.*')
{
return $true
}
return $false
}
function Test-IsWinServer2012R2
{
if (-not $IsWindows) {
return $false
}
$osInfo = [System.Environment]::OSVersion.Version
return ($osInfo.Major -eq 6 -and $osInfo.Minor -eq 3)
}
function Test-IsWindows2016 {
if (-not $IsWindows) {
return $false
}
$osInfo = [System.Environment]::OSVersion.Version
return ($osInfo.Major -eq 10 -and $osInfo.Minor -eq 0 -and $osInfo.Build -eq 14393)
}
# helpers for managing psdefaultparametervalues
[system.collections.generic.Stack[hashtable]]$script:DefaultParameterValueStack = [system.collections.generic.Stack[hashtable]]::new()
# Ensure that the global:PSDefaultParameterValues variable is a hashtable
function Initialize-PSDefaultParameterValue {
if ( $global:PSDefaultParameterValues -isnot [hashtable] ) {
$global:PSDefaultParameterValues = @{}
}
}
# reset the stack
function Reset-DefaultParameterValueStack {
$script:DefaultParameterValueStack = [system.collections.generic.Stack[hashtable]]::new()
Initialize-PSDefaultParameterValue
}
# return the current stack
function Get-DefaultParameterValueStack {
$script:DefaultParameterValueStack
}
# PSDefaultParameterValue may not have both skip and pending keys
function Test-PSDefaultParameterValue {
if ( $global:PSDefaultParameterValues -is [hashtable] ) {
if ( $global:PSDefaultParameterValues.ContainsKey('skip') -and $global:PSDefaultParameterValues.ContainsKey('pending') ) {
return $false
}
return $true
}
Initialize-PSDefaultParameterValue
}
# push a new value onto the stack
# if $ht is null, then the current value of $global:PSDefaultParameterValues is pushed
# if $NewValue is used, then $ht is used as the new value of $global:PSDefaultParameterValues
function Push-DefaultParameterValueStack {
param ([hashtable]$ht, [switch]$NewValue)
Initialize-PSDefaultParameterValue
$script:DefaultParameterValueStack.Push($global:PSDefaultParameterValues.Clone())
if ( $ht ) {
if ( $NewValue ) {
$global:PSDefaultParameterValues = $ht
}
else {
foreach ($k in $ht.Keys) {
$global:PSDefaultParameterValues[$k] = $ht[$k]
}
}
if ( ! (Test-PSDefaultParameterValue)) {
Write-Warning -Message "PSDefaultParameterValues may not have both skip and pending keys, resetting."
Pop-DefaultParameterValueStack
}
}
}
function Pop-DefaultParameterValueStack {
try {
$global:PSDefaultParameterValues = $script:DefaultParameterValueStack.Pop()
return $true
}
catch {
Initialize-PSDefaultParameterValue
return $false
}
}
function Get-HelpNetworkTestCases
{
param(
[switch]
$PositiveCases
)
# .NET doesn't consider these path rooted and we won't go to the network:
# \\?
# \\.
# \??
# Command discovery does not follow symlinks to network locations for module qualified paths
$networkBlockedError = "CommandNameNotAllowed,Microsoft.PowerShell.Commands.GetHelpCommand"
# This error may change as long as no test cases start failing for other reasons
$scriptBlockedError = "CommandNotFoundException"
$formats = @(
'//{0}/share/{1}'
'\\{0}\share\{1}'
'//{0}\share/{1}'
'Microsoft.PowerShell.Core\filesystem:://{0}/share/{1}'
)
if (!$PositiveCases) {
$formats += 'filesystem:://{0}/share/{1}'
}
$moduleQualifiedCommand = 'test.dll\fakecommand'
$lanManFormat = @(
'//;LanmanRedirector/{0}/share/{1}'
)
$hosts = @(
'fakehost'
'fakehost.pstest'
)
$commands = @(
'test.ps1'
'test.dll'
$moduleQualifiedCommand
)
$variants = @()
$cases = @()
foreach($command in $commands) {
$hostName = $hosts[0]
$format = $formats[0]
$cases += @{
Command = $format -f $hostName, $command
ExpectedError = $networkBlockedError
}
}
foreach($hostName in $hosts) {
# chose the format with backslashes(\) to match the host with blackslashes
$format = $formats[1]
$command = $commands[0]
$cases += @{
Command = $format -f $hostName, $command
ExpectedError = $networkBlockedError
}
}
foreach($format in $formats) {
$hostName = $hosts[0]
$command = $commands[0]
$cases += @{
Command = $format -f $hostName, $command
ExpectedError = $networkBlockedError
}
}
foreach($format in $lanManFormat) {
$hostName = $hosts[0]
$command = $moduleQualifiedCommand
$cases += @{
Command = $format -f $hostName, $command
ExpectedError = $scriptBlockedError
}
}
return $cases | Sort-Object -Property ExpectedError, Command -Unique
}