Pages

Friday, 15 June 2012

Get System Information - VBScript


Set oSystemSet = GetObject("winmgmts:").InstancesOf("Win32_ComputerSystem")


For Each oSystem in oSystemSet


 system_name = oSystem.Caption 
 system_type = oSystem.SystemType
 system_mftr = oSystem.Manufacturer
 system_model = oSystem.Model
Next


Set oProcSet = GetObject("winmgmts:").InstancesOf("Win32_Processor")


For Each oSystem in oProcSet
 proc_desc = oSystem.Caption 
 proc_mftr = oSystem.Manufacturer
 proc_mhz = oSystem.CurrentClockSpeed
Next


Set oBiosSet = GetObject("winmgmts:").InstancesOf("Win32_BIOS")


For Each oSystem in oBiosSet
      bios_info = oSystem.Version
Next


Set oZoneSet = GetObject("winmgmts:").InstancesOf("Win32_TimeZone")


For Each oSystem in oZoneSet
      loc_timezone = oSystem.StandardName
Next


Set oOSSet = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")


For Each oSystem in oOSSet


 os_name = oSystem.Caption
 os_version = oSystem.Version
 os_mftr = oSystem.Manufacturer
 os_build = oSystem.BuildNumber
 os_dir = oSystem.WindowsDirectory
 os_locale = oSystem.Locale
 os_totalmem = oSystem.TotalVisibleMemorySize
 os_freemem = oSystem.FreePhysicalMemory
 os_totalvirmem = oSystem.TotalVirtualMemorySize
 os_freevirmem = oSystem.FreeVirtualMemory
 os_pagefilesize = oSystem.SizeStoredInPagingFiles


Next


sMsg = ("OS Name:  " & os_name & Chr(10))
sMsg = sMsg & ("Version:  " & os_version & " Build " & os_build & Chr(10))
sMsg = sMsg & ("OS Manufacturer:  " & os_mftr & Chr(10))
sMsg = sMsg & ("oSystem Name:  " & system_name & Chr(10))
sMsg = sMsg & ("oSystem Manufacturer:  " & system_mftr & Chr(10))
sMsg = sMsg & ("oSystem Model:  " & system_model & Chr(10))
sMsg = sMsg & ("oSystem Type:  " & system_type & Chr(10))
sMsg = sMsg & ("Processor:  " & proc_desc & " " & proc_mftr & " ~" & proc_mhz & "Mhz" & Chr(10))
sMsg = sMsg & ("BIOS Version:  " & bios_info & Chr(10))
sMsg = sMsg & ("Windows Directory:  " & os_dir & Chr(10))
sMsg = sMsg & ("Locale:  " & os_locale & Chr(10))  
sMsg = sMsg & ("Time Zone:  " & loc_timezone & Chr(10))
sMsg = sMsg & ("Total Physical Memory:  " & os_totalmem & "KB" & Chr(10))
sMsg = sMsg & ("Available Physical Memory:  " & os_freemem & "KB" & Chr(10))
sMsg = sMsg & ("Total Virtual Memory:  " & os_totalvirmem & "KB" & Chr(10))
sMsg = sMsg & ("Available Virtual Memory:  " & os_freevirmem & "KB" & Chr(10))
sMsg = sMsg & ("Page File Space : " & os_pagefilesize & "KB" & Chr(10))


MsgBox sMsg, 0,"System Summary Information"

Get Serial Number of Operating system Installed - 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

Get Operating System installed on a machine


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    Wscript.Echo objOperatingSystem.Caption & " " & _
        objOperatingSystem.Version
Next

Get ComputerName and Username - VBScript


Set oNetwork = CreateObject("WScript.Network")
sUserName = oNetwork.UserName
sComputerName = oNetwork.ComputerName
MsgBox("UserName = " & sUserName & vbCRLF & "ComputerName = " & sComputername)

Get IP Address of machine - VBScript


On Error Resume Next
Dim IPADDRES 
Dim WshShell 


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")


Set WshShell=CreateObject("WScript.Shell")


For Each objNicConfig In colNicConfigs
      For Each strIPAddress In objNicConfig.IPAddress
             IPADDRES = strIPAddress
      Next
Next


msgbox IPADDRES


Set WshShell = Nothing
WScript.Quit

Create Text file and write lines using VBScript


Following script will create sample.txt file in the location where this script is stored and 10.10..10.93 will be written to sample.txt file




Dim objWshShell, objFSO, sScriptDir
Set objWshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")


sScriptDir=objFSO.GetParentFolderName(WScript.ScriptFullName)
strSecFileName = sScriptDir & "\sample.txt"


Set objSecFile = objFSO.CreateTextFile(strSecFileName, True)
objSecFile.WriteLine("10.10.10.93")


Set objFSO = Nothing
Set objWshShell = Nothing

Change File Attributes - VBScript


use following script to change attributes of any file or folder.
options are normal, read-only, hidden and system.
any combination of attributes may be set.


Dim FSO, fil, f, attr, i, Arg
Set FSO = CreateObject("Scripting.FileSystemObject")


  '--check for drag-and-drop:
   
     If WScript.arguments.count = 0 Then
           Arg = InputBox("Enter the path of the file or folder you want to change the attributes of.", "Change File Attributes")
     Else
           Arg = WScript.arguments.item(0)
     End If


If Arg = "" Then WScript.quit




  '--check for existence of either file or folder and keep track of which with i :


If FSO.FileExists(Arg) = True Then   
  f = "File" 
  i = 1
ElseIf FSO.FolderExists(Arg) = True Then
  f = "Folder"
  i = 2  
Else
  MsgBox "Path is wrong. No such file or folder.", 16, "Wrong Path"
  WScript.quit
End If


attr = InputBox("Enter the attributes number. Add numbers to get the desired combination of attributes." & VBCrLf & "Normal is 0" & VBCrLf & "ReadOnly is 1" & VBCrLf & "Hidden is 2" & VBCrLf & "System is 4", "Choose Attributes Setting", "0")


'--make sure that input is a number from 0 to 7:


If isNumeric(attr) = False Then
    MsgBox "Wrong entry. Must be a number from 0 to 7.", 16, "Wrong Number"
    WScript.quit
End If
If cInt(attr) > 7 or cInt(attr) < 0 Then
    MsgBox "Wrong entry. Only 0 to 7 are possible.", 16, "Wrong Number"
    WScript.quit
End If


  If i = 1 Then
     Set fil = FSO.GetFile(Arg)
     fil.attributes = attr
     Set fil = Nothing
     MsgBox f & " attributes changed.", 0, "All Done"
  End If
  If i = 2 Then
     Set fil = FSO.GetFolder(Arg)
     fil.attributes = attr
     Set fil = Nothing
    MsgBox f & " attributes changed.", 0, "All Done"
 End If