PowerShell | Compare Odd or Even Substring
So, we wanted to process our patching by splitting up the odd and even numbers on systems. The challenge presented was that the computer’s numbering scheme was a suffix at the end of the computer name. Like the ones below. So, I wrote a script to import the list and separate them by the last digit even in one file, odd in another and everything left (Char) to another file.
Comp-11
CompWS-14
Comp-25
CompAccount
CompDept-206
Comp-11
Computer-85
# Get Computers from File
$Computers = Get-Content C:\temp\ODD-Even.txt
#Array of Even Numbers
$EvenArray=@(2,4,6,8,0)
#Array of Odd Numbers
$OddArray=@(1,3,5,7,9)
#---------- Da Loop ---------
Foreach($Comp in $Computers){
#Get Length of Computer Name Minus 1
$Length = $Comp.Length - 1
#Acquire the Substring of the Computer Name
$Substring = $($Comp.Substring($Length))
#Check to see if Substring, last digiat in Computer Name is Even
if($EvenArray -Contains $Substring){$Comp | Tee-Object C:\temp\Even.txt -Append}
#Check to See if Substring, Last Digist of COmputer Name is Odd
if($OddArray -Contains $Substring){$Comp | Tee-Object C:\temp\Odd.txt -Append}
#Check if Not End in Numeric
if(($OddArray -NotContains $Substring) -and ($EvenArray -NotContains $Substring)){
#If Computer Does Not End with a Number
$Comp | Tee-Object C:\temp\NotDigit.txt -Append
}
}