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

Show Database ID in SQL Server

 The query below can retrieve database id by supplying the database name

-- Show database id using system view
 SELECT database_id FROM sys.databases
 WHERE name = '<db_name>';

-- Show database id using function
SELECT DB_ID('<db_name>');



SQL Server Database migration checklist

Pre Migration Checklist 

  • Analyze the disk space of the target server for the new database.
  • Confirm the data and log file location for the target server.
  • Collect the information about the Database properties (Auto Stats, DB Owner, Recovery Model, Compatibility level,etc).
  • Collect the information of dependent applications, make sure application services will be stopped during the database migration.
  • Collect the information of database logins, users and their permissions.
  • Check the database for the Orphan users if any.
  • Check the SQL Server for any dependent objects (SQL Agent Jobs and Linked Servers).
  • Check, if the database is part of any maintenance plan.

Database Migration Checklist

  • Stop the application services.
  • Change the database to single user mode.
  • Take the latest backup of all the databases involved in migration.
  • Stop the SQL Services on live server.
  • Copy the backup from live to destination server.
  • Restore the databases on the target server on the appropriate drives.
  • Cross check the database properties as per pre-migration checklist output.
  • Execute the output of Login transfer script on the target server, to create logins on the target server.
  • Check for Orphan Users and Fix Orphan Users. 
  • Execute DBCC UPDATEUSAGE on the restored database.
  • Rebuild Indexes ,As per the requirement.
  • Update statistics.
  • Recompile procedures.
  • Configure Full backup, Log backup, integrity check, rebuild index jobs.

Post Migration Checklist

  • Check the integrity of database.
  • Start the application services, check the application functionality.
  • Check the SQL Server Error Log for login failures and other errors