POWERSHELL – RUN INVOKE-SQLCMD
The Code
1
2
3
4
5
6
7
8
9
10
11
|
try
{
Invoke-Sqlcmd -ServerInstance ServerName -Database DatabaseName -Query 'EXEC sp_StoredProcedureName' |select -ExpandProperty XML_F52E2B61-18A1-11d1-B105-00805F49916B| Out-File -FilePath E:\FilePath\Filename_$(get-date -f yyyyMMdd).xml
(Get-Content E:\FilePath\Filename_$(get-date -f yyyyMMdd).xml -Raw).Replace("`r`n","") | Set-Content E:\FilePath\Filename_$(get-date -f yyyyMMdd).xml -Force
}
catch
{
Add-Content -Path "E:\FilePath\ErrorLog\PSErrorLog.txt" -Value (Write-Output $(Get-Date) $_) -Force
}
|
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…