September 19, 2017 at 9:08 pm
I have a powershell script that will execute a query on a database that has Replication.
$QueryPath = "C:\Replication\RepAsPub.sql"
$OutputFile = "C:\Replication\RepAsPub_Result.csv"
$ExecuteQuery = Get-Content -Path $QueryPath -Raw
Get-Content -Path "C:\Replication\Instance_List.txt" | ForEach-Object {
Invoke-SqlCmd -ServerInstance $_ -Query $ExecuteQuery -Queryimeout 60000
} | Export-Csv -NoTypeInformation -Path $OutputFile
SSMS output. Results and Messages:
But on the outputs on the csv file it is only showing the Results:
Need help that it will also have the Messages shown on a third column. (It should also catch in case there are errors)
Expected output should be, see screenshot below:
September 23, 2017 at 3:42 pm
If you want the row count can't you just add "SET NOCOUNT OFF" in your query argument that is passed?
Joie Andrew
"Since 1982"
September 24, 2017 at 5:38 am
SQLCMD does return the number of rows. At least Version 13 on Linux does:[username]@ihy:~$ sqlcmd -S localhost -U [Username] -Q "SELECT @@ServerName AS ServerName;"
Password:
ServerName
--------------------------------------------------------------------------------------------------------------------------------
ihy
(1 rows affected)
I did also give it a quick go on my Windows laptop and got the same (I think that has Version 13 as well, if not 12).
Could you be a bit more specific?
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
September 25, 2017 at 1:51 am
yhandz_21 - Tuesday, September 19, 2017 9:08 PMIs there a powershell script that will show both output Results and Messages.For example:
Select @@servernameIt should show on the powershell output
Result - Server/InstanceName
Messages- (1 row(s) affected
Basic answer: No.
Bit more detailed: The "message" tab you get in SSMS for row counts and print messages is a separate stream that is not as easily accessible in PowerShell. It can be accessed but not with one or two simply commands. You can see how you can do this in this post: here.
In order to get the example output you have provided you would have to generate that yourself using `Write-Output`, passing in the desired output after you captured it.
Shawn Melton
Twitter: @wsmelton
Blog: wsmelton.github.com
Github: wsmelton
September 25, 2017 at 8:59 pm
Hi,
See update above for expected output.
September 26, 2017 at 8:22 pm
yhandz_21 - Monday, September 25, 2017 8:59 PMHi,See update above for expected output.
In PowerShell and .NET world that third column of data you are seeking does not exist. SSMS is generating that information for you.
Shawn Melton
Twitter: @wsmelton
Blog: wsmelton.github.com
Github: wsmelton
September 27, 2017 at 12:26 am
Shawn Melton - Monday, September 25, 2017 1:51 AMyhandz_21 - Tuesday, September 19, 2017 9:08 PMIs there a powershell script that will show both output Results and Messages.For example:
Select @@servernameIt should show on the powershell output
Result - Server/InstanceName
Messages- (1 row(s) affectedBasic answer: No.
Bit more detailed: The "message" tab you get in SSMS for row counts and print messages is a separate stream that is not as easily accessible in PowerShell. It can be accessed but not with one or two simply commands. You can see how you can do this in this post: here.
In order to get the example output you have provided you would have to generate that yourself using `Write-Output`, passing in the desired output after you captured it.
can you help me to add that write output. I tried following the blog but isn't successful. or you can help adding error handling with the code
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply