将 Powershell 脚本转换为 jenkins 作业
Converting Powershell script to jenkins job
我有以下功能:
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 的答案,而是对原始 PowerShell 函数的重写,它以非常低效的方式拆分文件。
(不能在评论中这样做)
这利用了 Get-Content
的 -ReadCount
参数,它指定一次通过管道发送多少行内容。
此外,我已重命名函数以符合 Verb-Noun 命名建议。
function Split-File ([string]$file, [string]$ruleName, [int]$maxLines = 40) {
$suffix = 0
$path = [System.IO.Path]::GetDirectoryName($file)
Get-Content -Path $file -ReadCount $maxLines | ForEach-Object {
$_ | Set-Content -Path (Join-Path -Path $path -ChildPath ('{0}_{1}.txt' -f $ruleName, $suffix++))
}
}
您可以使用类似以下 groovy 的代码轻松实现此目的:
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 中处理所有逻辑更容易,包括这个函数。
我有以下功能:
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 的答案,而是对原始 PowerShell 函数的重写,它以非常低效的方式拆分文件。
(不能在评论中这样做)
这利用了 Get-Content
的 -ReadCount
参数,它指定一次通过管道发送多少行内容。
此外,我已重命名函数以符合 Verb-Noun 命名建议。
function Split-File ([string]$file, [string]$ruleName, [int]$maxLines = 40) {
$suffix = 0
$path = [System.IO.Path]::GetDirectoryName($file)
Get-Content -Path $file -ReadCount $maxLines | ForEach-Object {
$_ | Set-Content -Path (Join-Path -Path $path -ChildPath ('{0}_{1}.txt' -f $ruleName, $suffix++))
}
}
您可以使用类似以下 groovy 的代码轻松实现此目的:
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 中处理所有逻辑更容易,包括这个函数。