Powershell-JSON格式到PAC文件的转换

问题描述

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

我的问题是:

  • 我想删除最后一个括号之前的||if (shExpMatch(host,"*.lync.com") || shExpMatch(host,"*.teams.microsoft.com") || shExpMatch(host,"teams.microsoft.com") || ) 结果将是if (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,"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++
}

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

,

我将使用带有summary_object = model_fit.summary() 行预格式化集的Here-String。 使用它还可以使您避免使用shExpMatch(..)

将引号和字符串连接加倍
+=

输出:

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

按照要求,我认为在代码的顶部(将URL收集到arraylist中)可以容易得多。

之前有一个注释:您正在使用带有字符串# 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 的{​​{1}}变量。
在这种情况下,您可能最好使用$likeFilter运算符,而不是"12"运算符,该运算符更多用于通配符过滤(即-eq)。

现在,我假设您只想获取ID完全匹配-like的服务。

"12*"

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...