循环两个变量 [英] Loop for two variables
问题描述
我有以下ps1文件(下面的脚本,很遗憾,它必须是PowerShell,没有比这更简单的东西),运行一个curl
请求,将其中的一部分取出到一个文件中,然后运行一些文本替换.
I have the following ps1 file (script below, unfortunately it must be PowerShell, not anything easier), running a curl
request, taking a piece of it out to a file, then running some text replacements.
$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个不同事务的数组或列表.
The script as it is gives me one transaction value (Three rows in the csv: the transaction, the node, and a number). In practice I'd need to use an array or list with 10 machines, and 4 different transactions.
我的问题是为两个变量设置一个循环,我需要运行OUTPUT部分40次(4个事务X 10个节点).我已经在Python,bash中完成了类似的工作,但是发现自己在PowerShell中感到困惑和过时.我花了几个小时在网上跑来跑去,尝试不起作用的示例.
My problem is setting up a loop for two variables, I need to run the OUTPUT part 40 times (4 transactions X 10 nodes). I've done similair stuff in Python, bash but I find myself baffled and out of time in PowerShell. I spent hours running around the web and trying non-working examples.
你能给我个手吗?
推荐答案
我在您的变量中添加了s,然后假定第一个事务"需要在外部循环中:
I've added an s to your variables and then assumed the first "transactions" would need to be in the outerloop:
$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
}
}
这篇关于循环两个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!