Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Script - VB or Powershell to Check Services and Uptime

Posted on 2014-04-04
8
Medium Priority
?
588 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 total points
ID: 39978084
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
ID: 39978151
Uptime can be found via

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

Expert Comment

by:becraig
ID: 39978540
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39978560
@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
 
LVL 29

Expert Comment

by:becraig
ID: 39978649
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 35

Expert Comment

by:Dan Craciun
ID: 39978818
@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 29

Expert Comment

by:becraig
ID: 39978912
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 29

Expert Comment

by:becraig
ID: 40013756
Did this provide the desired result ?

Or do you need any additional help with this.
0

Featured Post

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

721 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