Powershell mail every user from SQL query - sql

Unfortunately, I could not find my answer from all the examples I came across.
I have a SQL query that contains the following values
First name
Suffix
Last Name
Email address
Now I want to send a mail per record and in the mail mention the name.
Below is the code that doesn't do the loop right now and puts the name in the mail.
<# Variabelen #>
$PlaceDate = "Amsterdam, " + (Get-Date -f dd-MM-yyyy)
$MailSubject = "My subject"
$sqlinstance = "MSSQLSRV1"
<# SQL data #>
$query = "SELECT [Firstname]
,[Suffix]
,[Lastname]
,[Emailaddress]
,[DateVisit]
FROM [DBname].[dbo].[tbl_EventVisitors]
where CONVERT(DATE, [DateVisit]) = CAST( GETDATE()-1 AS Date )"
$results = Invoke-Sqlcmd -Query $query -ServerInstance $sqlinstance
<# Create mail with SQL fields #>
$CoryReportHtml = ConvertTo-Html -PreContent #"
<body>
<br />
$PlaceDate<br /><br />
Dear $Firstname $Suffix $Lastname,<br />
MyMessage
</body>
"# | Out-String
<# Send the mail #>
$mailParams = #{
SmtpServer = 'localhost'
to = $Emailaddress
from = "from#example.com"
Subject = $MailSubject
Body = $CoryReportHtml
BodyAsHtml = $true
}
Send-MailMessage #mailParams

$results = Invoke-Sqlcmd -Query $query -ServerInstance $sqlinstance
foreach ($row in $results) {
$body_value = $null
$body_value = "Dear $($row.Firstname) $($row.$Lastname)"
$body_value += "my message"
$Emailaddress = $row.Name
$MailSubject = "Subject here"
$mailParams = #{
SmtpServer = 'localhost'
to = $Emailaddress
from = "from#example.com"
Subject = $MailSubject
Body = $body_valu
BodyAsHtml = $true
}
Send-MailMessage #mailParams
Write-Host "email sent to $row.name"
}

Related

How to add data from SQL server to hashtable using PowerShell?

Basically, I want the data to show up in an excel file like it shows in the SQL database.
This is much more simplified version of the work that I need to do but in essence this what it is.
I retrieve the data from SQL and for each item retrieved(which is the primary key) I want the data corresponding to it to be added in the hashtable. I then export this hashtable as a CSV
The CSV file is generated but with some weird data
I am not sure what exactly is wrong because when I Write-host $hashObject I can see the data is in there.
Code
$server = "DESKTOP\SQLEXPRESS"
$database = "AdventureWorks2019"
$hashTable = #{}
$hashObject = #([PSCustomObject]$hashTable)
$query = "SELECT[DepartmentID] FROM [AdventureWorks2019].[HumanResources].[Department]"
$invokeSql = Invoke-Sqlcmd -ServerInstance $server -Database $database -Query $query
$departmentResult = $invokeSql.DepartmentID
ForEach($department in $departmentResult){
$queryAll = "SELECT [Name],[GroupName],[ModifiedDate]FROM [AdventureWorks2019].[HumanResources].[Department] Where DepartmentID=$department"
$invokeSql = Invoke-Sqlcmd -ServerInstance $server -Database $database -Query $queryAll
$name = $invokeSql.Name
$groupName = $invokeSql.GroupName
$modifiedDate = $invokeSql.ModifiedDate
$hashObject+=("Department",$department, "Name",$name,"GroupName",$groupName,"ModifiedDate",$modifiedDate)
}
ConvertTo-Csv $hashObject| Export-Csv -Path "C:\Users\Desktop\PowerShell\HashTable_OutputFiles\HashOutput.csv"
This is a simplified version of what you're attempting to do, in this case you should be able to use the SQL IN Operator in your second query instead of querying your Database on each loop iteration.
As aside, is unclear what you wanted to do when declaring a hash table to then convert it to a PSCustomObject instance and then wrap it in an array:
$hashTable = #{}
$hashObject = #([PSCustomObject] $hashTable)
It's also worth noting that ConvertTo-Csv and Import-Csv are coded in such a way that they are intended to receive objects from the pipeline. This answer might help clarifying the Why. It's also unclear why are you attempting to first convert the objects to Csv and then exporting them when Import-Csv can (and in this case, must) receive the objects, convert them to a Csv string and then export them to a file.
$server = "DESKTOP\SQLEXPRESS"
$database = "AdventureWorks2019"
$query = "SELECT [DepartmentID] FROM [AdventureWorks2019].[HumanResources].[Department]"
$invokeSql = Invoke-Sqlcmd -ServerInstance $server -Database $database -Query $query
$department = "'{0}'" -f ($invokeSql.DepartmentID -join "','")
$query = #"
SELECT [Name],
[GroupName],
[ModifiedDate]
FROM [AdventureWorks2019].[HumanResources].[Department]
WHERE DepartmentID IN ($department);
"#
Invoke-Sqlcmd -ServerInstance $server -Database $database -Query $query |
Export-Csv -Path "C:\Users\Desktop\PowerShell\HashTable_OutputFiles\HashOutput.csv"
If you want to query the database per ID from the first query, you could do it this way (note this is similar to what you where looking to accomplish, merge the ID with the second results from the second query):
$invokeSql = Invoke-Sqlcmd -ServerInstance $server -Database $database -Query $query
$query = #"
SELECT [Name],
[GroupName],
[ModifiedDate]
FROM [AdventureWorks2019].[HumanResources].[Department]
WHERE DepartmentID = '{0}';
"#
& {
foreach($id in $invokeSql.DepartmentID) {
$queryID = $query -f $id
Invoke-Sqlcmd -ServerInstance $server -Database $database -Query $queryID |
Select-Object #{ N='DepartmentID'; E={ $id }}, *
}
} | Export-Csv -Path "C:\Users\Desktop\PowerShell\HashTable_OutputFiles\HashOutput.csv"

Powershell outputs mixed between loops - SQL datasets [duplicate]

This question already has an answer here:
PowerShell output is crossing between functions
(1 answer)
Closed 2 years ago.
I have a PowerShell script that loops through list of 3 servers. A SQL script is run with Invoke-Sqlcmd and the result set is stored to variable $DS. At the end of the loop I return the records with with $DS.Tables.Rows.
But the results sets are getting mixed together. I tried using a Write-Host message to breakup the results. But they are still getting mixed together.
Why are the result getting mixed together in the output?
How can I separate the outputs between each loop?
Thanks
Object type
$DS | gm ............... TypeName: System.Data.DataSet
$DS.Tables | gm ........ TypeName: System.Data.DataTable
$DS.Tables.Rows | gm ... TypeName: System.Data.DataRow
Script
#########################>
# SQL servers
$PCList= #("GCOD139","GCOD039","GCOP039")
Write-Host ($PCList -join ", ")
# Query multiple servers
foreach ($PC in $PCList) {
Write-Host ($PC + "...") -ForegroundColor Yellow
# SQL parameters
$Params = #{
'ServerInstance' = $PC;
'Database' = 'master';
# 'Username' = 'svcBIPOC';
# 'Password' = 'bipoc2020*';
# 'InputFile' = "C:\ScriptFolder\TestSqlCmd.sql"
'Query' = '
SELECT
[Server]= ##SERVERNAME
--MB to GB
, REPLACE(name, ''MB'', ''GB'')
,[value]= CAST(value as int)/1000
, [value_in_use]= CAST(value_in_use as int)/1000
--, value, value_in_use, [description]
FROM sys.configurations
WHERE name like ''%server memory%''
ORDER BY name desc
OPTION (RECOMPILE);
'
}
# Capture SQL Dataset
# (Get-Date).ToSTring('s') + " SQL query start..."
$DS = Invoke-Sqlcmd #Params -As DataSet
#(Get-Date).ToSTring('s') + " SQL query end..."
Write-host "-----"
Write-host "SQL"
sleep -Seconds 5
$DS.Tables.Rows
sleep -Seconds 5
}
#########################
Stop using Write-Host to convey progress information - use Write-Progress for that instead!
$PCList= #("GCOD139","GCOD039","GCOP039")
Write-Progress -Activity "Query servers" -Status "About to query: $($PCList -join ", ")"
# Query multiple servers
foreach ($PC in $PCList) {
Write-Progress -Activity "Query servers" -Status "Querying: $PC"
# SQL parameters
$Params = #{
'ServerInstance' = $PC;
'Database' = 'master';
# 'Username' = 'svcBIPOC';
# 'Password' = 'bipoc2020*';
# 'InputFile' = "C:\ScriptFolder\TestSqlCmd.sql"
'Query' = '
SELECT
[Server]= ##SERVERNAME
--MB to GB
, REPLACE(name, ''MB'', ''GB'')
,[value]= CAST(value as int)/1000
, [value_in_use]= CAST(value_in_use as int)/1000
--, value, value_in_use, [description]
FROM sys.configurations
WHERE name like ''%server memory%''
ORDER BY name desc
OPTION (RECOMPILE);
'
}
# Capture SQL Dataset
$DS = Invoke-Sqlcmd #Params -As DataSet
$DS.Tables.Rows
}
Write-Progress -Activity "Query servers" -Completed
Now the progress messages won't interfere with the actual output from the function

Powershell script to get output through Sql query in table format attached in email?

The below script is resulting in the error below when attempt to send mail is made.
New-Object : A positional parameter cannot be found that accepts argument '='.
At line:22 char:18
+ ... onnection = New-Object System.Data.SqlClient.SqlConnection $SqlCon ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Object], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
Exception calling "Fill" with "1" argument(s): "Login failed for user ''."
At line:29 char:1
+ $SqlAdapter.Fill($DataSet)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException
Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
At line:44 char:17
+ -BodyAsHtml $html_table `
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage
$Servers = (Import-Csv -Path "D:\Scripts\input.csv").ComputerName
$SQLDBName = "ReportServer"
$SQLQuery = #"
SELECT Distinct
RL.RoleName,
USR.UserName
FROM
Catalog C
INNER JOIN Policies PL
ON C.PolicyID = PL.PolicyID
INNER JOIN PolicyUserRole PUR
ON PUR.PolicyID = PL.PolicyID
INNER JOIN Users USR
ON PUR.UserID = USR.UserID
INNER JOIN dbo.Roles RL
ON RL.RoleID = PUR.RoleID
WHERE RoleName = 'Content Manager'
ORDER BY USR.UserName
"#
# This code connects to the SQL server and retrieves the data
$SQLConnection = New-Object System.Data.SqlClient.SqlConnection $SqlConnection.ConnectionString = "Server = $Servers; Database = $SQLDBName;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
# This code outputs the retrieved data
$html = $DataSet.Tables[0] | ConvertTo-Html -fragment
$results = $DataSet.Tables | format-table -autosize | out-string
$mail_body = $results
# Send the email
$html_table = $dt | sort-object "Status" | ConvertTo-Html -Fragment
Send-MailMessage `
-From "Reporting.Services#accenture.com" `
-To 'aditi.m.singh#accenture.com' `
-Subject 'Sending the Attachment' `
-BodyAsHtml $html_table `
-SmtpServer 'AMRINT.SMTP.ACCENTURE.COM'
This should work for you. One issue you had is that the variable $dt was never initialized in your script.
param(
$emailFrom = 'Reporting.Services#accenture.com',
$emailTo = 'aditi.m.singh#accenture.com',
$emailSubject = 'Sending the Attachment',
$smtp = 'AMRINT.SMTP.ACCENTURE.COM',
$Server = "$Env:ComputerName\MSSQLSERVER01",
$SQLDBName = 'Master',
$SQLQuery = #"
SELECT Distinct
RL.RoleName,
USR.UserName
FROM
Catalog C
INNER JOIN Policies PL
ON C.PolicyID = PL.PolicyID
INNER JOIN PolicyUserRole PUR
ON PUR.PolicyID = PL.PolicyID
INNER JOIN Users USR
ON PUR.UserID = USR.UserID
INNER JOIN dbo.Roles RL
ON RL.RoleID = PUR.RoleID
WHERE RoleName = 'Content Manager'
ORDER BY USR.UserName
"#
)
# This code connects to the SQL server and retrieves the data
$SQLConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $Server; Database = $SQLDBName; Integrated Security=true;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$data = $DataSet.Tables[0]
$html = $data `
| Select-Object -Property RoleName, UserName `
| ConvertTo-Html -fragment `
| Out-String
Send-MailMessage `
-From $emailFrom `
-To $emailTo `
-Subject $emailSubject `
-BodyAsHtml $html `
-SmtpServer $smtp

SQL Server backup status Report with PowerShell

I got a PowerShell script for reporting SQL backup status on multiple servers, it works fine but it had no function to send mail. I added that part and now I am able to get the mail with the attachment.
The only concern is, I want the report to show "NA" and not a default date where the satabase is in Simple Recovery Model or if backup has not happened. Can someone please advise?
Here is the code, just in case someone needs it unlike my requirement:
$ServerList = Get-Content "Serverlist location"
$OutputFile = "to save the report location"
$titleDate = Get-Date -UFormat "%m-%d-%Y - %A"
$HTML = '<style type="text/css">
#Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
#Header td, #Header th {font-size:14px;border:1px solid #98bf21;padding:3px 7px 2px 7px;}
#Header th {font-size:14px;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#A7C942;color:#fff;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>'
$HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 width=100% id=Header>
<TR>
<TH><B>Database Name</B></TH>
<TH><B>RecoveryModel</B></TD>
<TH><B>Last Full Backup Date</B></TH>
<TH><B>Last Differential Backup Date</B></TH>
<TH><B>Last Log Backup Date</B></TH>
</TR>"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
foreach ($ServerName in $ServerList)
{
$HTML += "<TR bgColor='#ccff66'><TD colspan=5 align=center><B>$ServerName</B></TD></TR>"
$SQLServer = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $ServerName
foreach ($Database in $SQLServer.Databases)
{
$HTML += "<TR>
<TD>$($Database.Name)</TD>
<TD>$($Database.RecoveryModel)</TD>
<TD>$($Database.LastBackupDate)</TD>
<TD>$($Database.LastDifferentialBackupDate)</TD>
<TD>$($Database.LastLogBackupDate)</TD>
</TR>"
}
}
$HTML += "</Table></BODY></HTML>"
$HTML | Out-File $OutputFile
$emailFrom = "send email address"
$emailTo = "recipient email address"
$subject = "Xyz Report"
$body = "your words "
$smtpServer = "Smptp server"
$filePath = "location of the file you want to attach"
function sendEmail([string]$emailFrom, [string]$emailTo, [string]$subject,[string]$body,[string]$smtpServer,[string]$filePath)
{
$email = New-Object System.Net.Mail.MailMessage
$email.From = $emailFrom
$email.To.Add($emailTo)
$email.Subject = $subject
$email.Body = $body
$emailAttach = New-Object System.Net.Mail.Attachment $filePath
$email.Attachments.Add($emailAttach)
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($email)
}
sendEmail $emailFrom $emailTo $subject $body $smtpServer $filePath
Replace
<TD>$($Database.LastBackupDate)</TD>
with something like
<TD>$(if ($Database.RecoveryModel -eq 'Simple' -or $Database.LastBackupDate -eq '01/01/0001 00:00:00') {'NA'} else {$Database.LastBackupDate})</TD>
Do the same for LastDifferentialBackupDate and LastLogBackupDate.
With that said, I strongly recommend looking into calculated properties, ConvertTo-Html, and Send-MailMessage, which would allow you to greatly simplify your code:
[Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
$emailFrom = 'sender#example.com'
$emailTo = 'recipient#example.com'
$subject = 'Xyz Report'
$smtpServer = 'mail.example.com'
$style = #'
<style type="text/css">
...
</style>
'#
$msg = Get-Content 'C:\path\to\serverlist.txt' |
ForEach-Object {New-Object 'Microsoft.SqlServer.Management.Smo.Server' $_} |
Select-Object -Expand Databases |
Select-Object Name, RecoveryModel,
#{n='LastBackupDate';e={if ($_.RecoveryModel -eq 'Simple' -or $_.LastBackupDate -eq '01/01/0001 00:00:00') {'NA'} else {$_.LastBackupDate}}},
#{n='LastDifferentialBackupDate';e={if ($_.RecoveryModel -eq 'Simple' -or $_.LastDifferentialBackupDate -eq '01/01/0001 00:00:00') {'NA'} else {$_.LastDifferentialBackupDate}}},
#{n='LastLogBackupDate';e={if ($_.RecoveryModel -eq 'Simple' -or $_.LastLogBackupDate -eq '01/01/0001 00:00:00') {'NA'} else {$_.LastLogBackupDate}}} |
ConvertTo-Html -Head $style | Out-String
Send-MailMessage -From $emailFrom -To $emailTo -Subject $subject -Body $msg -BodyAsHtml -SmtpServer $smtpServer
See also.

update sql table for Active Directory createdon and disabled on information

I have a user table in the database that i am trying to update with Createdon date and disabled on date with the data from Active Directory. So far this is what I have:
$SearchRoot = "OU=NonAIQ,OU=FrontOffice,DC=dev,DC=local"
$serverName = "localhost"
#$SearchRoot = "OU=NonAIQ,OU=FrontOffice,DC=dmz,DC=local"
#$serverName = "spoproddb3.dmz.local"
try {
Import-Module "sqlps" -DisableNameChecking
if ((Get-PSSnapin -Name "Quest.ActiveRoles.ADManagement" -ErrorAction SilentlyContinue) -eq $null ) {
Add-PsSnapin "Quest.ActiveRoles.ADManagement"
}
$externalUsers = Get-QADUser -SizeLimit 0 -SearchRoot $SearchRoot | Select-Object whencreated, whenchanged
$externalUsers | % {
$query = #"
Update tbl_EdgeUsers Set CompanyName = '$_.CreationDate'
Where UserUPN = $_.UserPrincipalName;
"#
Write-Host "The query is $query"
Invoke-SqlCmd -ServerInstance $serverName -Query $query -Database "EdgeDW"
}
} finally {
Remove-Module "sqlps" -ErrorAction SilentlyContinue
Remove-PsSnapin "Quest.ActiveRoles.ADManagement"
}
Now for when created, we just grab all the values.
But since AD does not track the Disabled in date, I am using the when changed date since we dont make changes to an account once it is changed.
The part that I am stuck on is about the logic for when changed date. For this I have to check if an account is disabled. If it is the update the table with that date. If an account is not disabled, then ignore that value and set the value in the sql table as '1/1/9999'.
can you guys please help with this logic?
Thank you in advance for any help.
of top of my head maybe something such as this, although thinking about it now, its a nasty way having the invoke-sql inside the foreach loop if the dataset is large, probably better to output the results of the if statement to csv or somewhere then run the invoke-sql against that.
$users = Get-ADUser -Filter * -Properties whenchanged | Select-Object -Property UserPrincipalName, whenchanged, enabled
$disabledsql = #"
update tbl_EdgeUsers Set date = '$user.whenchanged'
Where UserUPN = '$user.UserPrincipalName';
"#
$activesql = #"
update tbl_EdgeUsers Set date = '1/1/9999
Where UserUPN = '$user.UserPrincipalName';
"#
foreach ($user in $users)
{
if ($user.enabled -eq 'False')
{
Invoke-Sqlcmd -ServerInstance $serverName -Query $disabledsql -Database 'EdgeDW'
}
else
{
Invoke-Sqlcmd -ServerInstance $serverName -Query $activesql -Database 'EdgeDW'
}
}