Pages

Tuesday, July 26, 2011

How to start Windows Service on Remote Computer using VB.NET

Dim co As ConnectionOptions = New ConnectionOptions()
'CREDENTIALS
With co

 .Username = "username" 
 .Password = "password"
End With
Dim strComputer As String = "computername"

Dim scope As New System.Management.ManagementScope
scope=New System.Management.ManagementScope("\\" & strComputer & "\root\cimv2", co)
'CONNECT TO THE BOX
scope.Connect()
Dim query As ObjectQuery = New ObjectQuery("Select * from Win32_Service Where Name ='SERVICENAME' ")
Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher(scope, query)
Dim observer As ManagementOperationObserver = New ManagementOperationObserver()
Dim queryCollection As ManagementObjectCollection=searcher.Get()Dim m As ManagementObject
Dim args As Object
'START THE SERVICE

For Each m In queryCollection
 m.InvokeMethod("StartService", args)
Next

Monday, July 18, 2011

WMI Script to get BIOS details

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_BIOS",,48)
For Each objItem in colItems
  Msgbox "Win32_BIOS instance"
   If isNull(objItem.BIOSVersion) Then
     Msgbox "BIOSVersion: "
   Else
     Msgbox "BIOSVersion: " & Join(objItem.BIOSVersion, ",")
   End If
  Msgbox "Caption: " & objItem.Caption
  Msgbox "Description: " & objItem.Description
  Msgbox "InstallDate: " & objItem.InstallDate
  Msgbox "Manufacturer: " & objItem.Manufacturer
  Msgbox "Name: " & objItem.Name
Next

Wednesday, July 6, 2011

How to display Enabled Network Adapter and their IP Address in .NET using WMI

Imports System.Management

Dim search As New ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled=True")
Dim info As ManagementObject
Dim strIPAddress As String = Nothing
For Each info In search.Get     

If Not (info("IPAddress")) Is Nothing Then
       Dim i As Integer
       With GrdView
         .Rows.Add()
         i = GrdView.Rows.Count - 1
         Rows(i).Cells(1).Value = info("Description").ToString
       End With



'GET THE IP ADDRESS FOR SPECIFIC NETWORK ADAPTER
Dim sql As String = "SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled=True
And Description='" & info("Description").ToString & "'"
  Dim searchIP As New ManagementObjectSearcher(sql)

  Dim infoIP As ManagementObject
  For Each infoIP In searchIP.Get
    strIPAddress = Join(infoIP("IPAddress"), ",")
    strIPAddress = Mid(strIPAddress, 1, InStr(strIPAddress, ",") - 1)
    GrdView.Rows(i).Cells(2).Value = strIPAddress
  Next

End If
Next

Tuesday, July 5, 2011

WMI Script to get details about local Computer System

Below script will give you the computer system details of a remote computer

strComputer = "FullComputerName"
strDomain = "DOMAIN"

strUser = "username"
strPassword = "password"

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _
    "root\CIMV2", _
    strUser, _
    strPassword, _
    "MS_409", _
    "ntlmdomain:" + strDomain)

Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_ComputerSystem",,48)

For Each objItem in colItems
    Wscript.Echo "Win32_ComputerSystem instance"
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "Domain: " & objItem.Domain
    Wscript.Echo "InstallDate: " & objItem.InstallDate
    Wscript.Echo "Manufacturer: " & objItem.Manufacturer
    Wscript.Echo "Model: " & objItem.Model
Next

WMI Script to get details about local Computer System

Below script will give you details about local computer system using WMI.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_ComputerSystem",,48)

For Each objItem in colItems
    Wscript.Echo "Win32_ComputerSystem instance"
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "Domain: " & objItem.Domain
    Wscript.Echo "Manufacturer: " & objItem.Manufacturer
    Wscript.Echo "Model: " & objItem.Model
Next

Monday, July 4, 2011

.NET function to get user groups from Active Directory

Below .NET function is used to get the user groups from Active Directory.

To use the below function in your project, you need to add the reference for System.DirectoryServices


Public Function GetUserGroupMembership(ByVal strUser As String) As StringCollection  Dim groups As New StringCollection()
Try
  Dim obEntry As New DirectoryServices.DirectoryEntry("
LDAP://domain/DC=domain,DC=com")
  Dim srch As New DirectoryServices.DirectorySearcher(obEntry, "(sAMAccountName=" & strUser & ")")
  Dim res As DirectoryServices.SearchResult = srch.FindOne()
      If res IsNot Nothing Then
        Dim obUser As New DirectoryServices.DirectoryEntry(res.Path)
        ' Invoke Groups method.
        Dim obGroups As Object = obUser.Invoke("Groups")
        For Each ob As Object In DirectCast(obGroups, IEnumerable)
        ' Create object for each group.
        Dim obGpEntry As New DirectoryServices.DirectoryEntry(ob)
                    groups.Add(obGpEntry.Name)
        Next
      End If
Catch ex As Exception            'Trace.Write(ex.Message)
End Try
        Return groups
End Function



Above function can be called as :
Dim Groups As StringCollection = GetUserGroupMembership(username.text)

.NET function to get user information from Active Directory

Below script is a .NET function to get information about a user from AD. This information can be stored in a variable and used in your project/application.

To use the below function in your project, you need to add the reference for System.DirectoryServices


Public Function GetUserInfo(ByVal inAD As String, ByVal inType As String) As String
Try
    Dim sPath As String= "
LDAP://domain/DC=domain,DC=com"
    Dim SamAccount As String = Right(inAD, Len(inAD) - InStr(inAD, "\"))
    Dim myDirectory As New DirectoryServices.DirectoryEntry(sPath, "username", "Password")
    Dim mySearcher As New DirectoryServices.DirectorySearcher(myDirectory)
    Dim mySearchResultColl As DirectoryServices.SearchResultCollection
    Dim mySearchResult As DirectoryServices.SearchResult
    Dim myResultPropColl As DirectoryServices.ResultPropertyCollection
    Dim myResultPropValueColl As DirectoryServices.ResultPropertyValueCollection
    'Build LDAP query
    mySearcher.Filter = ("(&(objectClass=user)(samaccountname=" & SamAccount & "))")
    mySearchResultColl = mySearcher.FindAll()
    'only one user from search result
            Select Case mySearchResultColl.Count
                Case 0
                    Return "0"
                    Exit Function
                Case Is > 1
                    Return "1"
                    Exit Function
            End Select

            'Search result from the collection
            mySearchResult = mySearchResultColl.Item(0)
           
     'Properites, they contain the usefull info
            myResultPropColl = mySearchResult.Properties
            myResultPropValueColl = myResultPropColl.Item(inType)
            Return CStr(myResultPropValueColl.Item(0))


Catch ex As System.Exception           
    Return 0
End Try
End Function

Above function can be called as below:
Variablename = GetUserInfo(usernametexbox.text, "displayname")
where displayname is the property name in Active Directory

Friday, July 1, 2011

How to PING a specific website and send email, if site is down

This little Vbscript allows you to ping a site and send email, if the site is down

Dim strWebsite
strWebsite = "sitename.com"
If PingSite( strWebsite ) Then
    
Else
     Set myMail=CreateObject("CDO.Message")
            myMail.Subject="SITE is Down"
            myMail.From = fromemailaddress                
            myMail.To = toemailaddress
            myMail.HTMLBody= strWebsite
            myMail.Send
            set myMail=nothing
End If


Function PingSite( myWebsite )    Dim siteStatus, objHTTP
    Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
    objHTTP.Open "GET", "http://" & myWebsite & "/", False
    objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)"

    On Error Resume Next
    objHTTP.Send
    siteStatus = objHTTP.Status

    On Error Goto 0
    If siteStatus = 200 Then
        PingSite = True
    Else
        PingSite = False
    End If

    Set objHTTP = Nothing
End Function