Powershell - JSON 格式转换为 PAC 文件

Powershell - JSON format to PAC file convert

我已使用以下代码显示 JSON 结果,但现在需要更改脚本以显示输出而不是并排显示。我试过如下的脚本,但似乎无法让它做我想做的事。

我的问题是:

这是我的脚本:

    $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}"

输出:

// 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;
}

我想要的输出:

// 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;
}
# 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"))

我明白了 - 简单的计数器方法:

$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++
}

可能需要整理制表符。

我会使用 Here-String 和一组预格式化的 shExpMatch(..) 行。 使用它还可以使您免于使用 +=

双引号和字符串连接
# 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

输出:

// 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;
}

根据要求,我认为代码的顶部,您在 arraylist 中收集 url 可以更容易地完成。

之前的一个注意事项:您正在使用带有字符串 "12".
$likeFilter 变量 在这种情况下,您最好使用 -eq 运算符而不是 -like 运算符,后者更适合使用通配符进行过滤(即 "12*")。

目前,我假设您只想获得 ID 与 "12" 完全匹配的服务。

$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