【问题标题】:Powershell - JSON format to PAC file convertPowershell - JSON 格式到 PAC 文件的转换
【发布时间】:2020-08-11 07:05:25
【问题描述】:

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

我的问题是:

  • 我想删除最后一个括号之前的||if (shExpMatch(host, "*.lync.com") || shExpMatch(host, "*.teams.microsoft.com") || shExpMatch(host, "teams.microsoft.com") || ) 结果是if (shExpMatch(host, "*.lync.com") || shExpMatch(host, "*.teams.microsoft.com") || shExpMatch(host, "teams.microsoft.com"))

  • 我需要更改脚本以显示我想要的输出,而不是并排显示。

这是我的脚本:

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

【问题讨论】:

  • shExpMatch(host, "teams.microsoft.com") - 为什么在这里使用 shExpMatch?只需使用 host == "teams.microsoft.com"

标签: json powershell office365 pac


【解决方案1】:

我会使用带有一组预先格式化的shExpMatch(..) 行的Here-String。 使用它还可以使您免于使用 += 加倍引号和字符串连接

# 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

输出:

// 此 PAC 文件将为 Microsoft 365 服务提供代理配置 // 为所有端点使用来自公共 Web 服务的数据 函数 FindProxyForURL(url, 主机) { 变种直接=“直接”; var proxyServer = "代理服务器 10.11.12.13:8080"; 主机 = host.toLowerCase(); if (shExpMatch(host, "*.lync.com") || shExpMatch(主机,“*.teams.microsoft.com”) || shExpMatch(主机,“teams.microsoft.com”)) { 直接退货; } 返回代理服务器; }

根据要求,我认为代码的顶部,您在 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

【讨论】:

  • 谢谢西奥,我有一个问题。我不想使用这些行 `function add_url($url){ if(!$urls.Contains($url)){ $urls.Add($url); } } foreach($services 中的$service){ foreach($service.urls 中的$url){ add_url($url); } 有没有替代脚本?你有什么推荐的?
  • @Arbelac 请查看编辑后的答案。有了这个,您不必摆弄 ArrayList 也不需要辅助函数。干杯!
【解决方案2】:
# 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"))

【讨论】:

    【解决方案3】:

    我知道了 - 简单的计数器方法:

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

    可能需要整理一下制表符。

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 1970-01-01
      • 2021-10-15
      • 2015-10-03
      • 2018-07-16
      • 2015-12-17
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      相关资源
      最近更新 更多