Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Continue on error in foreach


Continue on error in foreach

Author
Message
Jeff Sims-413169
Jeff Sims-413169
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 828
I have PS that select all instance setup in CMS and then using that list does a foreach check for errors in the database mail log. IF all instance connect it runs great but if it cant connect to one instance it fails. I tried to add a try and catch but maybe didnt know where or who and also tried setting $ErrorActionPreference.

Anyone out there able to help me out?


$instanceNameList = invoke-Sqlcmd -query "
SELECT [server_name] as Name FROM [msdb].[dbo].[sysmanagement_shared_registered_servers_internal] SSRSI
join [msdb].[dbo].[sysmanagement_shared_server_groups_internal] SSSGI
on SSRSI.server_group_id = SSSGI.server_group_id
where SSSGI.server_group_id = '15' or SSSGI.server_group_id = '14' or SSSGI.server_group_id = '13' or SSSGI.server_group_id = '12'
" -serverinstance "consoleserver"
$results = @()

foreach($instanceName in $instanceNameList)
{$results += Invoke-Sqlcmd -Query "
use msdb
go
SELECT @@servername, items.subject,
items.last_mod_date
,l.description FROM dbo.sysmail_faileditems as items
INNER JOIN dbo.sysmail_event_log AS l
ON items.mailitem_id = l.mailitem_id
WHERE items.last_mod_date > DATEADD(hh, -1,getdate())

" -ServerInstance $instanceName.Name}
$results| Where-Object {$_} | Export-Csv c:\MailFailure.csv -NoTypeInformat


Gary Varga
Gary Varga
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8291 Visits: 6136
Have you tried:


$instanceNameList = invoke-Sqlcmd
-query
"SELECT [server_name] as Name
FROM [msdb].[dbo]
.[sysmanagement_shared_registered_servers_internal] SSRSI
join [msdb].[dbo].[sysmanagement_shared_server_groups_internal] SSSGI
on SSRSI.server_group_id = SSSGI.server_group_id
where SSSGI.server_group_id = '15' or SSSGI.server_group_id = '14'
or SSSGI.server_group_id = '13' or SSSGI.server_group_id = '12'"
-serverinstance "consoleserver"
$results = @()

foreach($instanceName in $instanceNameList)
{
try
{
$results += Invoke-Sqlcmd -Query
"use msdb;
go;
SELECT @@servername, items.subject, items.last_mod_date,l.description
FROM dbo.sysmail_faileditems as items
INNER JOIN dbo.sysmail_event_log AS l
ON items.mailitem_id = l.mailitem_id
WHERE items.last_mod_date > DATEADD(hh, -1,getdate());"
-ServerInstance $instanceName.Name
}
catch
{
Write-Error $_
}
}
$results | Where-Object {$_} | Export-Csv c:\MailFailure.csv -NoTypeInformat


Edit: Code formatting.

Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search