-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFixPrivateLinkScopeErrors.ps1
More file actions
63 lines (63 loc) · 2.78 KB
/
FixPrivateLinkScopeErrors.ps1
File metadata and controls
63 lines (63 loc) · 2.78 KB
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
<#.Synopsis
Fix PrivateLinkScopeErrors
.DESCRIPTION
Script will check the azcmagent.log for PrivateLinkScopeErrors and add the resolved hostnames to the local hosts file.
.CREATEDBY
Jim Gandy
Tommy Paulk
.NOTES
#>
Clear-Host
Write-Host "Checking for PrivateLinkScopeErrors..."
$LogPath="C:\programdata\AzureConnectedMachineAgent\Log\azcmagent.log"
$FailingHostname = Get-Content $LogPath | Select-String -Pattern 'PrivateLinkScopeErrors' | ForEach-Object { if ($_ -match '(\S+\.azure\.com)') { $matches[1] } } | sort -Unique
IF($FailingHostname){
Write-Host "FOUND: PrivateLinkScopeErrors"
$FailingHostname | %{Write-Host " $_"}
$hostnameToIps=@()
foreach ($hostname in $FailingHostname){
Write-Host "DNS Lookup $hostname with 8.8.8.8..."
$dip=(Resolve-DnsName $hostname -Type A -Server 8.8.8.8 -ErrorAction SilentlyContinue).IPAddress
if (!$dip) {
Write-Host " No Response from 8.8.8.8 Trying SimpleDNS.plus/lookup"
Try{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$response=Invoke-WebRequest 'https://simpledns.plus/lookup' -Method Default -SessionVariable rb # -Method Post -Body $formFields -ContentType "application/x-www-form-urlencoded"
$form=$response.forms[0]
$form.Fields["domain"]=$hostname
$form.Fields["server"]='8.8.8.8'
$form.Fields["recType"]='A'
$dip=Invoke-WebRequest -Uri ('https://simpledns.plus/lookup') -Body $form -Method POST -WebSession $rb -Headers @{'Content-Type' = 'application/x-www-form-urlencoded'}
$dip= [Regex]::Matches($dip.Content.split("\n"),".*?((\d*\.){3}\d)")[1].groups[1].value}
Finally{
if (!$dip) {
Write-Host " ERROR: Failed to resolve $hostname" -ForegroundColor Red
}Else{
Write-Host " SUCCESS: $dip`t$hostname" -ForegroundColor Green
}
}
}Else{
Write-Host " SUCCESS: $dip`t$hostname" -ForegroundColor Green
}
if ($dip) {
$hostnameToIps+="$dip`t$hostname"
}
}
$hostnamesfile=Get-Content -Path "C:\Windows\System32\drivers\etc\hosts"
if (!($hostnamesfile -match $hostname)){
Try{
Write-Host "Adding the following to C:\Windows\System32\drivers\etc\hosts"
$hostnameToIps
$Run = Read-Host "Append? [y/n]"
If ($run -ine "y"){Break}
$hostnameToIps | Out-File -FilePath "C:\Windows\System32\drivers\etc\hosts" -Append
}
Catch{
$errout=$Error[0]
Write-Host " ERROR: $errout" -ForegroundColor Red
Write-Host " Copy/Paste manually or RunAs Administrator" -ForegroundColor Red
}
}
}Else{
Write-Host "No PrivateLinkScopeErrors"
}