forked from JohnDuprey/PSWindowsImageRepair
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test-IsFileLocked.ps1
37 lines (37 loc) · 1.17 KB
/
Test-IsFileLocked.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
Function Test-IsFileLocked {
[cmdletbinding()]
Param (
[parameter(Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[Alias('FullName', 'PSPath')]
[string[]]$Path
)
Process {
ForEach ($Item in $Path) {
# Ensure this is a full path
$Item = Convert-Path $Item
# Verify that this is a file and not a directory
If ([System.IO.File]::Exists($Item)) {
Try {
$FileStream = [System.IO.File]::Open($Item, 'Open', 'Write')
$FileStream.Close()
$FileStream.Dispose()
$IsLocked = $False
return $False
}
Catch [System.UnauthorizedAccessException] {
$IsLocked = 'AccessDenied'
}
Catch {
$IsLocked = $True
return $True
}
[pscustomobject]@{
File = $Item
IsLocked = $IsLocked
}
}
}
}
}