Server |
Database |
Users |
| CSDSBAJ1\U12DM | Workbench Database | $ISSAdmin-RL |
| $ITIMSSQLDBA-FC-GS | ||
| svcSQLSupportAgent | ||
| svcSQLSupportSrvr | ||
| sp47284 |
Hello, I have a CSV file like above, I wrote a script to access the file and email managers from active directory and database owner from sql server.
+++++++++++++++++++++++++++++++
##Required for AD Support
Import-Module activedirectory
# List email recipients who should receive email
$InfraDba = "InfraDatabaseSupport@organization.com"
#Application & file paths
$ApplicationPath = "C:\script\databaseUsers\"
$RemediationCSV = $ApplicationPath + "SQL2000_Remediation.csv"
$ManagerCSV = $ApplicationPath + "ManagersResult.csv"
$UserCSV = $ApplicationPath + "UsersResult.csv"
### Deletes previous file
Remove-Item $UserCSV
import-csv $RemediationCSV | foreach {
$ServerName = $_.Server
$DatabaseName = $_.Database
$UserCSV = $_.Users
if (($_.Users -like "us.*") -OR ($_.Users -like "ew.*") -OR ($_.Users -like "os.*") -OR ($_.Users -like "sp.*")) {
### look in the LDAP for user's manager
$user = get-aduser $_.Usesrs -properties manager
### Create the mail message and add the $Results.csv text file as an attachment###
Send-MailMessage –From $InraDba –To $ManagerCSV –Subject "Please confirm user access to Database" $DatabaseName –Body "Below is the list of users who have access to the database $. As the manager, please confirm that users still need access to the datbase, else access will be revoked in 7 days." -Attachment $ResultsCSV –SmtpServer mltpbmxv30.corp.organization.org
else
### This is a service account, look in the database inventory for the owner of the database.
$owner = Invoke-Sqlcmd -Query "SELECT Techincal_owner from HumanResources.Department where Database_name = '$($DatabaseName)'" –Database “AdventureWorks2012” ###$owner is a full name e.g. Terry Owens.
$OwnerCSV=$($owner)@organization.com ###Owner from database is Terry Owens, replace middle space with "." For example Terry.Owens@orgamization.com
### Create the mail message and add the $Results.csv text file as an attachment###
Send-MailMessage –From $InraDba –To $OwnerCSV –Subject "Confirm user access to Database" –Body "Below is the list of service account that have access to the database $. As the owner, please confirm that these service account still need access to the datbase, else access will be revoked in 7 days." -Attachment $ResultsCSV –SmtpServer mltpbmxv30.corp.organization.org
}
}
This script is throwing a lot of errors.
Please help to debug.