Quantcast
Channel: THWACK: Popular Discussions - Script Lab
Viewing all articles
Browse latest Browse all 9937

Query sysadmin Role on SQL

$
0
0

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;

};


Viewing all articles
Browse latest Browse all 9937

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>