如何解决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*"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。