【问题标题】:Loop for two variables循环两个变量
【发布时间】:2017-09-12 14:48:34
【问题描述】:

我有以下 ps1 文件(下面的脚本,不幸的是它必须是 PowerShell,而不是更简单的东西),运行 curl 请求,将其中的一部分取出到文件中,然后运行一些文本替换。

$url = "https://someserver/trans="
$transaction = "1" #There are 4 transactions
$node = "node1" #There are 10 nodes

Remove-Item ATM.csv -Force

# So far so good
# Below is what I'd use as a function in bash. No sure what/how to do in PS:
#OUTPUT:

echo $transaction";"$node >> ATM.csv
curl -v -k -u user@pass $url$transaction$node | findstr "<value>" >> ATM.csv
(Get-Content ATM.csv) -replace "<value>"," " | Out-File ATM.csv
(Get-Content ATM.csv) -replace "</value>"," " | Out-File ATM.csv
(Get-Content ATM.csv) -replace " ","" | Out-File ATM.csv

脚本原样为我提供了一个交易值(csv 中的三行:交易、节点和一个数字)。在实践中,我需要使用包含 10 台机器和 4 个不同事务的数组或列表。

我的问题是为两个变量设置一个循环,我需要运行 OUTPUT 部分 40 次(4 个事务 X 10 个节点)。我在 Python、bash 中做过类似的事情,但我发现自己在 PowerShell 中感到困惑和没时间。我花了几个小时在网上跑来跑去,尝试一些无效的例子。

你能帮帮我吗?

【问题讨论】:

  • 这不是您在 PowerShell 中处理这些事情的方式。完全没有。请提供示例curl 输出(未过滤),以及您要从中创建的所需 CSV 输出。
  • 另外,-replace 使用正则表达式
  • 完成后的 URL 是什么样子的,https://serv/trans=1node1
  • 您的代码说 4 个事务 10 个节点,而文本说反之亦然?要处理 html/xml/json 返回,有比使用 findstr.exe 更好的方法。 edit 你的问题包含一个详细的返回和你预期的 csv 最终结果? ; 分隔符。
  • @LotPings - 4 个交易 10 个节点,修复了文本中的错误 :-)

标签: windows loops powershell export-to-csv


【解决方案1】:

我已经在你的变量中添加了一个 s,然后假设第一个“事务”需要在外循环中:

$url = "https://someserver/trans="
$transactions = '1','2','3','4' #There are 4 transactions
$nodes = 'node1','node2','node3','node4','node5','node6' #There are 10 nodes

Remove-Item ATM.csv -Force

# So far so good
# Below is what I'd use as a function in bash. No sure what/how to do in PS:
#OUTPUT:
foreach($transaction in $transactions)
{
    foreach($node in $nodes)
    {
    "$transaction;$node" |out-file -Append ATM.csv
    curl -v -k -u user@pass $url$transaction$node | findstr "<value>" | out-file -Append ATM.csv
    (Get-Content ATM.csv) -replace "<value>"," " | Out-File -Append ATM.csv
    (Get-Content ATM.csv) -replace "</value>"," " | Out-File -Append ATM.csv
    (Get-Content ATM.csv) -replace " ","" | Out-File -Append ATM.csv
    }
}

【讨论】:

  • 我建议将外部调用更改为&amp; 'path\to\curl.exe',如果是PSv3+,&amp; 'path\to\curl.exe' --%
  • 或者代替 curl 使用 Invoke-webrequest
  • 谢谢,我将 Get-Content 部分从循环中取出,并在脚本末尾添加了 Stop-Process 到 ps,否则如果运行不止一次,整个事情就会挂起。
【解决方案2】:

我仍然认为处理 curl 输出的最佳方法是解析它返回的对象类型 (html/xml/json)。

在解析文本输出时,我会使用正则表达式。 PowerShell 支持lookarounds 所以这个脚本(节点交易顺序相反):

## Q:\Test\2017\09\13\SO_46179493.ps1
$url = "https://someserver/trans="
$transactions = 1..4|ForEach-Object{"$_"}       #There are 4 transactions
$nodes = 1..10|ForEach-Object{"node{0}" -f $_}  #There are 10 nodes
$RE = [RegEx]'(?<=\<value\>).+(?=\<\/value\>)'
# The RE uses lookbehind and lookahead to assert the captured value is surrounded by
# the tags <value> and </value>

$ATM = ForEach($node in $nodes){
    ForEach($transaction in $transactions) {
      curl -v -k -u user@pass $url$transaction$node | Where-Object {$_ -match $RE}|
        ForEach-Object {[pscustomobject]@{
          node = $node
          transaction = $transaction
          value = $Matches[0]}
        }
    }
}
$ATM
$ATM | Export-Csv '.\ATM.csv' -NoTypeInformation

将产生这个(模拟的)显示:

node   transaction value
----   ----------- -----
node1  1           79719
node1  2           77829
node1  3           90337
node1  4           39470
...
node10 1           17294
node10 2           62468
node10 3           70542
node10 4           46147

和文件输出:

> cat .\ATM.csv
"node","transaction","value"
"node1","1","79719"
"node1","2","77829"
"node1","3","90337"
"node1","4","39470"
...
"node10","1","17294"
"node10","2","62468"
"node10","3","70542"
"node10","4","46147"

【讨论】:

  • TL;DR 问题已通过 findstr 和 value.Replace 调用解决,但为了将来使用 - 我将如何使用 powershell 解析 xml? curl 返回一堆 something 行,我只需要该行中的数字: number.
猜你喜欢
  • 1970-01-01
  • 2013-02-11
  • 1970-01-01
  • 2013-09-10
  • 2016-12-13
  • 2016-10-31
  • 2017-02-27
  • 2014-03-29
  • 2017-03-01
相关资源
最近更新 更多