Skip to content

Commit 638e8b4

Browse files
Added first iteration of wait-path
1 parent 79870a6 commit 638e8b4

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

Wait-Path.ps1

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
Function Wait-Path {
2+
<#
3+
.SYNOPSIS
4+
Wait for a path to exist
5+
6+
.DESCRIPTION
7+
Wait for a path to exist
8+
9+
Default behavior will throw an error if we time out waiting for the path
10+
Passthru behavior will return true or false
11+
Behaviors above apply to the set of paths; unless all paths test successfully, we error out or return false
12+
13+
.PARAMETER Path
14+
Path(s) to test
15+
16+
Note
17+
Each path is independently verified with Test-Path.
18+
This means you can pass in paths from other providers.
19+
20+
.PARAMETER Timeout
21+
Time to wait before timing out, in seconds
22+
23+
.PARAMETER Interval
24+
Time to wait between each test, in seconds
25+
26+
.PARAMETER Passthru
27+
When specified, return true if we see all specified paths, otherwise return false
28+
29+
Note:
30+
If this is specified and we time out, we return false.
31+
If this is not specified and we time out, we throw an error.
32+
33+
.EXAMPLE
34+
Wait-Path \\Path\To\Share -Timeout 30
35+
36+
# Wait for \\Path\To\Share to exist, test every 1 second (default), time out at 30 seconds.
37+
38+
.EXAMPLE
39+
$TempFile = [System.IO.Path]::GetTempFileName()
40+
41+
if ( Wait-Path -Path $TempFile -Interval .5 -passthru )
42+
{
43+
Set-Content -Path $TempFile -Value "Test!"
44+
}
45+
else
46+
{
47+
Throw "Could not find $TempFile"
48+
}
49+
50+
# Create a temp file, wait until we can see that file, testing every .5 seconds, write data to it.
51+
52+
.EXAMPLE
53+
Wait-Path C:\Test, HKLM:\System
54+
55+
# Wait until C:\Test and HKLM:\System exist
56+
57+
.FUNCTIONALITY
58+
PowerShell Language
59+
60+
#>
61+
[cmdletbinding()]
62+
param (
63+
[string[]]$Path,
64+
[int]$Timeout = 5,
65+
[int]$Interval = 1,
66+
[switch]$Passthru
67+
)
68+
69+
$StartDate = Get-Date
70+
$First = $True
71+
72+
Do
73+
{
74+
#Only sleep if this isn't the first run
75+
if($First -eq $True)
76+
{
77+
$First = $False
78+
}
79+
else
80+
{
81+
Start-Sleep -Seconds $Interval
82+
}
83+
84+
#Test paths and collect output
85+
[bool[]]$Tests = foreach($PathItem in $Path)
86+
{
87+
Try
88+
{
89+
if(Test-Path $PathItem -ErrorAction stop)
90+
{
91+
Write-Verbose "'$PathItem' exists"
92+
$True
93+
}
94+
else
95+
{
96+
Write-Verbose "Waiting for '$PathItem'"
97+
$False
98+
}
99+
}
100+
Catch
101+
{
102+
Write-Error "Error testing path '$PathItem': $_"
103+
$False
104+
}
105+
}
106+
107+
# Identify whether we can see everything
108+
$Return = $Tests -notcontains $False -and $Tests -contains $True
109+
110+
# Poor logic, but we break the Until here
111+
# Did we time out?
112+
# Error if we are not passing through
113+
if ( ((Get-Date) - $StartDate).TotalSeconds -gt $Timeout)
114+
{
115+
if( $Passthru )
116+
{
117+
$False
118+
break
119+
}
120+
else
121+
{
122+
Throw "Timed out waiting for paths $($Path -join ", ")"
123+
}
124+
}
125+
elseif($Return)
126+
{
127+
if( $Passthru )
128+
{
129+
$True
130+
}
131+
break
132+
}
133+
}
134+
Until( $False ) # We break out above
135+
136+
}

0 commit comments

Comments
 (0)