Hi,
Trying to write a small script that will alert if there are any additional users/groups added to sysadmin role. It's working fine running as a standalone script but always returns 0 when run via Solarwinds. Can anyone point out where I'm going wrong?
Import-Module SQLPS -disablenamechecking
$MySQL = new-object Microsoft.SqlServer.Management.Smo.Server $env:computername;
$SQLLogins = $MySQL.Logins;
$i=[int]0;
$SysAdmins = $null;
$SysAdmins = foreach($SQLUser in $SQLLogins);
{
foreach($role in $SQLUser.ListMembers())
{
if($role -match 'sysadmin' -and $SQLUser.Name -ne 'NT SERVICE\Winmgmt' -and $SQLUser.Name -ne 'NT SERVICE\SQLWriter' -and $SQLUser.Name -ne 'sa' -and !$SQLUser.Name.startswith('NT SERVICE\SQLAgent’) -and !$SQLUser.Name.startswith('NT SERVICE\MSSQL’))
{
$i++;
};
};
};
write-host "MESSAGE: There are $i additional people/groups in the Sysadmin group";
write-host "STATISTIC: $i";
if ($i -gt 0)
{
exit 1;
};
elseif ($i -eq 0)
{
exit 0;
};