'How to find a string/word in a string using Powershell?
I know this sounds like a simple question but nothing I have tried works.
I have to compare data from SQL database to a text file. I have to check if the PartNumber(PartNo), Revision(Rev), and Version(Ver) exist in the first line of the text file. The issue is that for some PartNo the script gives incorrect results. I can see the PartNo is the same but it shows that it is not a match. I have no issues matching the Rev and Ver.
There are two PartNo in particular that don't match(when they should) and they are C29254 and 1-1-25B17. There are other PartNo that are in alphanumeric form but they match. For example, D73874 matches. I don't know what is about these two parts that is not working out.
I have checked the data type of the values that I get from the database and it is a string. The first line from my text file is also a string.
This is what I have tried so far:
If($EiaContent -Contains "$partNo")) None of the parts match(when some should)If($EiaContent -Like $partNo) None of the parts match(when some should)If($EiaContent -Like "*$partNo*") C29254, 1-1-25B17 Do not match(when they should)If($EiaContent -match $partNo) C29254, 1-1-25B17 Do not match(when they should)If($EiaContent -match "$partNo") C29254, 1-1-25B17 Do not match(when they should)If($EiaContent | Select-String -Pattern $partNo -SimpleMatch) C29254, 1-1-25B17 Do not match(when they should)If($EiaContent | Select-String -Pattern $partNo) C29254, 1-1-25B17 Do not match(when they should)If($EiaContent | Select-Object $partNo ) All parts match(when some shouldn't)
The code below is part of a long block that does other stuff so I am just posting the part related to the issue.
Any help is appreciated! Thanks in advance.
Code:
#Get the content (first line)of each .EIA file in the folder.
$EiaContent=(Get-Content $aidLibPathFolder\$folderName\$fileName -TotalCount 1)
Write-host $EiaContent
#Sql query to get the Part Number
$partNoQuery = "SELECT [PartNo] FROM [NML_Sidney].[dbo].[vMADL_EngParts] Where MAFN=$firstPartTrimmed"
$partNoSql = Invoke-Sqlcmd -ServerInstance $server -Database $database -Query $partNoQuery
$partNo = $partNoSql.PartNo.ToString()
$partNo.GetType().Name
write-host $partNo -ForegroundColor Magenta
##Find PartNo##
If($EiaContent | Select-Object $partNo ){
Write-Host "Part Matches"
}
Else{
Write-Host "Part Does Not Match"
}
#Sql query to get the Revs
$revQuery = "SELECT [MA_Rev] As Rev FROM [NML_Sidney].[dbo].[MADL_EngParts] Where MAFN=$firstPartTrimmed"
$revSql = Invoke-Sqlcmd -ServerInstance $server -Database $database -Query $revQuery
$rev = "REV" + " "+ $revSql.Rev
$rev.GetType().Name
write-host $rev -ForegroundColor Red
If($EiaContent -like "*$rev*"){
Write-Host "Rev Matches"
}
Else{
Write-Host "Rev Does not Match"
}
Write-Host $version -BackgroundColor Black
If($EiaContent -like "*$version*"){
Write-Host "Version Matches"
}
Else{
Write-Host "Version Does Not Match"
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
