VB Scripts



Short notes on Windows scripting and basic administrator script (vbscript) & Some useful VB script examples that assist you in automating system administrator tasks and helps you in developing appropriate script that best suits to your work. The system administrator can make use of this repository for commonly used scripts at their work and aimed to provide basic vb script knowledge for system administrators on Windows platform too. It is a simple repository where you can tailor your script needs from frequently used system administrator script examples.


What is Windows script file?
A Windows script is a text file. You can create a script with any text editor as long as you save your script with a WSH-compatible script extension (.js, vbs, or .wsf). 



To simplify your script writing, you can divide a script into more than one part. With this approach, you would create a .wsf file and use it as the starting point of execution. The other parts could be .js or .vbs files



Windows Script Host (WSH) enables scripts to be run directly in Windows by double-clicking a script file or by typing the name of a script file at a command prompt. Like Microsoft Internet Explorer, WSH serves as a controller of ActiveX scripting engines. WSH has very low memory requirements and is ideal for both interactive and non-interactive scripting needs.
The Windows Script Host object model consists of different objects. The root object is the WScript object.




The Windows Script Host object model provides a logical, systematic way to perform many administrative tasks


How to execute windows script file?
To Run Scripts Using the Windows-Based Script Host (Wscript.exe)
•At a command prompt type wscript.exe or cscript.exe, and then press ENTER.
•Set the script host properties you want, and then click OK.

•In Windows Explorer or My Computer, double-click the script file you want to run.


There are two versions of WSH:
A Windows-based version (Wscript.exe) that provides Windows-based properties for setting script properties, and a command prompt-based version (Cscript.exe) that provides command-line switches for setting script properties. You can run either of these versions by typing "wscript.exe" or "cscript.exe" at a command prompt. 





Script Examples:



'******** VB Script to initiate SCCM client action for machine policy*********************


t="Request & Evaluate Machine Policy"
k=initclientaction(t)
msgbox k
function initclientaction(t)
k=3


set cpApplet = CreateObject("CPAPPLET.CPAppletMgr")
Set actions = cpApplet.GetClientActions
For Each action In actions


    If Instr(action.Name,t) > 0 and k =3 Then
        action.PerformAction  
        initclientaction= true
  
        k=1
   
       
         
End if


Next
if not k=1 then
initclientaction=k
end if 
end function






================================================================

'****** VB Script to kill  process **************************





Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcess
Dim  strList, count


pname=inputbox("Please enter process name to kill ")




strComputer = "."




sqlcmd="Select * from Win32_Process Where Name = '" & pname&"'"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 
Set colProcess = objWMIService.ExecQuery (sqlcmd)

count=0

For Each objProcess in colProcess

if objProcess.Name=pname then


objProcess.Terminate()



end if 
next





================================================================


'******* VB Script to count  process **************************



 pname=inputbox("Enter process name to count")


msgbox pcount(pname)




function pcount(pname)


Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcess
Dim  strList, count, sqlcmd


strComputer = "."


sqlcmd="Select * from Win32_Process Where Name = '" & pname&"'"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 
Set colProcess = objWMIService.ExecQuery (sqlcmd)

count=0


For Each objProcess in colProcess

if objProcess.Name=pname then
count=count+1


end if 
next

pcount=count
end function




================================================================


'******** VB Script to get free disk space details  **************************



strComputer = "."


sqlcmd="Select * from Win32_Process Where Name = '" & pname&"'"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 




 Set coldisks = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk")


For Each objDisk in colDisks
    
   if objDisk.DeviceID = "C:" then    
         
                l2=objDisk.FreeSpace/1024
                l2=l2/1024
                l2=l2/1024
  
end if
Next


msgbox "Free Disk Space on C drive : "&left(l2,5)&" GB"





================================================================




'******* VB Script to start or stop Service  **************************




strComputer = "."

sqlcmd="Select * from Win32_Process Where Name = '" & pname&"'"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")


 sq="Select * from Win32_NTLogEvent Where Logfile = 'Application' and Type='Error'"
ii=0
desc=""

Set colLoggedEvents = objWMIService.ExecQuery(sq)

For Each objEvent in colLoggedEvents

ii=ii+1
if ii<=4 then
t=objEvent.TimeWritten
desc= left(t,12)&" ECode " & objEvent.EventCode & " Message: " & objEvent.Message & "!"&desc

end if
next
msgbox desc




================================================================



'******** VB Script to capture event logs  **************************
vServiceName="Alerter"

strComputer = "."

sqlcmd="Select * from Win32_Process Where Name = '" & pname&"'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 





    strWQL = "SELECT * FROM Win32_Service"
    strWQL = strWQL & " WHERE Name = '" & vServiceName & "'"
    Set colWinServiceSet = objWMIService.ExecQuery(strWQL)

    If colWinServiceSet.Count > 0 Then
        For Each objWinService in colWinServiceSet


                  'intRetVal = objWinService.StartService
      
           
                If objWinService.Started = False Then '// If Service is in Stop status
                    intRetVal = objWinService.StartService
                    
                
                
       
            Else  '// Stop Service
          
                'If objWinService.Started = True Then '// If Service is in Start status
                    intRetVal = objWinService.StopService
                    
                
                'End If
            End If
        Next
    Else
         errdesc= vServiceName & " not found."
                writefile file9,errdesc
    End If
    Set objWMIService = Nothing





Thanks for visiting ...