The code you posted had a lot of syntax errors and I don’t know if it’s indented in your editor, but every time you don’t indent your code a puppy dies, so you should really do it. When the code is indented, the logic does not make sense
For each ini file
For each line in the ini file
For each printer in the list
if the printer is even replace this line else replace this line
So, you are going thru a list of printers on every line of the INI file. The logic would indicated that every line is going to be ODD because the last printer in the list is ‘ef-4-227’. Here is a cleaned up version of the code with indention, but you still need to check this logic as it will always result in ODD and you’re not doing anything with the printer names in the actual logic, like if you see this printer in the INI, then do X
#http://mow001.blogspot.com/2006/10/powershell-how-can-i-tell-whether.html
Function check-even ($num) {[bool]!($num%2)}
$printers = @('AC-10-170',
'AC-11-171',
'ac-11-173',
'ac-11-175',
'ac-11-176',
'ac-11-180',
'ac-11-181',
'AC-11-182',
'ac-11-183',
'ac-11-185',
'ac-11-186',
'ac-11-187',
'ef-11-218',
'ef-11-219',
'ef-11-222',
'ef-11-223',
'ef-4-207',
'ef-4-208',
'ef-4-211',
'ef-4-212',
'ef-4-213',
'ef-4-215',
'EF-4-220',
'ef-4-224',
'ef-4-225',
'ef-4-226',
'ef-4-227')
foreach ($file in (Get-ChildItem -Path c:\Test\*.ini)) {
$newfilename = $file.fullname -replace ".ini", ".txt"
foreach ($line in (Get-Content $file.fullname)){
foreach($printer in $printers) {
# Get the number off the end of the printer name
$printerNum = $printer.Split("-")[2]
##############################################
##### Logic doesn't make sense here #####
##############################################
if((check-even $printerNum) -eq $true) {
#even
$line.Replace('Host="ACFILE01"','Host="VJJJRT02"').Replace('Host="effile01"','Host="VJJJRT02"')
}
else {
#odd
$line.Replace('Host="ACFILE01"','Host="VSHHPRT01"').Replace('Host="ACFILE01"','Host="VSHHPRT01"')
}
}
}
}