SQL SERVER DBA, Linux and Azure

Get CPU utilization for a host using PowerShell Script

 The code snippet below shows how to get CPU utilization for a host.

Get-WmiObject -Class win32_Processor -ComputerName <ip address> | select DeviceID,Name,Status,LoadPercentage | Sort-Object LoadPercentage -Descending

Note: You have to update ip address or hostname to get output.

Send email with attachments using PowerShell script

Being able to send emails through script is really useful, particularly for system administrators who often have to administer their servers remotely. It also comes in handy for various other purposes. The code below demonstrates how to send email with attachments using PowerShell.

$smtpServer = "<your smtp mail server address>"
$msg = new-object Net.Mail.MailMessage
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
 
$msg.From = "from@xyz.com"
$msg.To.Add("to@xyz.com")
$msg.Subject = "Your email subject line"
$msg.Body = "Your email body"
 
# Attachments
$file1 = ".\f1.txt"
$file2 = ".\f2.txt"
 
foreach ($file in ($file1, $file2)){
    $att = New-Object Net.Mail.Attachment($file)
    $msg.Attachments.Add($att)
}
 
$smtp.Send($msg)

Get last reboot time for a host using powershell script

Here is how we can get the last reboot time for a host using powershell.

Get-WmiObject Win32_OperatingSystem -ComputerName <ip address> | foreach ($_) { [System.DateTime]::ParseExact($_.LastBootupTime.split('.')[0],'yyyyMMddHHmmss',$null) }


Note: You have to replace ip address or hostname to get output.

Delete files older than n days using Powershell script

 Here is a code to delete files older than N days where N refers number of days. In the code snippet below we are deleting files older than seven days.

$LastWrite = (Get-Date).AddDays(-7) 
 
Get-ChildItem "C:\temp" -Recurse | Where {$_.LastWriteTime -le $LastWrite} | foreach ($_) { Remove-Item $_.fullname}

How to schedule and automate backups of SQL Server databases in SQL Server Express

Anyone using SQL Server Express editions would have noticed that there is no way to schedule either jobs or maintenance plans because the SQL Server Agent is by default not included in these editions.

But what if those using Express editions would like to schedule and automate backups. Is there a way?

Yes, please read this article by Microsoft