I want to verify the database for the given script in CI. I want to check specific string exists or not in the given file.
Below is the content of my file
USE [SampleDB]
I am reading the content of my file and checking [SampleDB] string exists or not in the file.
I have tried the below things.
If(Get-Content -path D:/Document/admin.sql | Select-String -Pattern "[SampleDB]")
{
# Right Database exist
} else {
# Right Database does not exist
}
How can I verify the right database name after the USE string?
CodePudding user response:
Select-String
uses regular expressions to perform string searches, and the construct [...]
has a special meaning in that context - namely, it means "match any one of these characters", not quite what you're expecting.
To search for the word [SampleDB]
verbatim you either have to escape it correctly:
# escape your search term!
$searchTerm = [regex]::Escape('[SampleDB]')
if(Get-Content -path D:/Document/admin.sql | Select-String -Pattern $searchTerm) {
# Right Database exist
} else {
# Right Database does not exist
}
... or you can request Select-String
perform a non-regex match with the -SimpleMatch
switch:
if(Get-Content -path D:/Document/admin.sql | Select-String -Pattern "[SampleDB]" -SimpleMatch) {
# Right Database exist
} else {
# Right Database does not exist
}