Solved

Script - VB or Powershell to Check Services and Uptime

Posted on 2014-04-04
8
578 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
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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 34

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 34

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
This article will help you understand what HashTables are and how to use them in PowerShell.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

837 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