Powershell - JSON 格式到 PAC 文件的转换 [英] Powershell - JSON format to PAC file convert
问题描述
我使用以下代码来显示 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]@()$服务函数 add_url($url){if(!$urls.Contains($url)){ $urls.Add($url);}}foreach($service 中的 $service){foreach($service.urls 中的 $url){ add_url($url);}}# 输出$txt_proxypacText += "//此 PAC 文件将为 Microsoft 365 服务提供代理配置`r`n";$txt_proxypacText += "//使用来自公共网络服务的数据用于所有端点`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($urls 中的 $url){$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}";
输出:
//此 PAC 文件将为 Microsoft 365 服务提供代理配置//对所有端点使用来自公共 Web 服务的数据函数 FindProxyForURL(url, host){var direct = "DIRECT";var proxyServer = "PROXY 10.11.12.13:8080";主机 = 主机.toLowerCase();if (shExpMatch(host, "*.lync.com") || shExpMatch(host, "*.teams.microsoft.com") || shExpMatch(host, "teams.microsoft.com") || ){直接返回;}返回代理服务器;}
我想要的输出:
//此 PAC 文件将为 Microsoft 365 服务提供代理配置//对所有端点使用来自公共 Web 服务的数据函数 FindProxyForURL(url, host){var direct = "DIRECT";var proxyServer = "PROXY 10.11.12.13:8080";主机 = 主机.toLowerCase();如果(shExpMatch(主机,*.lync.com")||shExpMatch(主机,*.teams.microsoft.com")||shExpMatch(host, "teams.microsoft.com")){直接返回;}返回代理服务器;}
我会使用带有一组预先格式化的 shExpMatch(..)
行的 Here-String.使用它也可以使您免于使用 +=
# 演示网址$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 [环境]::NewLine$txt_proxypacText = @"//此 PAC 文件将为 Microsoft 365 服务提供代理配置//对所有端点使用来自公共 Web 服务的数据函数 FindProxyForURL(url, host){var direct = "DIRECT";var proxyServer = "PROXY 10.11.12.13:8080";主机 = 主机.toLowerCase();如果($hostMatches){直接返回;}返回代理服务器;}"@$txt_proxypacText
输出:
<前>//此 PAC 文件将为 Microsoft 365 服务提供代理配置//对所有端点使用来自公共 Web 服务的数据函数 FindProxyForURL(url, host){var direct = "DIRECT";var proxyServer = "代理 10.11.12.13:8080";主机 = 主机.toLowerCase();if (shExpMatch(host, "*.lync.com")||shExpMatch(host, "*.teams.microsoft.com")||shExpMatch(host, "teams.microsoft.com")){直接返回;}返回代理服务器;}<小时>
根据要求,我认为代码的顶部,您在数组列表中收集 url 的地方可以更容易地完成.
之前的一个注意事项:您正在使用带有字符串 "12"
的 $likeFilter
变量.
在这种情况下,您可能会更好地使用 -eq
运算符而不是 -like
运算符,后者更适用于使用通配符进行过滤(即 "12*"
).
现在,我假设您只想获得 id 与 "12"
完全匹配的服务.
$url = https://endpoints.office.com/endpoints/worldwide?noipv6&ClientRequestId=b10c5ed1-bad1-445f-b386-b919946339a7"$过滤器= 12# 从通过过滤器的服务中获取一组 url$urls = ((Invoke-WebRequest $url | ConvertFrom-Json) | Where-Object { $_.id -eq $filter }).urls |选择对象 - 唯一
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 beif (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;
}
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
这篇关于Powershell - JSON 格式到 PAC 文件的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!