Showing posts with label Script. Show all posts
Showing posts with label Script. Show all posts

Wednesday, January 27, 2010

Restart a service from the Command line

Taken from Robbie Allen webpage : Robust_restart.vbs
(the problem is that it doesn't work because of an underscore "_" before the strSvcName variable. Remove it just as it is listed below.
Of course you can specify the strComputer(server name) and strSvcName (service shortname, for e.g. Audiosrv and not "windows audio" cause it won't work in the other way).

Restart.vbs
----------------------------------------------------------------------------------------------

strService = "spooler"
strComputer = "."

WScript.Echo "Restarting " & strService & "..."

' Stop dependent services
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objServiceList = objWMI.ExecQuery("Associators of " _
& "{Win32_Service.Name='" & strService & "'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
For Each objService In objServiceList
WScript.Echo " Stopping " & objService.Name
objService.StopService()
Next
WScript.Sleep 10000 ' Pause to allow services to stop
' Stop target service
Set objService = objWMI.Get("Win32_Service.Name='" & strService & "'")
WScript.Echo " Stopping " & objService.Name
objService.StopService()
WScript.Sleep 10000 ' Pause to allow service to stop

' Start target service
Set objService = objWMI.Get("Win32_Service.Name='" & strService & "'")
WScript.Echo " Starting " & objService.Name
objService.StartService()
WScript.Sleep 10000 ' Pause to allow service to start

' Start dependent services
Set objServiceList = objWMI.ExecQuery("Associators of " _
& "{Win32_Service.Name='" & strService & "'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
For Each objService In objServiceList
WScript.Echo " Starting " & objService.Name
objService.StartService()
Next
WScript.Echo "The script has completed successfully."
----------------------------------------------------------------------------------------------

To be run from a command line/script
Just type : cscript restart.vbs


What's the difference with this and doing a net stop service /y & net start service?
The difference is that if you have dependant services they will not start.
While using this script, it will start any dependant service as well as the requested service but it's not perfect. It will fail to tell the truth if a dependant service doesn't start. I would use sc.exe to query the service state after the restart script finished.

0 comments:

Post a Comment

Thursday, January 7, 2010

Command line Output sent to clipboard

Oh yes, this tool is so good that I had to advertise it :
Source : Here

For example: dir | clip (done, you already have the dir output on your clipboard instead of the normal output).

0 comments:

Post a Comment

Wednesday, January 6, 2010

Find out Disk Space Remotely thanks to DIR / Borrowing code

I just came across a really handy script to find out Total Disk sapce and free space and it works remotely just by using the dir command, this is awesome.

Source http://www.computing.net/answers/programming/batch-file-reporting-hdd-space/15057.html

Thank you Douglas for sharing this with the rest of the world.
-----------------------------------
:: tester.bat
:: 2007 Douglas Craig with massive assistance by Mechanix2Go

@echo off

setLocal EnableDelayedExpansion

:: check for path
if ()==(%1%) goto syntax

:: check existence of path
if not exist %1 goto badpath

:: find total used space
for /f "tokens=3 delims= " %%A in ('dir /s/-c %1 ^|find "File(s)"') do (
set totalused=%%A
)

:: find total free space
for /f "tokens=3 delims= " %%A in ('dir /-c %1 ^|find "Dir(s)"') do (
set freespace=%%A
)

:: output truncated totals in temp file for use later
echo.Used !totalused:~0,-9! >"testtemp.txt"
echo.Free !freespace:~0,-9! >>"testtemp.txt"

:: input truncated total used space from temp file
for /f "tokens=2 delims= " %%A in ('type testtemp.txt ^|find "Used"') do (
set totalused=%%A
)

:: input truncated free space
for /f "tokens=2 delims= " %%A in ('type testtemp.txt ^|find "Free"') do (
set freespace=%%A
)

:: calculate total drive space, percentage used, and percentage free
set /a totalspace=!totalused!+!freespace!
set /a percentused=(!totalused!*100)/!totalspace!
set /a percentfree=100-!percentused!

:: output results
echo.
echo Total space: %totalspace% GB
echo Free space: %freespace% GB (%percentfree%%%)
echo Used space: %totalused% GB (%percentused%%%)

:: clean up temp file
del testtemp.txt

goto end

:: syntax info
:syntax
echo.
echo.Usage: TESTER [path]
echo.
echo. where [path] is a directory on drive or network path
echo.
echo. example: TESTER \
echo. TESTER \\mynetwork\sharedfolder
goto end

:: invalid path
:badpath
echo.
echo.The path %1 is not accessible.
echo.

:end
-----------------------------------
What I would do is :
for /f %%i in (serverlist.txt) do TESTER.cmd \\%%i\c$ >>%%i.txt

et voilà

0 comments:

Post a Comment

Tuesday, November 24, 2009

The Pig Ping Script

This is my own personal ping script, feel free to use but please include the credits to the Pig



PigPing.cmd

del falla.txt >nul
del falluto.txt >nul
del timedout.txt > nul
del notresolving.txt >nul
del ok1.txt >nul


for /f %%i in (serverlist.txt) do call :set %%i
type ok1.txt | find /i "problem" >>falla.txt
for /f "tokens=1 delims=;" %%b in (falla.txt) do echo %%b >>falluto.txt
for /f %%y in (falluto.txt) do ping %%y -n 1 | find "timed out"&&echo %%y >>timedout.txt || echo %%y >>notresolving.txt
ren ok1.txt results.csv


:set
set serv=%1
IF [%1]==[] goto :fin


goto :next

:next
for /f "tokens=*" %%z in ('ping %serv% -n 1 ^| find /i "replY" ^|^| echo Problem found Check timedout.txt and notresolving.txt logs') do echo %serv%;"%%z" >>ok1.txt


:eof

:fin





All you need to do is to fill a list of servers (no space after the server names or IP Addresses), one per line.
Then you execute this script. It will create 3 useful files :
Results.csv Open this with excel, this should display a summary of the pinged servers.
Timedout.txt : servers that did not respond correctly (ping request timed out).
notresolving.txt : servers that are not resolving correctly to its IP Address.

0 comments:

Post a Comment