Skip to content

Commit 9b9ecf4

Browse files
committed
Added try catch
1 parent 1b6e688 commit 9b9ecf4

File tree

1 file changed

+52
-48
lines changed

1 file changed

+52
-48
lines changed

tools/ImportExamples.ps1

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,49 @@ function Start-Generator {
2121
if ($GenerationMode -eq "auto") {
2222
#Test path first
2323
if (Test-Path $CommandMetadataPath) {
24-
$CommandMetadataContent = Get-Content $CommandMetadataPath | ConvertFrom-Json
25-
$CommandMetadataContent | ForEach-Object {
26-
$GraphCommand = $_.Command
27-
$GraphModule = $_.Module
28-
$UriPath = $_.Uri
29-
$ExternalDocsUrl = $_.ApiReferenceLink
30-
$ApiVersion = $_.ApiVersion
31-
$ProfilePathMapping = "v1.0\examples"
32-
if ($ApiVersion -eq "beta") {
33-
$ProfilePathMapping = "beta\examples"
34-
}
35-
$ModulePath = Join-Path $PSScriptRoot "..\src\$GraphModule\$ProfilePathMapping"
36-
37-
$ExampleFile = "$ModulePath\$GraphCommand.md"
38-
Test-Commands -Command $GraphCommand -CommandPath $ExampleFile
39-
if ($null -ne $ExternalDocsUrl) {
40-
if (-not (Test-Path $ExampleFile)) {
41-
New-Item -Path $ExampleFile -ItemType File -Force
24+
$CommandMetadataContent = Get-Content $CommandMetadataPath | ConvertFrom-Json
25+
$CommandMetadataContent | ForEach-Object {
26+
$GraphCommand = $_.Command
27+
$GraphModule = $_.Module
28+
$UriPath = $_.Uri
29+
$ExternalDocsUrl = $_.ApiReferenceLink
30+
$ApiVersion = $_.ApiVersion
31+
$ProfilePathMapping = "v1.0\examples"
32+
if ($ApiVersion -eq "beta") {
33+
$ProfilePathMapping = "beta\examples"
4234
}
43-
$IntuneUrl = $ExternalDocsUrl.Replace("intune-onboarding-", "")
44-
$IntuneUrl = $IntuneUrl.Replace("intune-mam-", "")
45-
$IsValid = IsValidEndPoint -GraphEndpoint $IntuneUrl
46-
if ($IsValid) {
47-
$ExternalDocsUrl = $IntuneUrl
35+
$ModulePath = Join-Path $PSScriptRoot "..\src\$GraphModule\$ProfilePathMapping"
36+
37+
$ExampleFile = "$ModulePath\$GraphCommand.md"
38+
Test-Commands -Command $GraphCommand -CommandPath $ExampleFile
39+
if ($null -ne $ExternalDocsUrl) {
40+
if (-not (Test-Path $ExampleFile)) {
41+
New-Item -Path $ExampleFile -ItemType File -Force
42+
}
43+
$IntuneUrl = $ExternalDocsUrl.Replace("intune-onboarding-", "")
44+
$IntuneUrl = $IntuneUrl.Replace("intune-mam-", "")
45+
$IsValid = IsValidEndPoint -GraphEndpoint $IntuneUrl
46+
if ($IsValid) {
47+
$ExternalDocsUrl = $IntuneUrl
48+
}
49+
Start-WebScrapping -GraphProfile $ApiVersion -ExternalDocUrl $ExternalDocsUrl -Command $GraphCommand -GraphProfilePath $ModulePath -UriPath $UriPath -Module $GraphModule
4850
}
49-
Start-WebScrapping -GraphProfile $ApiVersion -ExternalDocUrl $ExternalDocsUrl -Command $GraphCommand -GraphProfilePath $ModulePath -UriPath $UriPath -Module $GraphModule
50-
}
51-
else {
52-
if (Test-Path $ExampleFile) {
53-
#Check file content and retain correct examples
54-
$Content = Get-Content -Path $ExampleFile
55-
Clear-Content $ExampleFile -Force
56-
if ($Content | Select-String -pattern $GraphCommand) {
57-
Retain-ExistingCorrectExamples -Content $Content -File $ExampleFile -CommandPattern $GraphCommand
58-
}
51+
else {
52+
if (Test-Path $ExampleFile) {
53+
#Check file content and retain correct examples
54+
$Content = Get-Content -Path $ExampleFile
55+
Clear-Content $ExampleFile -Force
56+
if ($Content | Select-String -pattern $GraphCommand) {
57+
Retain-ExistingCorrectExamples -Content $Content -File $ExampleFile -CommandPattern $GraphCommand
58+
}
59+
}
5960
}
60-
}
6161

62+
}
63+
}
64+
else {
65+
Write-Host "The path to the command metadata file is invalid. Please ensure that the path is correct"
6266
}
63-
}
64-
else {
65-
Write-Host "The path to the command metadata file is invalid. Please ensure that the path is correct"
66-
}
6767
}
6868
else {
6969

@@ -199,20 +199,24 @@ function IsValidEndPoint {
199199
param(
200200
[string] $GraphEndpoint
201201
)
202+
try {
203+
$HTTP_Request = [System.Net.WebRequest]::Create($GraphEndpoint)
202204

203-
$HTTP_Request = [System.Net.WebRequest]::Create($GraphEndpoint)
204-
205-
# We then get a response from the site.
206-
$HTTP_Response = $HTTP_Request.GetResponse()
205+
# We then get a response from the site.
206+
$HTTP_Response = $HTTP_Request.GetResponse()
207207

208-
# We then get the HTTP code as an integer.
209-
$HTTP_Status = [int]$HTTP_Response.StatusCode
208+
# We then get the HTTP code as an integer.
209+
$HTTP_Status = [int]$HTTP_Response.StatusCode
210210

211-
If ($HTTP_Status -eq 200) {
212-
return $True
211+
If ($HTTP_Status -eq 200) {
212+
return $True
213+
}
214+
If ($HTTP_Response -ne $null) { $HTTP_Response.Close() }
215+
return $False
216+
}
217+
catch {
218+
return $False
213219
}
214-
If ($HTTP_Response -ne $null) { $HTTP_Response.Close() }
215-
return $False
216220
}
217221
function FetchStream {
218222
param(

0 commit comments

Comments
 (0)