-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWait-PathToBeNotReady.ps1
136 lines (124 loc) · 6.02 KB
/
Wait-PathToBeNotReady.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
function Wait-PathToBeNotReady {
# .SYNOPSIS
# Waits for the specified path to be unavailable.
#
# .DESCRIPTION
# This function waits for the specified path to be unavailable, e.g., after
# removing a PSDrive, it may take a few seconds for the drive to be removed
# from the system. This function will wait for the path to be unavailable for
# up to the specified number of seconds.
#
# .PARAMETER ReferenceToUseGetPSDriveWorkaround
# This parameter is a memory reference to a boolean variable that indicates
# whether or not the Get-PSDrive workaround should be used. If the Get-PSDrive
# workaround is used, then the function will use the Get-PSDrive cmdlet to
# refresh PowerShell's "understanding" of the available drive letters. This
# variable is passed by reference to ensure that this function can set the
# variable to $true if the Get-PSDrive workaround is successful - which
# improves performance of subsequent runs.
#
# .PARAMETER Path
# This parameter is a string containing the path to be tested for availability.
#
# .PARAMETER MaximumWaitTimeInSeconds
# This parameter is the maximum amount of seconds to wait for the path to be
# ready. If the path is not ready within this time, then the function will
# return $false. By default, this parameter is set to 10 seconds.
#
# .PARAMETER DoNotAttemptGetPSDriveWorkaround
# This parameter is a switch that indicates whether or not the Get-PSDrive
# workaround should be attempted. If this switch is specified, then the
# Get-PSDrive workaround will not be attempted. This switch is useful if you
# know that the Get-PSDrive workaround will not work on your system, or if you
# know that the Get-PSDrive workaround is not necessary on your system.
#
# .EXAMPLE
# $boolUseGetPSDriveWorkaround = $false
# $boolPathUnavailable = Wait-PathToBeNotReady -Path 'D:\Shares\Share\Data' -ReferenceToUseGetPSDriveWorkaround ([ref]$boolUseGetPSDriveWorkaround)
#
# .OUTPUTS
# A boolean value indiciating whether the path is unavailable
#
# .NOTES
# Version: 1.0.20241216.0
param (
[System.Management.Automation.PSReference]$ReferenceToUseGetPSDriveWorkaround = ([ref]$null),
[string]$Path,
[int]$MaximumWaitTimeInSeconds = 10,
[switch]$DoNotAttemptGetPSDriveWorkaround
)
#region License ############################################################
# Copyright (c) 2024 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#endregion License ############################################################
#region Process Input ######################################################
if ([string]::IsNullOrEmpty($Path) -eq $true) {
Write-Error 'When calling Wait-PathToBeNotReady, the Path parameter cannot be null or empty'
return $false
}
if ($DoNotAttemptGetPSDriveWorkaround.IsPresent -eq $true) {
$boolAttemptGetPSDriveWorkaround = $false
} else {
$boolAttemptGetPSDriveWorkaround = $true
}
#endregion Process Input ######################################################
$boolFunctionReturn = $false
if ($null -ne ($ReferenceToUseGetPSDriveWorkaround.Value)) {
if (($ReferenceToUseGetPSDriveWorkaround.Value) -eq $true) {
# Use workaround for drives not refreshing in current PowerShell session
Get-PSDrive | Out-Null
}
}
$doubleSecondsCounter = 0
# Try Test-Path and sleep for up to $MaximumWaitTimeInSeconds seconds until
# it's successful
while ($doubleSecondsCounter -le $MaximumWaitTimeInSeconds -and $boolFunctionReturn -eq $false) {
if (Test-Path $Path) {
Start-Sleep 0.2
$doubleSecondsCounter += 0.2
} else {
$boolFunctionReturn = $true
}
}
if ($boolFunctionReturn -eq $false) {
if ($null -eq ($ReferenceToUseGetPSDriveWorkaround.Value) -or ($ReferenceToUseGetPSDriveWorkaround.Value) -eq $false) {
# Either a variable was not passed in, or the variable was passed in
# and it was set to false
if ($boolAttemptGetPSDriveWorkaround -eq $true) {
# Try workaround for drives not refreshing in current PowerShell
# session
Get-PSDrive | Out-Null
# Restart counter and try waiting again
$doubleSecondsCounter = 0
# Try Test-Path and sleep for up to $MaximumWaitTimeInSeconds
# seconds until it's successful
while ($doubleSecondsCounter -le $MaximumWaitTimeInSeconds -and $boolFunctionReturn -eq $false) {
if (Test-Path $Path) {
Start-Sleep 0.2
$doubleSecondsCounter += 0.2
} else {
$boolFunctionReturn = $true
}
}
}
}
}
return $boolFunctionReturn
}