Quantcast
Channel: SQLServerCentral » Programming » Powershell » Latest topics
Viewing all articles
Browse latest Browse all 467

Continue on error in foreach

$
0
0
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?[center]$instanceNameList = invoke-Sqlcmd -query "SELECT [server_name] as Name FROM [msdb].[dbo].[sysmanagement_shared_registered_servers_internal] SSRSIjoin [msdb].[dbo].[sysmanagement_shared_server_groups_internal] SSSGIon SSRSI.server_group_id = SSSGI.server_group_idwhere 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 msdbgoSELECT @@servername, items.subject, items.last_mod_date ,l.description FROM dbo.sysmail_faileditems as itemsINNER JOIN dbo.sysmail_event_log AS l ON items.mailitem_id = l.mailitem_idWHERE items.last_mod_date > DATEADD(hh, -1,getdate())" -ServerInstance $instanceName.Name}$results| Where-Object {$_} | Export-Csv c:\MailFailure.csv -NoTypeInformat[/center]

Viewing all articles
Browse latest Browse all 467

Trending Articles