Solved

Script - VB or Powershell to Check Services and Uptime

Posted on 2014-04-04
8
573 Views
Last Modified: 2014-05-14
Hi,

Can someone share some handy powershell or vb script to run on either server 2003 and 2008 to check if all services set to automatic are running and its uptime and dump the results to a text file.

Thanks!!!
0
Comment
Question by:syseng007
  • 4
  • 3
8 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
Comment Utility
Test if this works on PS2:
Get-CimInstance win32_service -Filter "startmode = 'auto' AND state != 'running'" -ComputerName computername | export-csv report.csv

Open in new window

HTH,
Dan
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Uptime can be found via

$wmi = Get-WmiObject -Class Win32_OperatingSystem
$wmi.LocalDateTime - $wmi.LastBootUpTime
0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
Something like this should work:

$Results = @()
gc serverlist.txt | % {
$computer = $_
$lastboottime = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime
$sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime) 
$uptimer = $sysuptime.days "days" $sysuptime.hours "hours" $sysuptime.minutes "minutes" $sysuptime.seconds "seconds" 
$slist = gwmi Win32_Service-computername $computer | where {$_.StartMode -ne "Auto"} | select Name
$results += $computer $uptimer $slist

}
$Results | out-file reportfile.csv

Open in new window

0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
@becraig: looks good, but the requirement was services with automatic startup that are running.

So something like (untested):
$slist = gwmi Win32_Service -computername $computer | where {$_.StartMode -eq "Auto" -and $_.State -eq "Running"} | select Name

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Expert Comment

by:becraig
Comment Utility
Thanks Dan, update:

$Results = @()
$Results += 
@"
Server, Service, StartMode, State, Uptime
"@
gc serverlist.txt | % {
$computer = $_
$lastboottime = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime
$sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime) 
$uptimer = $sysuptime.days "days" $sysuptime.hours "hours" $sysuptime.minutes "minutes" $sysuptime.seconds "seconds" 
$slist = gwmi Win32_Service-computername $computer
$slist | % {
$svcmode = $_.StartMode; $svcname = $_.Name; $svcstate = $_.State 
$Results += "$computer, $svcname, $svcstate, $svcmode, $uptimer"
}
}
$Results | out-file reportfile.csv

Open in new window

0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
@becraig: tried it and it threw an error on line 10. Had to rewrite it like this:
$uptimer = "$($sysuptime.days) days, $($sysuptime.hours) hours, $($sysuptime.minutes) minutes, $($sysuptime.seconds) seconds"

Open in new window

0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
Ok fixed it:


$Results = @()
$Results += 
@"
Server`tService`tState`tStartMode`tLastBoot
"@
gc serverlist.txt | % {
$computer = $_
$LastBoot = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer) | %{$_.ConverttoDateTime($_.LastBootUpTime)}

$slist = gwmi Win32_Service -computername $computer
$slist | % {
$svcmode = $_.StartMode; $svcname = $_.Name; $svcstate = $_.State 
$Results += "$computer`t$svcname`t$svcstate`t$svcmode`t$Lastboot"
}
}
$Results | out-file reportfile.csv

Open in new window

0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
Did this provide the desired result ?

Or do you need any additional help with this.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to dynamically set the form action using jQuery.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now