Powershell script to find Windows Server reboot date

  • Hello

    I am searching for a Powershell script which picks Windows Server names from SQL server table(eg: Instance.DB.tbServerList) & writes last reboot date to SQL server table(can be same or different table)

    Can someone help me in this?

    Cheers,

    Sree

  • --Create test database and table.

    CREATE DATABASE Test

    GO

    USE Test

    GO

    CREATE TABLE dbo.tbServerList (Srv sysname, lastRebootDate datetime)

    GO

    --Replace server names as required.

    INSERT dbo.tbServerList (Srv)

    VALUES ('Srv1'),('Srv2');

    GO

    /** Run PowerShell script **/

    --Check results.

    SELECT

    Srv

    ,lastRebootDate

    FROM dbo.tbServerList;

    GO

    Here is the PowerShell script.

    $inst = 'yourServer'

    $DB = 'Test'

    #Import the sqlps module to allow use of invoke-sqlcmd.

    Import-Module “sqlps” -DisableNameChecking

    $srvList = invoke-sqlcmd –ServerInstance $inst -Database $DB -Query "SELECT Srv FROM dbo.tbServerList;" | %{'{0}' -f $_[0]}

    foreach ($srv in $srvList)

    {

    $rebootDate = invoke-sqlcmd –ServerInstance $srv -Query "SELECT sqlserver_start_time FROM sys.dm_os_sys_info;" | %{'{0}' -f $_[0]}

    invoke-sqlcmd –ServerInstance $inst -Database $DB -Query "SET dateformat dmy; UPDATE dbo.tbServerList SET lastRebootDate = '$rebootDate' WHERE Srv = '$srv';"

    }

  • Thanks for your reply:-)Very useful script..:-):-):-)

    The code is showing the SQL server service restart date & time. But I am looking for server reboot time?Is it possible to tweak the same code to show server reboot time?

    Kind Regards,

    Sree

  • Try this:

    $inst = 'yourserver'

    $DB = 'Test'

    #Import the sqlps module to allow use of invoke-sqlcmd.

    Import-Module “sqlps” -DisableNameChecking

    $srvList = invoke-sqlcmd –ServerInstance $inst -Database $DB -Query "SELECT Srv FROM dbo.tbServerList;" | %{'{0}' -f $_[0]}

    foreach ($srv in $srvList)

    {

    $os = Get-WmiObject win32_OperatingSystem

    $rebootDate = $os.ConvertToDateTime($os.lastbootuptime)

    invoke-sqlcmd –ServerInstance $inst -Database $DB -Query "SET dateformat mdy; UPDATE dbo.tbServerList SET lastRebootDate = '$rebootDate' WHERE Srv = '$srv';"

    }

  • Thanks for the quick response..:)

    I tried to implement the same. lastRebootDate column for all servers is updated with the reboot time of first server in the table

    Srv lastRebootDate

    ----- ------------------------------

    Srv12015-10-18 04:20:12.000

    Srv22015-10-18 04:20:12.000

    Srv32015-10-18 04:20:12.000

    Could you please help?

  • Sorry, I had removed the -ComputerName switch during testing.

    $inst = 'yourServerName'

    $DB = 'Test'

    #Import the sqlps module to allow use of invoke-sqlcmd.

    Import-Module “sqlps” -DisableNameChecking

    $srvList = invoke-sqlcmd –ServerInstance $inst -Database $DB -Query "SELECT Srv FROM dbo.tbServerList;" | %{'{0}' -f $_[0]}

    foreach ($srv in $srvList)

    {

    $os = Get-WmiObject win32_OperatingSystem -ComputerName $srv

    $rebootDate = $os.ConvertToDateTime($os.lastbootuptime)

    invoke-sqlcmd –ServerInstance $inst -Database $DB -Query "SET dateformat mdy; UPDATE dbo.tbServerList SET lastRebootDate = '$rebootDate' WHERE Srv = '$srv';"

    }

  • Run this to see all your history... and no UI necessary 🙂

    get-eventlog System | where-object {$_.EventID -eq "6005"} | sort -desc TimeGenerated

  • The modified script worked for me....:-):-)Thank you very much 🙂

  • Hey..I have one question..what happens if any of the server mentioned in the server list is down?I cannot test it by bringing one of the server down;-)

  • Add a fictitious server name

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply