Pages

Thursday 23 July 2009

Creating STRING and DWORD Values - VBScript

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set StdOut = WScript.StdOut


Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")


strKeyPath = "SOFTWARE\System Admin Scripting Guide"
strValueName = "String Value Name"
strValue = "string value"
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue


strValueName = "DWORD Value Name"
dwValue = 82
oReg.SetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,dwValue

Check Service - VBScript

On Error resume next
Dim StrComputer: strComputer = "."
Dim oservice
Dim oWMIService : Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Dim colServiceList: Set colServiceList = oWMIService.ExecQuery("Select * from Win32_Service where Name='SharedAccess'")


For Each oService in colServiceList


if oService.startmode="Manual" or oService.startmode="Disabled" then
wscript.quit(1)
end if


if oservice.State="Stopped" then
wscript.quit(1)
end if


Next

Stop and Start Service - VBScript

Option Explicit
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, intSleep
strComputer = "."
intSleep = 15000
WScript.Echo " Click OK, then wait " & intSleep & " milliseconds"


'On Error Resume Next
'NB strService is case sensitive


strService = " 'Alerter' "
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name ="_
& strService & " ")
For Each objService in colListOfServices
objService.StopService()
WSCript.Sleep intSleep
objService.StartService()
Next
WScript.Echo "Your "& strService & " service has Started"
WScript.Quit
' End of Example WMI script to Start / Stop services

Delete Registry if its Empty - VBScript

On Error Resume next
Const HKEY_LOCAL_MACHINE = &H80000002
Set wsh1=createobject("Wscript.shell")


strKeyRoot = "SOFTWARE\Microsoft\Microsoft SQL Server"
strKeyRoot1 = "SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent"
strKeyRoot2 = "SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\VIA"
strKeyRoot3 = "SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib"
strKeyRoot4 = "SOFTWARE\Microsoft\MSSQLServer\Client"
strKeyRoot5 = "SOFTWARE\Microsoft\MSSQLServer"
strKeyRoot6 = "SOFTWARE\Microsoft\SQL Redist\Setup"
strKeyRoot7 = "SOFTWARE\Microsoft\SQL Redist\1.00.000"
strKeyRoot8 = "SOFTWARE\Microsoft\SQL Redist"
strKeyRoot9 = "SOFTWARE\Microsoft\dasetup"
Set oReg=GetObject("winmgmts:root\default:StdRegProv")


oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\"
End If


oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot1, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent\"
End If


oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot2, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\VIA\"
End If


oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot3, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\"
End If


oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot4, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\"
End If


oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot5, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\MSSQLServer\"
End If


oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot6, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\SQL Redist\Setup\"
End If


oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot7, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\SQL Redist\1.00.000\"
End If


oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot8, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\SQL Redist\"
End If


oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot9, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\dasetup\"
End If


WScript.Quit

Kill Process -VBScript

Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strProcessKill = "'crvw.exe'"
Set objWMIService = GetObject("winmgmts:root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate()
Next
WScript.Quit

Script to Shutdown the system - VBScript

strComputer = "."
Set objWMIService = GetObject_
("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & _
strComputer & "\root\cimv2")


Set colOperating Systems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")


For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Win32Shutdown(1)
Next

Wednesday 15 April 2009

Find Serial Key of OS intalled - VBScript

Set SNSet = GetObject("winmgmts:").InstancesOf ("win32_OperatingSystem")

For each SN in SNSet
MsgBox "The serial number for the installed OS is: " & SN.SerialNumber
Next