Pages

Friday, 15 June 2012

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




Increment ActiveSetup Version - VBScript


'This script will create the Active setup if not present on the system, otherwise it will increment the version of the active setup 


' Need to replace the "Any that you want to run" with the exact path of the EXE/MSI/Script which has to run
' Replace the "<NameOfActiveSetup>" with the related unique application name




Option Explicit
On Error Resume Next
Dim WshShell ,PrgFiles,KeyValue
Set WshShell=CreateObject("WScript.Shell")




KeyValue=WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Active Setup\Installed Components\<NameOfActiveSetup>\Version")


WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Active Setup\Installed Components\<NameOfActiveSetup>\StubPath", "Any that you want to run"


'Err.Clear


If  Err.Number=0 Then
     
WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Active Setup\Installed Components\<NameOfActiveSetup>\Version",KeyValue+1,"REG_SZ"   

Else

WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Active Setup\Installed Components\<NameOfActiveSetup>\Version","1","REG_SZ"

End If




Set WshShell = Nothing
Set PrgFiles = Nothing


WScript.Quit

Get current directory - VBScript


on Error Resume Next


Set objWshShell = CreateObject("WScript.Shell")
Set objFileSystem = CreateObject("Scripting.FileSystemObject")

sScriptDir=objFileSystem.GetParentFolderName(WScript.ScriptFullName)


msgbox sscriptdir

Hide ARP Entry of MSI

use following script to Hide ARP entry of an MSI



Dim WSHShell,strRegKey 
Set WSHShell = WScript.CreateObject("WScript.Shell") 
WSHShell.Regwrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\<ProductCode>\SystemComponent","1","REG_DWORD"

Check Free Disk Space - VBScript


strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_LogicalDisk WHERE DeviceID = 'C:'",,48) 
For Each objItem in colItems 
    Bytes = objItem.FreeSpace


If Bytes >= 1073741824 Then
SetBytes = Round(FormatNumber(Bytes / 1024 / 1024 / 1024, 2), 0) & " GB"
ElseIf Bytes >= 1048576 Then
SetBytes = Round(FormatNumber(Bytes / 1024 / 1024, 2), 0) & " MB"
ElseIf Bytes >= 1024 Then
SetBytes = Round(FormatNumber(Bytes / 1024, 2), 0) & " KB"
ElseIf Bytes < 1024 Then
SetBytes = Bytes & " Bytes"
Else
SetBytes = "0 Bytes"
End If


Wscript.Echo "OUTPUT = " & SetBytes


Next