Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Script - VB or Powershell to Check Services and Uptime

Posted on 2014-04-04
8
Medium Priority
?
591 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 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
WatchGuard Case Study: NCR

With business operations for thousands of customers largely depending on the internal systems they support, NCR can’t afford to waste time or money on security products that are anything less than exceptional. That’s why they chose WatchGuard.

 
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

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

876 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