1

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?

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.