Monday, December 20, 2010

Perl Example

#!/bin/perl
#realsecure to agentservicename and ISS to aISS
$startCommand = "/etc/init.d/agentservicename start";
$stopCommand = "/etc/init.d/agentservicename stop";
# THE ONLY LINES THAT SHOULD NEED TO BE EDITED IN THIS SCRIPT ARE THE THREE ABOVE
# THIS SCRIPT SHOULD AUTOMATICALLY DETECT ALL OTHER SENSOR SETTINGS
# THE FIRST VALUE ABOVE SHOULD BE "#!" FOLLOWED BY THE VALUE OF "which perl" FROM THE COMMAND LINE
# THE SECOND VALUE ABOVE SHOULD BE THE START COMMAND FOR THE APPLICABLE PLATFORM
# THE THIRD VALUE ABOVE SHOULD BE THE STOP COMMAND FOR THE APPLICABLE PLATFORM
print "\nResetting aISS sensors for SiteProtector connectivity...\n\n";
# STOPPING THE aISSDAEMON SERVICE
$adjustedStop = "|" . $stopCommand;
open STOP, $adjustedStop;
# WAITING UNTIL THE aISSDAEMON SERVICE IS NO LONGER RUNNING
# THIS IS DONE BY CHECKING ALL ACTIVE SERVICES AND VERIFYING THE aISSDaemon SERVICE IS GONE
$stopWait = 0;
open(PS_F, "ps -ef|");
        while ($stopWait == 0) {
                $stillRunning = 0;
                while (<PS_F>) {
                        (@ps) = split;
                        foreach(@ps) {
                                if ($_ =~ /aISSDaemon/) {
                                        $stillRunning = 1;
#                                       print "aISSDaemon still running...\n";
                                }
                        }
                }
                if ($stillRunning == 0) {
                        $stopWait = 1;
                }
        }
close(PS_F);
close (STOP);
print "\n";
# OPEN CRYPT.POLICY AND READ IN ALL LINES, SET TO @CRYPT ARRAY.
open(INFILE,  "/opt/aISS/aISSDaemon/crypt.policy") or die "Can't open crypt.policy file: $!";
@crypt = <INFILE>;
$cryptReset = 0;
print "Allowing first connection in crypt.policy:  ";
# RESET ALLOW FIRST CONNECTION VALUE TO 1 WHEN STRING MATCH IS FOUND
foreach(@crypt) {
#       print $_;
        if ($_ =~ /allowfirst/) {
                $_ = "allowfirstconnection      =L      1;\n";
                print "Done\n";
                $cryptReset = 1;
        }
#       print $_, "\n";
}
# IF THERE IS NO STRING MATCH FOR ALLOW FIRST CONNECTION, ADD IT TO THE PROPER PLACE IN THE FILE
if ($cryptReset == 0) {
        foreach(@crypt) {
#               print $_;
                if ($_ =~ /authent/) {
                        $_ = $_ . "allowfirstconnection      =L      1;\n";
                        print "MaISSing...Added\n";
                }
#       print $_, "\n";
        }
}
close(INFILE);
# OPEN CRYPT.POLICY FOR WRITING AND SUBMIT NEW VALUES
open(OUTFILE, ">/opt/aISS/aISSDaemon/crypt.policy") or die "Can't open crypt.policy file: $!";
foreach(@crypt) {
        print OUTFILE $_;
#       print $_, "\n";
}
close(OUTFILE);
# OPEN aISSDAEMON.POLICY AND READ IN ALL LINES, SET TO @DAEMON ARRAY.
$sensorCount = 0;
$masterCount = 0;
open(INFILE,  "/opt/aISS/aISSDaemon/aISSDaemon.policy") or die "Can't open aISSDaemon.policy file: $!";
@daemon = <INFILE>;
# PERFORM LINE BY LINE CHECKS ON @DAEMON ARRAY AND PERFORM FUNCTIONS BELOW
foreach(@daemon) {
#       print $_;
# RESET MASTER CONSOLE STATUS FOR EACH OCCURRENCE IN THE ARRAY
        if ($_ =~ /master_console/) {
                $_ = "master_console  =S      ;\n";
                $masterCount = $masterCount + 1;
        }
# SET ELEMENTS OF @SENSORLOC ARRAY TO PATHS IN POLICY PATH ENTRIES
        if ($_ =~ /policy_path/) {
# THIS IS THE TRICKY PART, THE STRING BELOW SEARCHES FOR...
# NON-WHITESPACE --> WHITESPACE --> NON-WHITESPACE --> WHITESPACE --> POLICY PATH --> SEMICOLON
# THE PARENTHESES AROUND THE [\S]+ SET THE VALUE OF $1 TO THAT VALUE WHICH IS...
# THE THIRD OCCURRENCE OF NON-WHITESPACE (FIRST CHAR OF THE PATH) UP TO THE SEMICOLON (LAST CHAR OF THE PATH)
                if ($_ =~ /[\S]+[\s]+[\S]+[\s]+([\S]+)+[;]/) {
                        $sensorLoc[$sensorCount] = $1;
                        $sensorCount = $sensorCount + 1;
                        print "Sensor #", $sensorCount, " found at path:  -->", $1, "<--\n";
                }
        }
#       print $_, "\n";
}
close(INFILE);
print "Resetting ", $masterCount, " occurrences of master status:  Done\n";
# DELETE ALL PUBLIC KEYS IN ALL PATHS LISTED IN @SENSORLOC ARRAY
$feedbackCount = 1;
foreach(@sensorLoc) {
        print "Deleting existing Certicom Keys on Sensor #", $feedbackCount, ":  ";
        $dirTemp = $_ . "Keys/CerticomNRA";
#       print $dirTemp , "\n";
        chdir ($dirTemp);
        @filelist = glob("*");
#       print @filelist, "\n";
        unlink glob("*.PubKey");
        @filelist = glob("*");
#       print @filelist, "\n";
        print "Done\n";
        print "Deleting existing RSA Keys on Sensor #", $feedbackCount, ":  ";
        $dirTemp = $_ . "Keys/RSA";
#       print $dirTemp , "\n";
        chdir ($dirTemp);
        @filelist = glob("*");
#       print @filelist, "\n";
        unlink glob("*.PubKey");
        @filelist = glob("*");
#       print @filelist, "\n";
        print "Done\n";
        $feedbackCount = $feedbackCount + 1;
}
# OPEN aISSDAEMON.POLICY FOR WRITING AND SUBMIT NEW VALUES
open(OUTFILE, ">/opt/aISS/aISSDaemon/aISSDaemon.policy") or die "Can't open aISSDaemon.policy file: $!";
foreach(@daemon) {
        print OUTFILE $_;
#       print $_, "\n";
}
close(OUTFILE);
print "\n";
# RESTARTING THE aISSDaemon SERVICE
$adjustedStart = "|" . $startCommand;
open START, $adjustedStart;
close(START);
print "\nAll aISS Sensors have been reset for SP connectivity.\n\n";

Sunday, November 21, 2010

Recovery

' Recovery Scenario example
Browser("Internet Explorer cannot").Page("Internet Explorer cannot").WebButton("Diagnose Connection Problems").Click
Browser("Internet Explorer cannot").Page("Internet Explorer cannot").WebButton("Diagnose Connection Problems");
Recovery.Enabled=True
Recovery.Activate
wait(1)

'MyCurrentStatus=Recovery.Enabled
''msgbox MyCurrentStatus
'If Not( MyCurrentStatus ) Then
'Recovery.Enabled=True
'End If
'Recovery.Activate
'Recovery = MyCurrentStatus
'msgbox "helo"

This below example will retrieve specified table cell data

' This below example will retrieve specified table cell data
Set oDesc = Description.Create
'oDesc("innerText").Value = "one.*"
oDesc("micClass").Value = "WebTable"
'oDesc("innerText").Value  = True
Set allTables = Browser("Browser").Page("Page").ChildObjects(oDesc)
msgbox allTables.Count
For i=0 to allTables.Count-1
 'msgbox allTables.item(i).ToString()

 If i=2Then
  'msgbox "I value is "&i
  Set des = Description.Create
  des("outertext").Value = "one.*"
  des("outertext").RegularExpression = true
  val = Browser("Browser").Page("Page").WebTable(des).ColumnCount(i)
  msgbox "Coulmn Count is "&val
  celldata = Browser("Browser").Page("Page").WebTable(des).GetCellData(1,i)
  msgbox celldata
  msgbox Browser("Browser").Page("Page").WebTable(des).RowCount
 
 End If
Next
'msgbox objPro.RowCount  ' displays the no. of rows present in a web table
'msgbox objPro.ColumnCount(1) ' displays number of columns in the first row
'msgbox objPro.GetCellData(2,1)
'

' ///////////////////////////////////////////
'Set oDesc = Description.Create
''oDesc("micclass").value = "webbutton"    'this statement working, returned  1
'oDesc("micclass").Value = "webtable"
'oDesc("micclass").RegularExpression = True
'set obj = Browser("Browser").Page("Page").ChildObjects(oDesc)
'
''this loop displays only 1,1 info. it  wont displays the table name or properties
''For i=0 to obj.count-1
'' msgbox obj.item(i).ToString() 
''Next
'
'msgbox "Total webtables " &obj.Count
'' when there was static tables and independent table, means not nested table. For nested table this index will not work
''Set objPro = Browser("Browser").Page("Page").WebTable("index := 0")
'
'' working to identify , when there were nested tables
'Set objPro = Browser("Browser").Page("Page").WebTable("index := 1")
'
'msgbox objPro.RowCount  ' displays the no. of rows present in a web table
'msgbox objPro.ColumnCount(1) ' displays number of columns in the first row
'msgbox objPro.GetCellData(2,1)
'
''set res = Browser("Browser").Page("Page").Webtable("").GetTOProperties
'''msgbox res.count
''For k=0 to  res.count-1
'' msgbox res(k).Name
'' msgbox res(k).value
''Next

Dynamically reading Frames and its contents

' C:\Perl\html\index.html
Dim frame(2)
frame(1) = "PerlDoc"
frame(0) = "TOC"
On Error Resume Next
For i=0 to ubound(frame)-1
 'msgbox frame(i)
     If Browser("ActivePerl User Guide").Page("ActivePerl User Guide").Frame(frame(i)).Link("ASPN Perl").Exist Then
   msgbox frame(i)
   msgbox "yes"
   If Err.Number <= 0 Then
     Browser("ActivePerl User Guide").Page("ActivePerl User Guide").Frame(frame(i)).Link("ASPN Perl").Click
     Else
     msgbox frame(i) 
   End If
 
  End If
Next

'Browser("ActivePerl User Guide").Page("ActivePerl User Guide").Frame("PerlDoc").Link("ASPN Perl").Click
Browser("ActivePerl User Guide").Page("ActivePerl User Guide").Frame("PerlDoc").Link("ASPN Perl")
'Browser("Internet Explorer cannot").Page("Internet Explorer cannot").Sync
'Browser("Internet Explorer cannot").CloseAllTabs

ChildObjects - Frames,Dynamic Array

'http://localhost/sqlitemanager/
'Browser("WAMP5 Homepage").Page("WAMP5 Homepage").Link("phpinfo( )").Click @@ hightlight id_;_Browser("WAMP5 Homepage").Page("WAMP5 Homepage").Link("phpinfo( )")_;_script infofile_;_ZIP::ssf7.xml_;_
'Browser("WAMP5 Homepage").Page("WAMP5 Homepage").Link("SQLitemanager 1.2.0").Click @@ hightlight id_;_Browser("WAMP5 Homepage").Page("WAMP5 Homepage").Link("SQLitemanager 1.2.0")_;_script infofile_;_ZIP::ssf8.xml_;_
'Browser("WAMP5 Homepage").Page("SQLiteManager").Frame("main").WebCheckBox("uploadDB").Set "ON" @@ hightlight id_;_Browser("WAMP5 Homepage").Page("SQLiteManager").Frame("main").WebCheckBox("uploadDB")_;_script infofile_;_ZIP::ssf9.xml_;_
'Browser("WAMP5 Homepage").Page("SQLiteManager").Frame("main").WebCheckBox("uploadDB").Set "OFF" @@ hightlight id_;_Browser("WAMP5 Homepage").Page("SQLiteManager").Frame("main").WebCheckBox("uploadDB")_;_script infofile_;_ZIP::ssf10.xml_;_
'Dynamic Arrays
Dim dynamicArray()
Set oDesc = Description.Create
oDesc("micclass").Value = "Frame"
set cObj = Browser("WAMP5 Homepage").Page("SQLiteManager").ChildObjects(oDesc)
'msgbox cObj.Count
ReDim dynamicArray(cObj.Count)
' below for loop will convert  as below :
' from =  [ welcome ] link
'  to = welcome
For i=0 to cObj.Count -1
 childObjVal = cObj.item(i).ToString()  ' msgbox childObjVal
 startPos = Instr(1,childObjVal,"[")
 newVal = mid(childObjVal,startPos+1,len(childObjVal))
 endPos = instr(1,newVal,"]")
 endVal = mid(newVal,1,endPos-1)
    dynamicArray(i) = trim(endVal)  ' msgbox dynamicArray(i)
Next
'msgbox ubound(dynamicArray)
' below for loop is used click left side frames specific link
For j=0 to ubound(dynamicArray)-1
    strArray = dynamicArray(j)
If  strArray = "left" Then
    Set oDes = Description.Create
 oDes("micclass").Value = "Link"
 set childLinks = Browser("WAMP5 Homepage").Page("SQLiteManager").Frame(strArray).ChildObjects(oDes)  ' Browser("WAMP5 Homepage").Page("SQLiteManager").Frame("left").Link("Test").Click @@ hightlight id_;_Browser("WAMP5 Homepage").Page("SQLiteManager").Frame("main").WebCheckBox("uploadDB")_;_script infofile_;_ZIP::ssf9.xml_;_
     For k=0 to childLinks.Count -1
  childLinksVal = childLinks.item(k).ToString()    ' msgbox childLinksVal
  startPos = Instr(1,childLinksVal,"[")
  newVal = mid(childLinksVal,startPos+1,len(childLinksVal))
  endPos = instr(1,newVal,"]")
  endVal =  mid(newVal,1,endPos-1)   'dynamicArray(i) = trim(endVal)
  endVal = trim(endVal)
  msgbox trim(endVal)
  If endVal = "Test" Then
   Browser("WAMP5 Homepage").Page("SQLiteManager").Frame(strArray).Link(endVal).Click
   msgbox "Test is clicked"
  End If
 Next
End If
Next
' How to find co-ordinates of any text in the application
'l = 0
't = 0
'r = 0
'b = 0
'result = Window("Mozilla Firefox").WinObject("MozillaWindowClass").GetTextLocation("test123",l,t,r,b) @@ hightlight id_;_2164128_;_script infofile_;_ZIP::ssf1.xml_;_
'If result Then @@ hightlight id_;_460284_;_script infofile_;_ZIP::ssf2.xml_;_
' MsgBox "Text found. Coordinates:" & l & "," & t & "," & r & "," & b
'End If

' GetTOProperties example
'set objPro = Window("Mozilla Firefox").WinObject("MozillaWindowClass").GetTOProperties
'msgbox objPro.Count
'For i=0 to objPro.count-1
' 'msgbox objPro(i).Name
'If objPro(i).Name = "text" Then
' msgbox objPro(i).Name ' property name
'End If
'Next

ChildObjects - Click desired link

' Browser("iTKO LISA Functional Testing").Page("iTKO LISA Functional Testing").Link("Key Features").Click
Set oDesc = Description.Create()
    oDesc("micclass").Value = "Link"
 'oDesc("Class Name").Value = "Link"   
'Set Lists = Browser("iTKO LISA Functional Testing").Page("iTKO LISA Functional Testing").ChildObjects(oDesc)
Set Lists = Browser("iTKO LISA Functional Testing").Page("WAMP5 Homepage").ChildObjects(oDesc)
'Set Lists = Browser("iTKO LISA Functional Testing").Page("Internet Explorer cannot").ChildObjects(oDesc)
    NumberOfLists = Lists.Count()
 'msgbox NumberOfLists
 bVal = ""
For i = 0 To NumberOfLists - 1
    sVal = Lists.item(i).ToString()
 'msgbox sVal  ' return value like - [ xyz ] link
 startPos = Instr(1,sVal,"[")
 newVal = mid(sVal,startPos+1,len(sVal))
 endPos = instr(1,newVal,"]")
 endVal = mid(newVal,1,endPos-1)
 msgbox endVal
Next
'Browser("iTKO LISA Functional Testing").Page("Internet Explorer cannot").Link("More information").Click
'If endVal="test123" Then
'  msgbox endVal
'  Reporter.ReportEvent micPass,"Match Found","Success - Found the match"
'  Else
'   'Reporter.ReportEvent micFail,"Match Not Found","Fail - Match not found"
'    End If

Import Data Shet - example

' Import Data Sheet
DataTable.Import "C:/perldbtesting.xls"
rCount = DataTable.GetRowCount
msgbox rCount
For i=1 to rCount Step 1
 DataTable.SetCurrentRow(i)
  sno = DataTable.Value("sno",dtGlobalSheet)
  name_ = DataTable.Value("name",dtGlobalSheet)
       msgbox sno
    msgbox name_
Next
DataTable.DeleteSheet "Action1"
DataTable.AddSheet("newsheet").AddParameter "newParam","Added new sheet & and then added new column param and value is filled"
oldVal = DataTable.Value ("newParam","newsheet")
msgbox oldVal

FSO Examples

Set fso = CreateObject ("Scripting.FileSystemObject")
If fso.FolderExists ("C:\parent") Then
 ' fso.GetParentFolderName ("C:\parent")            '- displays C:/
 'msgbox fso.GetFolder("C:\parent").IsRootFolder       ' false
     'icount = fso.GetFolder("C:\parent").SubFolders.Count     ' returns count
    Set f = fso.GetFolder("c:\parent")
    Set sf = f.SubFolders
  For Each f1 in sf
   s = s & f1.name
   s = s & ";"
  Next
   ShowFolderList = s
End If
  msgbox ShowFolderList
  ' lst = Array(10)    
  lst = split (ShowFolderList, ";")
  icount = ubound(lst)
  For i=0 to icount-1 step 1
   msgbox lst(i)
  Next
 
  'msgbox arr()
'set obj = CreateObject ("WScript.Shell")
'set oExec = obj.Exec ("perl c:\Perl\eg\example.pl")
'msgbox oExec.Status
'If  oExec.Status = 0 Then
' Reporter.ReportEvent micWarning, perlscript, "Success"
' Else
' Reporter.ReportEvent micWarning, perlscript, "Fail"
'End If
'Set fso = CreateObject ("Scripting.FileSystemObject")
'If  fso.FolderExists ("C:\QTPPractice")  Then
' msgbox "Folder Already Exists"
' Reporter.ReportEvent micWarning, FolderExits, "Folder Already Exists"
'Else
'  fso.CreateFolder ("C:\QTPPractice")
'       '  set fileobj = fso.CreateTextFile ("C:\QTPPractice\textfile.txt", ForWriting, True)
'  set fileobj = fso.CreateTextFile ("C:\QTPPractice\textfile.txt")
'  msgbox "Folder and File Created"
'  Reporter.ReportEvent micPass, FolderExits, "Folder and File Created"
'End If

WMI examples

'Set Obj = CreateObject("WScript.Shell")
' val = Obj.Run ("net user rupakala pa$$w0rd /add") -- will execute at command prompt, as background process
'val = Obj.Run ("cmd /K perl c:/Perl/eg/example.pl >c:/testfile.txt")     ' output will be written into testfile.txt
'msgbox val ' returns 0
'Obj.CreateShortcut "C:\Users\Administrator\Desktop\aaaaa.lnk"
' msgbox Obj.CurrentDirectory    '  -----displays current directory path of  script
'set WshSysEnv = Obj.Environment("SYSTEM")
'msgbox WshSysEnv.Item("OS")
'set exe = Obj.Exec ("notepad.exe")
' --- WMI  examples
'Set LoginProfiles = GetObject("winmgmts:").InstancesOf ("Win32_NetworkLoginProfile")
'   For each Profile in LoginProfiles
'     msgbox Profile.Name
'     'msgbox Profile.LogonName
'   next
Set net = CreateObject("WScript.Network")   
' net.MapNetworkDrive "P:", "file://pc/public%22,%22True%22,%22Administrator",""
'msgbox net.ComputerName
'msgbox net.UserName
Set oDrives = net.EnumNetworkDrives()
msgbox oDrives.Count
        For i = 0 to oDrives.Count - 1 Step 2
          msgbox "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
         Next

Wednesday, October 20, 2010

Switching Service Accounts to Local Service

Changes the service account to LocalService for any services running under the hypothetical service account Netsvc.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery _
    ("Select * from Win32_Service")
For each objService in colServices
    If objService.StartName = ".\netsvc" Then
        errServiceChange = objService.Change _
        ( , , , , , , "NT AUTHORITY\LocalService" , "") 
    End If
Next

Stopping Services Running Under a Specific Account

Stops all services running under the hypothetical service account Netsvc.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery _
    ("Select * from win32_Service")
For each objService in colServices
    If objService.StartName = ".\netsvc" Then
        errReturnCode = objService.StopService()
    End If
Next

Stopping a Service and Its Dependents

Stops the NetDDE service and all its dependent services.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery("Associators of " _
   & "{Win32_Service.Name='NetDDE'} Where " _
        & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
For each objService in colServiceList
    objService.StopService()
Next
Wscript.Sleep 20000
Set colServiceList = objWMIService.ExecQuery _
        ("Select * from Win32_Service where Name='NetDDE'")
For each objService in colServiceList
    errReturn = objService.StopService()
Next

Starting a Service and Its Dependents

Starts the NetDDE service and all its dependent services.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service where Name='NetDDE'")
For each objService in colServiceList
    errReturn = objService.StartService()
Next
Wscript.Sleep 20000
Set colServiceList = objWMIService.ExecQuery("Associators of " _
   & "{Win32_Service.Name='NetDDE'} Where " _
        & "AssocClass=Win32_DependentService " & "Role=Dependent" )
For each objService in colServiceList
    objService.StartService()
Next

Starting AutoStart Services that have Stopped

Restarts any auto-start services that have been stopped.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
 ("Select * from Win32_Service Where State = 'Stopped' and StartMode = " _
     & "'Auto'")
For Each objService in colListOfServices
    objService.StartService()
Next

Retrieving Service Status Changes from Event Logs

Retrieves events from the System event log that have an event ID of 7036. These events are recorded any time a service changes status.

Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceEvents = objWMIService.ExecQuery _
    ("Select * from Win32_NTLogEvent Where Logfile = 'System' and " _
        & "EventCode = '7036'")
For Each strEvent in colServiceEvents
    dtmConvertedDate.Value = strEvent.TimeWritten
    Wscript.Echo dtmConvertedDate.GetVarDate   
    Wscript.Echo strEvent.Message
Next

Retrieving Service Status

Returns a list of all the services installed on a computer, and indicates their current status (typically, running or not running).

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
    ("Select * from Win32_Service")
For Each objService in colRunningServices
    Wscript.Echo objService.DisplayName  & VbTab & objService.State
Next

Retrieving Service Properties

Retrieves a complete list of services and their associated properties. Information is saved to a text file:

C:\Scripts\Service_List.cs.
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile("c:\scripts\service_list.csv", _
    ForAppending, True)
objLogFile.Write _
    ("System Name,Service Name,Service Type,Service State, Exit " _
        & "Code,Process ID,Can Be Paused,Can Be Stopped,Caption," _
        & "Description,Can Interact with Desktop,Display Name,Error " _
        & "Control, Executable Path Name,Service Started," _
        & "Start Mode,Account Name ")
objLogFile.Writeline
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
        ("Select * from Win32_Service")
For Each objService in colListOfServices
    objLogFile.Write(objService.SystemName) & ","
    objLogFile.Write(objService.Name) & ","
    objLogFile.Write(objService.ServiceType) & ","
    objLogFile.Write(objService.State) & ","
    objLogFile.Write(objService.ExitCode) & ","
    objLogFile.Write(objService.ProcessID) & ","
    objLogFile.Write(objService.AcceptPause) & ","
    objLogFile.Write(objService.AcceptStop) & ","
    objLogFile.Write(objService.Caption) & ","
    objLogFile.Write(objService.Description) & ","
    objLogFile.Write(objService.DesktopInteract) & ","
    objLogFile.Write(objService.DisplayName) & ","
    objLogFile.Write(objService.ErrorControl) & ","
    objLogFile.Write(objService.PathName) & ","
    objLogFile.Write(objService.Started) & ","
    objLogFile.Write(objService.StartMode) & ","
    objLogFile.Write(objService.StartName) & ","
    objLogFile.writeline
Next
objLogFile.Close

Resuming AutoStart Services that are Paused

Restarts any auto-start services that have been paused.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
 ("Select * from Win32_Service Where State = 'Paused' and StartMode = 'Auto'")
For Each objService in colListOfServices
    objService.ResumeService()
Next

Removing a Service

Removes a hypothetical service named DbService.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
    ("Select * from Win32_Service Where Name = 'DbService'")
For Each objService in colListOfServices
    objService.StopService()
    objService.Delete()
Next

Pausing Services Running Under a Specific Account

Pauses all services running under the hypothetical service account Netsvc.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery _
    ("Select * from Win32_Service")
For each objService in colServices
    If objService.StartName = ".\netsvc" Then
        errReturnCode = objService.PauseService()
    End If
Next
 

©2010 Software Testing powered by Free Blogger Templates | Author : Anand Satish