Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 639955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:53:59+00:00 2026-05-13T20:53:59+00:00

Disclaimer: i still suck at writing scripts please be gentle if this is a

  • 0

Disclaimer: i still suck at writing scripts please be gentle if this is a dumb question! 🙂

What i’m doing is pretty simple. I am using a SqlServerCmdletSnapin which gives me the ability to run t-sql queries in powershell 1.0 and return it to either the console or a out-file. I am currently sending it to a .txt file, my code so far is here:

$sqlcheck = Invoke-Sqlcmd -Query "select * from client" -ServerInstance "TESTDB1\TESTSQL1" | out-file -filepath "C:\MyFolder\Client_Result.txt" 

The above returns a line of sql (example):

1 CAAT PI 2003-08-05 13:34:00 PI 2003-08-05 13:34:00

So this sends the results to the .txt file. What im trying to accomplish is from the results that print to the .txt file, if it returns nothing or blank, than that’s a failure and it should send an e-mail.

I already wrote an e-mail function for this but what i’m stumbling on is how i’m actually going to check for empty results or i guess $null results. I’m probably using the completely wrong logic but, im writing something like this:

$sqlcheck = Invoke-Sqlcmd -Query "select * from client" -ServerInstance "TESTDB1\TESTSQL1" | out-file -filepath "C:\MyFolder\Client_Result.txt" 
if($sqlcheck -is [Array]){
if($sqlcheck[1] -match "OK"){
echo "Status is OK" > C:\MyFolder\Result.log
}
}
else{
Send-Email
}

Obviously not working! Someone please slap me with some knowledge. 🙂

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:53:59+00:00Added an answer on May 13, 2026 at 8:53 pm

    There are two problems.

    First, out-file doesn’t output anything. So in $sqlcheck there will be allways $null. You could split it to two statements:

     $sqlcheck = Invoke-Sqlcmd -Query ...
     $sqlcheck | out-file ...
    

    Second, indexes begin with 0, so $sqlcheck[1] is a mistake. It should be $sqlcheck[0].

    You can also skip checking the type and use matching for all the items in $sqlcheck (either simple object or array of objects). Generally it is safer to use the @(<item>) syntax as Keith proposes.

    if ($sqlcheck -and $sqlcheck -match ...) { ... }
    

    For some experiments look at

    if ($null) { 'true' }
    if (@($null)) { 'true' }
    if (,@($null)) { 'true' }
    if (('a','b','c') -match 'c') { 'true' }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: This is not actually a programming question, but I feel the audience on
Disclaimer: This is for a homework assignment, but the question is not regarding the
Disclaimer This is not strictly a programming question, but most programmers soon or later
Full disclaimer: I'm a CS student, and this question is related to a recently
Disclaimer : My apologies for all the text below (for a single simple question),
Background / Disclaimer First of all, please feel free to skip this entirely if
Disclaimer: I'm pretty terrible with multithreading, so it's entirely possible I'm doing something wrong.
Disclaimer This question is a repost. I originally asked it here . While there
Again disclaimer disclaimer still learning C# and OOP generally so I hope you'll be
First of all, a disclaimer: I have experience in other languages, but am still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.