2017-09-12 234 views
0

我有下面的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 [email protected] $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中完成了相似的東西,但是我發現自己在PowerShell中感到困惑並且沒有時間。我花了幾個小時在網絡上運行,並嘗試了無法使用的示例。

你能給我一隻手嗎?

+2

這是不如何你會在PowerShell中處理這些事情。完全一樣。請提供示例'curl'輸出(未經過濾)以及您想要從中創建的所需CSV輸出。 –

+0

此外,'-replace'使用正則表達式 – TheIncorrigible1

+0

完成後的URL看起來是什麼樣子:https:// serv/trans = 1node1? – TheIncorrigible1

回答

2

我已經添加了一個s變量和假設,然後第一個「交易」將需要在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 [email protected] $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 
    } 
} 
+0

我建議把外部調用改成'&'路徑\到\ curl.exe',如果它是'PSv3' +,'&'路徑\到\ curl.exe' - %' – TheIncorrigible1

+1

或者不是捲曲使用Invoke-webrequest –

+0

謝謝,我將Get-Content部分從循環中取出,並在腳本的末尾添加了Stop-Process到ps,否則如果多次運行,整個事件都會掛起。 –

1

我仍然認爲處理捲曲輸出將是解析的最佳方式與它返回的對象類型(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 [email protected] $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" 
+0

TL; DR和問題解決了findstr和value.Replace調用,但供將來使用 - 我將如何解析與PowerShell的XML?捲曲返回一堆東西行,我只需要從行中的號碼:號碼。 –