0

I have used the following code to display JSON results but now need to change the script to display the output instead of side by side. I have tried a script like below, but just cant seem to get it to do what I want.

My question is :

  • I want to remove || before the last bracket. if (shExpMatch(host, "*.lync.com") || shExpMatch(host, "*.teams.microsoft.com") || shExpMatch(host, "teams.microsoft.com") || ) As a result , it will be if (shExpMatch(host, "*.lync.com") || shExpMatch(host, "*.teams.microsoft.com") || shExpMatch(host, "teams.microsoft.com"))

  • I need to change the script to display the my desired output instead of side by side.

Here is my script :

    $result = Invoke-WebRequest "https://endpoints.office.com/endpoints/worldwide?noipv6&ClientRequestId=b10c5ed1-bad1-445f-b386-b919946339a7"
    $services = ConvertFrom-Json $result
    $likeFilter = "12"
    $services = $services | Where-Object { $_.id -like $likeFilter } 
    $urls = [System.Collections.ArrayList]@()
    
    $services
    
    
    
    
    function add_url($url){
    if(!$urls.Contains($url)){ $urls.Add($url); }
    }
    
    
    
    foreach($service in $services){
    
    foreach($url in $service.urls){ add_url($url);
    }
    }
    
    # OUTPUT
$txt_proxypacText += "// This PAC file will provide proxy config to Microsoft 365 services`r`n"
$txt_proxypacText += "//  using data from the public web service for all endpoints`r`n"
$txt_proxypacText += "function FindProxyForURL(url, host)`r`n"
$txt_proxypacText += "{`r`n"

$txt_proxypacText += "var direct = ""DIRECT"";`r`n"
$txt_proxypacText += "var proxyServer = ""PROXY 10.11.12.13:8080"";`r`n"
$txt_proxypacText += "host = host.toLowerCase();`r`n"
$txt_proxypacText += "if ("

foreach($url in $urls){
$txt_proxypacText += "shExpMatch(host, ""$url"") || "
}



$txt_proxypacText += ")`r`n"
$txt_proxypacText += "{`r`n"
$txt_proxypacText += "`r`n return direct;"
$txt_proxypacText += "`r`n}"
$txt_proxypacText += "`r`n return proxyServer;"
$txt_proxypacText += "`r`n}"

Output:

// 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 direct = "DIRECT";
var proxyServer = "PROXY 10.11.12.13:8080";
host = host.toLowerCase();
if (shExpMatch(host, "*.lync.com") || shExpMatch(host, "*.teams.microsoft.com") || shExpMatch(host, "teams.microsoft.com") || )
{

 return direct;
}
 return proxyServer;
}

My Desired Output :

// 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 direct = "DIRECT";
    var proxyServer = "PROXY 10.11.12.13:8080";

    host = host.toLowerCase();

    if(shExpMatch(host, "*.lync.com")
        || shExpMatch(host, "*.teams.microsoft.com")
        || shExpMatch(host, "teams.microsoft.com"))
    {
        return direct;
    }

    return proxyServer;
}
1
  • shExpMatch(host, "teams.microsoft.com") - Why you use shExpMatch here ? Just use host == "teams.microsoft.com" Commented Aug 11, 2020 at 7:29

3 Answers 3

0

I would make use of a Here-String with a preformated set of shExpMatch(..) lines. Using that also relieves you from doubling quotes and string concatenations using +=

# demo urls
$urls = "*.lync.com", "*.teams.microsoft.com", "teams.microsoft.com"


$hostMatches = $(for ($i = 0; $i -lt $urls.Count; $i++) {
    $prefix = if ($i -eq 0) { '' } else { '        || '}
    '{0}shExpMatch(host, "{1}")'-f $prefix,  $urls[$i]
}) -join [Environment]::NewLine


$txt_proxypacText = @"
// 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 direct = "DIRECT";
    var proxyServer = "PROXY 10.11.12.13:8080";
    host = host.toLowerCase();
    if ($hostMatches)
    {
        return direct;
    }

    return proxyServer;
}
"@

$txt_proxypacText

Output:

// 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 direct = "DIRECT";
    var proxyServer = "PROXY 10.11.12.13:8080";
    host = host.toLowerCase();
    if (shExpMatch(host, "*.lync.com")
        || shExpMatch(host, "*.teams.microsoft.com")
        || shExpMatch(host, "teams.microsoft.com"))
    {
        return direct;
    }

    return proxyServer;
}

As requested, I think the top part of the code, where you are gathering the urls in an arraylist can be done much easier.

One note before: You are using a $likeFilter variable with a string "12".
In that case, you could probably better use the -eq operator instead of the -like operator that has more use for filtering with wildcards (i.e. "12*").

For now, I'm assuming you want to get only the service with id matching "12" exactly.

$url    = "https://endpoints.office.com/endpoints/worldwide?noipv6&ClientRequestId=b10c5ed1-bad1-445f-b386-b919946339a7"
$filter = 12

# get an array of urls from the service(s) that get through the filter
$urls = ((Invoke-WebRequest $url | ConvertFrom-Json) | Where-Object { $_.id -eq $filter }).urls | Select-Object -Unique
Sign up to request clarification or add additional context in comments.

2 Comments

thanks Theo , I have a question. I don't want to use these lines ` function add_url($url){ if(!$urls.Contains($url)){ $urls.Add($url); } } foreach($service in $services){ foreach($url in $service.urls){ add_url($url); } }` Is there any alternative script ? What do you recommended?
@Arbelac Please see the edited answer. With this, you don't have to fiddle around with an ArrayList nor do you need a helper function. Cheers!
0
# EXAMPLE prepare begin
$urls = @(
    'microsoft.com',
    '*.microsoft.com',
    'teams.microsoft.com',
    '*.teams.microsoft.com')
# EXAMPLE prepare End

$urlLines = $urls | 
ForEach-Object { return $_.Trim() } |
ForEach-Object { 
    if($_.StartsWith('*.')) {
        return "shExpMatch(host, '$($_)')"
    } else { 
        return "host == '$($_)'" 
    }}
         
$innerIf = [String]::Join("`r`n" + (' ' * 8) + "|| ", $urlLines)

#// $txt_proxypacText += "    if ($($innerIf))"
Write-Host "    if ($($innerIf))"

# Output:
# if (host == "microsoft.com"
#    || shExpMatch(host, "*.microsoft.com")
#    || host == "teams.microsoft.com"
#    || shExpMatch(host, "*.teams.microsoft.com"))

Comments

0

I got this - simple counter method:

$counter = 0
foreach($url in $urls){
    If ($counter -eq $urls.Count){
        $txt_proxypacText += "shExpMatch(host, ""$url"") `r`n"
    }else{
        $txt_proxypacText += "shExpMatch(host, ""$url"") || `r`n"
    }
    $counter++
}

Probably needs some tidying up with the tab characters.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.