October 16, 2015 at 9:23 am
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
October 23, 2015 at 6:01 am
--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';"
}
October 29, 2015 at 10:25 am
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
October 29, 2015 at 11:53 am
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';"
}
October 30, 2015 at 6:35 am
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?
October 30, 2015 at 11:19 am
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';"
}
November 1, 2015 at 10:02 pm
Run this to see all your history... and no UI necessary
get-eventlog System | where-object {$_.EventID -eq "6005"} | sort -desc TimeGenerated
November 9, 2015 at 3:35 pm
The modified script worked for me....:-):-)Thank you very much
November 14, 2015 at 5:16 am
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;-)
November 14, 2015 at 10:55 am
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
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy