我有以下功能:
function DivideAndCreateFiles ([string] $file, [string] $ruleName) {
$Suffix = 0
$FullData = Get-Content $file
$MaxIP = 40
while ($FullData.count -gt 0 ){
$NewData = $FullData | Select-Object -First $MaxIP
$FullData = $FullData | Select-Object -Skip $MaxIP
$NewName = "$ruleName$Suffix"
New-Variable -name $NewName -Value $NewData
Get-Variable -name $NewName -ValueOnly | out-file "$NewName.txt"
$Suffix++
}
}
這個函數需要一個文件位置,這個文件包含數百個ips。然後它迭代文件並從中創建文件,每個文件都包含 40 個 ip,命名為 $rulename$suffix 所以如果 $rulename=blabla
我會得到 blabla_0 、 blabla_1 、 .. 等等,每個都有 40 ips。
我需要轉換此邏輯並將其放入 jenkins 作業中。此文件位於作業的工作目錄中,名為 ips.txt
suffix = 0
maxIP = 40
ips = readFile('ips.txt')
...
我很高興在這裡重新編寫這個函數的一些幫助,我對 jenkins / groovy 很陌生
在advacne感謝
您可以使用類似於以下常規代碼的內容輕鬆實現這一點:
def divideAndCreateFiles(path, rule_name, init_suffix = 0, max_ip = 40){
// read the file and split lines by new line separator
def ips = readFile(path).split("\n").trim()
// divide the IPs into groups according to the maximum ip range
def ip_groups = ips.collate(ips.size().intdiv(max_ip))
// Iterate over all groups and write them to the corresponding file
ip_groups.eachWithIndex { group, index ->
writeFile file : "${rule_name}${init_suffix + index}", text: group.join("\n")
}
}
在我看來,因為您已經在 groovy 中編寫了一個完整的管道,所以在 groovy 中處理所有邏輯更容易,包括這個函數。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句