-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebscrape.ps1
103 lines (68 loc) · 2.19 KB
/
Webscrape.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
#need to check this page for an article modify date change
$URI = 'https://learn.microsoft.com/en-us/azure/security/fundamentals/azure-ca-details?tabs=root-and-subordinate-cas-list'
#region - Invoke-WebRequest - No content is included
$FirstTry = Invoke-webrequest $uri
#endregion
#region - let's try with Invoke-RestMethod - Yay! we seee the content
$secondTry = Invoke-RestMethod $URI
#endregion
#region - pattern we are looking for
# sample pattern: <meta name="ms.date" content="04/19/2024" />
# regex that works: (.*?)
$pattern = '<meta name="ms\.date" content=(.*?)\/>'
#endregion
#region - test the pattern
$pattern = '<meta name="ms\.date" content=(.*?)\/>'
$HTML = Invoke-RestMethod $URI
$HTML -match $pattern
#endregion
#region - see the matches
$Matches
$matches.1
#endregion
#region - put it all together
$URI = 'https://learn.microsoft.com/en-us/azure/security/fundamentals/azure-ca-details?tabs=root-and-subordinate-cas-list'
$pattern = '<meta name="ms\.date" content=(.*?)\/>'
$HTML = Invoke-RestMethod $URI
$HTML -match $pattern | Out-Null
$Matches.1
#endregion
Try {
Test-Connection "fakesystem" -Count 1 -ErrorAction 'Stop'
} Catch [System.Net.NetworkInformation.PingException] {
"Ping Exception"
} Catch {
"Unknown Exception"
}
Clear-Host
try {
# Code that may throw exceptions
Get-ChildItem -Path "C:\temp2" -ErrorAction Stop
} catch [System.Management.Automation.ItemNotFoundException] {
Write-Host "Folder not found."
} catch [System.Management.Automation.MethodInvocationException] {
Write-Host "Error invoking method."
} catch {
Write-Host "An error occurred"
}
try {
# The code that may throw an exception goes here
$result = 10 / 0
} catch [System.Management.Automation.ItemNotFoundException] {
Write-Host "The specified folder does not exist."
} catch {
Write-Host "An error occurred: $_"
}
try {
$file = [System.IO.File]::OpenWrite("C:\example.txt")
Write-Output "File opened for writing."
# Code that writes to the file
$file.WriteLine("Hello, World!")
} catch {
Write-Host "An error occurred: $_"
} finally {
if ($file -ne $null) {
$file.Close()
Write-Output "File handle closed."
}
}