Solved

Script - VB or Powershell to Check Services and Uptime

Posted on 2014-04-04
8
575 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

832 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