VSSADMIN LIST WRITERS (restart and email)

Hi All,

Hoping you can advise on the following. Is there a way to add an email function to the below script in order to email me , if the output of the script shows that one of the vss writes is down and it was restarted?

Thank you all for the replies and apologies for the poor formatting.

If the following command is run in powershell

vssadmin list writers

and the output reads

Writer name: ‘SqlServerWriter’
Writer Id: {a65faa63-5ea8-4ebc-9dbd-a0c4db26912a}
Writer Instance Id: {d4c7386a-6860-4ec0-8c82-eceeff80b6d8}
State: [1] Failed
Last error: Retryable error

Notice the Failed State?

Is it possible for powershell to run the following command if the State equals Failed?

net stop SQLWriter

net start SQLwriter

and then email when the service was started


Kind regards



Please format the script in a readable way and repost

I tried to make heads or tails of this script but like Sam asked pls try again with proper formating

it stops working after [pre] ForEach-Object {$_.Line.tostring().Split("’")[1]} |[/pre] for me

Agree on the formatting thing here, but there are lots of things wrong with this script, well one-liner.

PowerShell provides natural line breaks, pipe, semicolon, math symbols, comparison operators, backtick, etc., or use splatting. These help makes folks reading your stuff, more willing to deal with it vs having to rewrite/reformat what you provided.

The $servicearray line all by itself, is not valid syntax. If you’d put this in the ISE/VSCord, you will immediately see all the error indicators. Just copying and pasting in VSCode, immediately report 21 errors.

1 - it’s to long, making it hard to read and troubleshoot, and maintenance is going to be a nightmare.
Simple rule, with the exception of some cases, if your code line won’t fit in the width of a 8.1x 11 sheet of paper (normal human reading thing), it’s too long, potentially overly complicated and well you know.

2 - You cannot at a function to this script using the style are using to make sense.

Turn this one liner in to a proper function(s) that add the addition function using the Send-MailMessage cmdlet.