Get-PacFile.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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. <#PSScriptInfo .VERSION 1.0.4 .AUTHOR Microsoft Corporation .GUID 7f692977-e76c-4582-97d5-9989850a2529 .COMPANYNAME Microsoft .COPYRIGHT Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. .TAGS PAC Microsoft Microsoft365 365 .LICENSEURI .PROJECTURI http://aka.ms/ipurlws .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .SYNOPSIS Create a PAC file for Microsoft 365 prioritized connectivity .DESCRIPTION This script will access updated information to create a PAC file to prioritize Microsoft 365 Urls for better access to the service. This script will allow you to create different types of files depending on how traffic needs to be prioritized. .PARAMETER Instance The service instance inside Microsoft 365. .PARAMETER ClientRequestId The client request id to connect to the web service to query up to date Urls. .PARAMETER DirectProxySettings The direct proxy settings for priority traffic. .PARAMETER DefaultProxySettings The default proxy settings for non priority traffic. .PARAMETER Type The type of prioritization to give. Valid values are 1 and 2, which are 2 different modes of operation. Type 1 will send Optimize traffic to the direct route. Type 2 will send Optimize and Allow traffic to the direct route. .PARAMETER Lowercase Flag this to include lowercase transformation into the PAC file for the host name matching. .PARAMETER TenantName The tenant name to replace wildcard Urls in the webservice. .PARAMETER ServiceAreas The service areas to filter endpoints by in the webservice. .PARAMETER FilePath The file to print the content to. .EXAMPLE Get-PacFile.ps1 -ClientRequestId b10c5ed1-bad1-445f-b386-b919946339a7 -DefaultProxySettings "PROXY 4.4.4.4:70" -FilePath type1.pac .EXAMPLE Get-PacFile.ps1 -ClientRequestId b10c5ed1-bad1-445f-b386-b919946339a7 -Instance China -Type 2 -DefaultProxySettings "PROXY 4.4.4.4:70" -FilePath type2.pac .EXAMPLE Get-PacFile.ps1 -ClientRequestId b10c5ed1-bad1-445f-b386-b919946339a7 -Instance WorldWide -Lowercase -TenantName tenantName -ServiceAreas Sharepoint,Skype #> #Requires -Version 2 [CmdletBinding(SupportsShouldProcess=$True)] Param ( [Parameter(Mandatory = $false)] [ValidateSet('Worldwide', 'Germany', 'China', 'USGovDoD', 'USGovGCCHigh')] [String] $Instance = "Worldwide", [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [guid] $ClientRequestId, [Parameter(Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $DirectProxySettings = 'DIRECT', [Parameter(Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $DefaultProxySettings = 'PROXY 10.10.10.10:8080', [Parameter(Mandatory = $false)] [ValidateRange(1, 2)] [int] $Type = 1, [Parameter(Mandatory = $false)] [switch] $Lowercase = $false, [Parameter(Mandatory = $false)] [ValidateNotNullOrEmpty()] [string] $TenantName, [Parameter(Mandatory = $false)] [ValidateSet('Exchange', 'Skype', 'SharePoint', 'Common')] [string[]] $ServiceAreas, [Parameter(Mandatory = $false)] [ValidateNotNullOrEmpty()] [string] $FilePath ) ################################################################################################################## ### Global constants ################################################################################################################## $baseServiceUrl = "https://endpoints.office.com/endpoints/$Instance/?ClientRequestId={$ClientRequestId}" $directProxyVarName = "direct" $defaultProxyVarName = "proxyServer" $bl = "`r`n" ################################################################################################################## ### Functions to create PAC files ################################################################################################################## function Get-PacClauses { param( [Parameter(Mandatory = $false)] [string[]] $Urls, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $ReturnVarName ) if (!$Urls) { return "" } $clauses = (($Urls | ForEach-Object { "shExpMatch(host, `"$_`")" }) -Join "$bl || ") @" if($clauses) { return $ReturnVarName; } "@ } function Get-PacString { param( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [array[]] $MapVarUrls ) @" // This PAC file will provide proxy config to Microsoft 365 services // using data from the public web service for all endpoints function FindProxyForURL(url, host) { var $directProxyVarName = "$DirectProxySettings"; var $defaultProxyVarName = "$DefaultProxySettings"; $( if ($Lowercase) { " host = host.toLowerCase();" }) $( ($MapVarUrls | ForEach-Object { Get-PACClauses -ReturnVarName $_.Item1 -Urls $_.Item2 }) -Join "$bl$bl" ) return $defaultProxyVarName; } "@ -replace "($bl){3,}","$bl$bl" # Collapse more than one blank line in the PAC file so it looks better. } ################################################################################################################## ### Functions to get and filter endpoints ################################################################################################################## function Get-Regex { param( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $Fqdn ) return "^" + $Fqdn.Replace(".", "\.").Replace("*", ".*").Replace("?", ".") + "$" } function Match-RegexList { param( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $ToMatch, [Parameter(Mandatory = $false)] [string[]] $MatchList ) if (!$MatchList) { return $false } foreach ($regex in $MatchList) { if ($regex -ne $ToMatch -and $ToMatch -match (Get-Regex $regex)) { return $true } } return $false } function Get-Endpoints { $url = $baseServiceUrl if ($TenantName) { $url += "&TenantName=$TenantName" } if ($ServiceAreas) { $url += "&ServiceAreas=" + ($ServiceAreas -Join ",") } return Invoke-RestMethod -Uri $url } function Get-Urls { param( [Parameter(Mandatory = $false)] [psobject[]] $Endpoints ) if ($Endpoints) { return $Endpoints | Where-Object { $_.urls } | ForEach-Object { $_.urls } | Sort-Object -Unique } return @() } function Get-UrlVarTuple { param( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $VarName, [Parameter(Mandatory = $false)] [string[]] $Urls ) return New-Object 'Tuple[string,string[]]'($VarName, $Urls) } function Get-MapVarUrls { Write-Verbose "Retrieving all endpoints for instance $Instance from web service." $Endpoints = Get-Endpoints if ($Type -eq 1) { $directUrls = Get-Urls ($Endpoints | Where-Object { $_.category -eq "Optimize" }) $nonDirectPriorityUrls = Get-Urls ($Endpoints | Where-Object { $_.category -ne "Optimize" }) | Where-Object { Match-RegexList $_ $directUrls } return @( Get-UrlVarTuple -VarName $defaultProxyVarName -Urls $nonDirectPriorityUrls Get-UrlVarTuple -VarName $directProxyVarName -Urls $directUrls ) } elseif ($Type -eq 2) { $directUrls = Get-Urls ($Endpoints | Where-Object { $_.category -in @("Optimize", "Allow")}) $nonDirectPriorityUrls = Get-Urls ($Endpoints | Where-Object { $_.category -notin @("Optimize", "Allow") }) | Where-Object { Match-RegexList $_ $directUrls } return @( Get-UrlVarTuple -VarName $defaultProxyVarName -Urls $nonDirectPriorityUrls Get-UrlVarTuple -VarName $directProxyVarName -Urls $directUrls ) } } ################################################################################################################## ### Main script ################################################################################################################## $content = Get-PacString (Get-MapVarUrls) if ($FilePath) { $content | Out-File -FilePath $FilePath -Encoding ascii } else { $content } |