POWERSHELL – RUN INVOKE-SQLCMD

The Code

The Analysis

This code is written in PowerShell to execute a stored procedure on a Microsoft SQL Server and save the results to an XML file.

The first line of code invokes the Invoke-Sqlcmd cmdlet, which allows you to run T-SQL statements, scripts, or stored procedures on a SQL Server instance. The cmdlet takes several parameters, including the name of the server instance (-ServerInstance), the name of the database (-Database), and the T-SQL query (-Query) to be executed. In this case, the query is calling a stored procedure named “sp_StoredProcedureName”.

The results of the Invoke-Sqlcmd cmdlet are then piped (|) to the Select-Object cmdlet, which is using the -ExpandProperty parameter to expand the value of the XML_F52E2B61-18A1-11d1-B105-00805F49916B property. This property contains the results of the stored procedure in XML format.

The expanded XML results are then piped to the Out-File cmdlet, which is used to save the results to a file. The -FilePath parameter specifies the file path and name for the file, and the filename includes the current date in the format “yyyyMMdd”.

The second block of code reads the contents of the XML file into memory using the Get-Content cmdlet, and then uses the .Replace() method to replace all instances of “rn” with an empty string. This effectively removes all newline characters from the file. The modified contents are then saved back to the file using the Set-Content cmdlet and the -Force parameter, which overwrites the existing file.

The entire script is wrapped in a try-catch block, which allows the script to handle any errors that may occur during execution. If an error occurs, it is logged to a file using the Add-Content cmdlet and the Write-Output cmdlet, which writes the current date and the error message to the specified file.

Overall, this code is designed to execute a stored procedure on a SQL Server instance, save the results to an XML file, remove all newline characters from the file, and handle any errors that may occur during execution.

6 Comments

  1. I really like your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you? Plz reply as I’m looking to design my own blog and would like to know where u got this from. thank you

  2. Randy,

    My name is Scott Niblock and I am the 911 System Administrator at Iredell County Emergency Communications in North Carolina. I was provided a link to your site from a friend in Public Safety and was wondering if you would have time to discuss using this for our Frontline API?

    • Hi Scott!

      Thanks for reaching out. I just sent you an email. Please let me know if you are still interested and if you have any specific questions regarding implementation!

  3. Heya i am for the first time here. I came across this board and I find It really useful & it helped me out a lot. I’m hoping to give something again and aid others like you aided me.

  4. Excellent blog here! Also your site lots up very fast! What web host are you using? Can I get your affiliate link for your host? I want my web site loaded up as fast as yours lol

Leave a Reply

Your email address will not be published. Required fields are marked *