add-pssnapin sqlserverprovidersnapin100 -ErrorAction SilentlyContinue
add-pssnapin sqlservercmdletsnapin100 -ErrorAction SilentlyContinue
CLS
$Server = "Server"
$Qry = "Select Top 5 Name from NOCTools.dbo.ADComputers Where Left(Name,1) in ('E','A')"
$ServerList = Invoke-Sqlcmd -MaxCharLength 10000000 -ServerInstance $Server -Query $Qry
Invoke-Sqlcmd -MaxCharLength 10000000 -ServerInstance $Server -Query "Truncate Table NOCTools.dbo.RebootReport"
foreach ($computer in $ServerList)
#$ServerList | Foreach-Object
{
Try{
$computer = $_.
$Computerobj = "" | select ComputerName, Uptime, LastReboot
$wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem" -EV Err -EA SilentlyContinue
$now = Get-Date
$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
$uptime = $now - $boottime
$d = $uptime.days
$h = $uptime.hours
$m = $uptime.Minutes
$s = $uptime.Seconds
$Computerobj.ComputerName = $computer
$Computerobj.Uptime = "$d Days $h Hours $m Min $s Sec"
$Computerobj.LastReboot = $boottime
$Computerobj
$Qry2 = "INSERT INTO NOCTools.dbo.RebootReport(Computer,RebootDate,Uptime,[Action],Reason,ADUser,Process)
VALUES ('$computer','$boottime','$d Days $h Hours $m Min $s Sec',NULL,NULL,NULL,NULL)"
Write-Host @Qry2
Invoke-Sqlcmd -MaxCharLength 10000000 -ServerInstance $Server -Query "INSERT INTO NOCTools.dbo.RebootReport(Computer,RebootDate,Uptime,[Action],Reason,ADUser,Process)
VALUES ('$computer','$boottime','$d Days $h Hours $m Min $s Sec',NULL,NULL,NULL,NULL)"
}
Catch {
Write-host $computer
}
}
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
PS C:\Users\usgltorres>
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE