I’m fairly new to using powershell so bear with me. My goal is to query a database over our network and save the file output of the query as a csv on the local machine. The query works as intended but will occasionally output a blank csv file with no data in it. I’ll post my script below. Any insight would be greatly appreciated. Thanks!
Invoke-sqlcmd -ServerInstance $Server -Database $Database -Username $username -Password $password -Query "SELECT * FROM $Table WHERE EventTimeStamp >= DATEADD(HH, -2, GETDATE());" | export-csv -path "$Path_$(Get-Date -Format yyyyMMddHHmmss).csv"
I’ve tried the above script and it works as expected but sometimes outputs a blank csv file.
Daniel Blanton is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.