如何从设备名称列表中获取最大值



通过读取活动目录生成windows工作站计算机名称列表,我需要找到最高的数字,以便我可以用下一个可用的数字分配新设备-我没有任何成功这样做-如何做到这一点?正如您从名称列表中看到的,我也有序列中缺少的数字,理想情况下,我也想用新设备填充…

我用来从AD获取列表的代码如下。

((Get-ADComputer -Filter {operatingsystem -notlike "*server*" -and Name -like $NamingConvention -and enabled -eq "true"} -Credential $credential -server $ADServerIP).Name)

设备名称列表

PC01PC28PC29PC30PC31PC32PC33PC34PC35PC36PC37PC38PC40PC41PC42PC43PC44PC45PC46PC47PC27PC48PC26PC24PC179PC18PC180PC181PC182PC183PC184PC185PC186PC187PC188PC189PC19PC190PC191PC192PC21PC22PC23PC25PC178PC49PC51PC77PC78PC79PC80PC81PC83PC84PC85PC87PC88PC89PC90PC91PC92PC93PC94PC95PC96PC97PC76PC50PC75PC72PC52PC53PC54PC55PC56PC57PC59PC60PC61PC62PC63PC64PC65PC66PC67PC68PC69和PC70PC71PC73PC98PC177PC175PC115PC116PC117PC118PC119PC12PC120PC121PC122PC123PC124PC125PC126PC127PC128PC129PC13PC130PC131PC114PC132PC113PC111PC02PC03PC04PC06PC08PC09PC10PC100PC101PC102PC103PC104PC105PC106PC107PC108PC109PC11PC110PC112PC176PC133PC135PC158PC159PC16PC160PC161PC162PC163PC164PC165PC166PC167PC168PC169PC17PC170PC171PC172PC173PC174PC157PC134PC156PC154PC136PC137PC138PC139PC14PC140PC141PC142PC143PC144PC145PC146PC147PC148PC149PC150PC151PC152PC153PC155PC99

根据pc名的数值排序并选择最后一个:

$lastPC = (Get-ADComputer -Filter {operatingsystem -notlike "*server*" -and Name -like $NamingConvention -and enabled -eq "true"} -Credential $credential -server $ADServerIP).Name  | 
Sort-Object { [int]($_ -replace 'D+')} | Select-Object -Last 1

这是一个解决方案,它会给你最高的数字($dataMax),缺失的数字($dataMissing),下一个数字使用($dataNext)。下一个要使用的数字将是第一个缺失的数字,或者如果没有缺失的数字,那么它将是最高的数字+ 1

# load the computers list
$data = ((Get-ADComputer -Filter {operatingsystem -notlike "*server*" -and Name -like $NamingConvention -and enabled -eq "true"} -Credential $credential -server $ADServerIP).Name)
# create an array by splitting the data text using the "space" character as a delimiter
$data = $data.Split(" ")
# remove all the alpha characters ("PC"), leaving only the number values so it can be sorted easier
$dataCleaned = $data -replace "[^0-9]" , '' | sort { [int]$_ }
# after sorting the data, [-1] represents the last element in the array which will be the highest number
[int]$dataMax = $dataCleaned[-1]
# create a number range that represents all the numbers from 1 to the highest number
$range = 1..$dataMax | foreach-object { '{0:d2}' -f $_ }
# compare the created range against the numbers actually in the computer array to find the missing numbers
$dataMissing = @(compare $range $dataCleaned -PassThru)
# if there's a missing value, [0] represents the first element in the array of missing numbers
if ($dataMissing)
{
$dataNext = $dataMissing[0]
}
# if there's no missing values, the next value is the max value + 1
else
{
$dataMissing = "none"
$dataNext = $dataMax + 1
}
Write-Host "The highest number is:"('{0:d2}' -f $dataMax)
Write-Host "The missing numbers are: $dataMissing"
Write-Host "The next number to use is:" ('{0:d2}' -f $dataNext)

假设您的列表完全像它看起来的那样,那么这似乎是一种方法:

$List = 'PC01 PC28 PC29 PC30 PC31 PC32 PC33 PC34 PC35 PC36 PC37 PC38 PC40 PC41 PC42 PC43 PC44 PC45 PC46 PC47 PC27 PC48 PC26 PC24 PC179 PC18 PC180 PC181 PC182 PC183 PC184 PC185 PC186 PC187 PC188 PC189 PC19 PC190 PC191 PC192 PC21 PC22 PC23 PC25 PC178 PC49 PC51 PC77 PC78 PC79 PC80 PC81 PC83 PC84 PC85 PC87 PC88 PC89 PC90 PC91 PC92 PC93 PC94 PC95 PC96 PC97 PC76 PC50 PC75 PC72 PC52 PC53 PC54 PC55 PC56 PC57 PC59 PC60 PC61 PC62 PC63 PC64 PC65 PC66 PC67 PC68 PC69 PC70 PC71 PC73 PC98 PC177 PC175 PC115 PC116 PC117 PC118 PC119 PC12 PC120 PC121 PC122 PC123 PC124 PC125 PC126 PC127 PC128 PC129 PC13 PC130 PC131 PC114 PC132 PC113 PC111 PC02 PC03 PC04 PC06 PC08 PC09 PC10 PC100 PC101 PC102 PC103 PC104 PC105 PC106 PC107 PC108 PC109 PC11 PC110 PC112 PC176 PC133 PC135 PC158 PC159 PC16 PC160 PC161 PC162 PC163 PC164 PC165 PC166 PC167 PC168 PC169 PC17 PC170 PC171 PC172 PC173 PC174 PC157 PC134 PC156 PC154 PC136 PC137 PC138 PC139 PC14 PC140 PC141 PC142 PC143 PC144 PC145 PC146 PC147 PC148 PC149 PC150 PC151 PC152 PC153 PC155 PC99'
$NextNumber = ($List -split "s" | ForEach-Object { if ($_ -match 'PC(?<Number>d+)') { $Matches.Number } }  | Measure-Object -Maximum).Maximum + 1
$NextNumber
"PC$NextNumber"

最新更新