Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

iterating through an array to gather data for an email to a customer

$
0
0

Ok to start I'm extremely new to powershell. I've taken one class and now my boss thinks I'm proficient. What I am trying to accomplish :

dump sql database into an array

sort array by owner

iterate through owners capturing all server names, descriptions, support contacts into a csv for each unique owner

look up email address for said owner

send email to the owner with the CSV and text on how to get the information updated

then continue on to the next owner until I reach the end of the database

I have figured out how to get the array and sort it, and I'm almost sure I can get it to send the email, but I can't figure out how to have Powershell do the middle part or look up the email address based on the server owner. Here is the code I have so far (I have only tested getting the database and sorting it so I don't even know if I got the email part right):

$ServerBook = New-Object System.Data.DataSet "myDataSet"
 $sqlConn = New-Object System.Data.SqlClient.SqlConnection("Data Source=<sql server>;Initial Catalog=ServerBook;Integrated Security=True")
 $adapter = New-Object System.Data.SqlClient.SqlDataAdapter("SELECT ServerName,Status,Departament,Owner,SupportContact,SupportExternal,ServerDescription FROM v_ServerBook_BasicInfo", $sqlConn)
 $adapter.Fill($ServerBook) 

param(
$ServerBook = $ServerBook.Tables[0] | Sort-Object -Property Owner,

$newfile,
$Alex,
$OwnerName,
$defaultName = Alexander Schluens,
$EmailAddress

)

#this is where I get stuck on how to pull the information I need and look up the email address#

$ServerBook | foreach { $_ }
{
If (-property Owner -eq "")
$message = @"
                                
Please check the attached spreadsheet. You will find a list of all the servers with the owner field blank. If you could please let me know if there are any corrections that need to be made we would greatly appreciate it. If there are servers that you no longer require I can put in the change request to have it decommissioned. If there are no changes to be made please still send me an email letting me know. Thank you in advance for your cooperation. Please let me know if you have any questions.
 
Thank You,

<signature block>

"@       
 
$emailTo = "email address"
$emailFrom = "my_email_address"
$subject="Bi-yearly server review"
$attachment = $newfile
$smtpserver="smtp server"
$smtp=new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $message, $attachment)
}
else (-property Owner -eq "*")
{
   $message = @"
                                
Please check the attached spreadsheet. You will find a list of all your current servers, what they are for, and who is listed as the owner and support contact. If you could please let me know if there are any corrections that need to be made we would greatly appreciate it. If there are servers that you no longer require I can put in the change request to have it decommissioned. If there are no changes to be made please still send me an email letting me know. Thank you in advance for your cooperation. Please let me know if you have any questions.
 
Thank You,

<signature block>

"@       

$emailTo = "email address"
$emailFrom = "my_email_address"
$subject="Bi-yearly server review"
$attachment = $newfile
$smtpserver="smtp server"
$smtp=new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $message, $attachment)
}


Viewing all articles
Browse latest Browse all 6937

Trending Articles