-
Notifications
You must be signed in to change notification settings - Fork 2
/
chapter_11.ps1
40 lines (32 loc) · 1.12 KB
/
chapter_11.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
#region Force Hard-Terminating Errors
$someFile = Get-Item -Path 'a file that does not exist'
$acl = Get-Acl -Path $someFile.FullName
Set-Acl -Path 'a file that exists' -AclObject $acl
$someFile = Get-Item -Path 'a file that does not exist' -ErrorAction Stop
$acl = Get-Acl -Path $someFile.FullName
Set-Acl -Path 'a file that exists' -AclObject $acl
try {
$someFile = Get-Item -Path 'a file that does not exist' -ErrorAction Stop
$acl = Get-Acl -Path $someFile.FullName
Set-Acl -Path 'a file that exists' -AclObject $acl
} catch {
Write-Warning -Message $_.Exception.Message
}
$ErrorActionPreference = 'Stop'
try {
$someFile = Get-Item -Path 'a file that does not exist'
$acl = Get-Acl -Path $someFile.FullName
Set-Acl -Path 'a file that exists' -AclObject $acl
} catch {
Write-Warning -Message $_.Exception.Message
}
#endregion
#region Avoid Using $?
$someFile = Get-Item -Path 'a file that does not exist' -ErrorAction Ignore
if (-not $?) {
Write-Warning -Message $_.Exception.Message
} else {
$acl = Get-Acl -Path $someFile.FullName
Set-Acl -Path 'a file that exists' -AclObject $acl
}
#endregion