none
Need help to correct the VB Script RRS feed

  • Question

  • Hi ,

    I am not good at script at all.But somehow looking to portal I have written a script which actually need to collect  Server name,Domain name,last Bootup time,eventlog service status,C drive free space,Server is in domain/workgroup from 10000+ servers and the output report should save it in CSV format. Can anyone please help me to correct the script, so that it will collect the above said data.

    Option Explicit
     
    Const PATH_TO_INPUT = "servers.txt"
    Const PATH_TO_OUTPUT = "output.csv"
     
    Dim fso
    Set fso = WScript.CreateObject("Scripting.FileSystemObject")
     
    Dim shl
    Set shl = WScript.CreateObject("WScript.Shell")
     
    Dim input
    Set input = fso.OpenTextFile(PATH_TO_INPUT)
     
    Dim output
    Set output = fso.CreateTextFile(PATH_TO_OUTPUT, True)
     
    output.WriteLine "Host name,Domain,DeviceID,Free space,service,service state,LastBootUpTime"

    For Each strComputer In arrComputers
       WScript.Echo
       WScript.Echo "=========================================="
       WScript.Echo "Computer: " & strComputer
       WScript.Echo "=========================================="

       Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
       Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _
                                              wbemFlagReturnImmediately + wbemFlagForwardOnly)
       Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk", "WQL", _
                                              wbemFlagReturnImmediately + wbemFlagForwardOnly)
       Set colItems = objWMIService.ExecQuery( "Select * from Win32_Service Where Name = 'eventlog' " )
       Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _
                                              wbemFlagReturnImmediately + wbemFlagForwardOnly)

       For Each objItem In colItems
          WScript.Echo "Domain: " & objItem.Domain
          WScript.Echo "PartOfDomain: " & objItem.PartOfDomain
          WScript.Echo "DeviceID: " & objItem.DeviceID
          WScript.Echo "FreeSpace: " & objItem.FreeSpace
          WScript.Echo "DisplayName: " & objItem.DisplayName
          WScript.Echo "State: " & objItem.State
          WScript.Echo "LastBootUpTime: " & WMIDateStringToDate(objItem.LastBootUpTime)
          WScript.Echo
       Next
    Next


    Function WMIDateStringToDate(dtmDate)
    WScript.Echo dtm: 
    WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
    Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
    & " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
    End Function

    Appreciate any help you can provide.

    • Moved by Bill_Stewart Monday, November 23, 2015 10:28 PM This is not "scripts on demand"
    Tuesday, September 8, 2015 6:16 PM

All replies

  • Hi,

    Can anyone help me with a validation script (VBscript) with below details.

    Requirement : We need the script to validate the 10000+ prod server before and after patching in our environment.

    The script should collect below details and the report should save as CSV.

    Server name

    Domain name

    Server up-time

    Oracle 12c agent service status

    C drive free space

    Server is in domain/workgroup

    Can any one help me with this please?

    Any help is much appreciated.

    Regards

    Jo



    • Edited by Jonyee Tuesday, September 8, 2015 11:46 AM
    • Merged by Bill_Stewart Wednesday, September 9, 2015 5:44 PM Duplicate
    Tuesday, September 8, 2015 11:38 AM
  • Hi Jonyee, the script needs to be requested in script center

    https://gallery.technet.microsoft.com/scriptcenter/site/requests

    Sample code - Change it as required or make use of the above link and this is in PowerShell

    $servers = Get-Content C:\Temp\Server.txt 
    $Coll = @()
    foreach($server in $servers)
    {
        $Info = Get-WmiObject win32_operatingsystem -ComputerName $server | select csname, @{LABEL='LastBootUpTime';EXPRESSION={(Get-Date) - ($_.ConverttoDateTime($_.lastbootuptime))}}
        $Disk = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='C:'" -ComputerName $server | Select @{Label='FreeSpace';Expression = {[Math]::Round(($_.FreeSpace / 1GB))}}
        $Group = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $server | Select *
        $service = Get-WmiObject -Class Win32_Service -Filter "Name='Winrm'" -ComputerName $server 
        $Object = New-Object psobject -Property @{
        "Uptimeindays" = $Info.LastBootUpTime.Days
        "ServerName" = $Info.csname
        "FreeSpace(GB)" = $Disk.FreeSpace
        "WorkGroup" = $Group.PartOfDomain
        "WinRMService" = $service.State
        }
        $coll += $Object
        #$Object
        
    }
    $Coll | Export-Csv C:\Temp\Information.csv -NoTypeInformation


    Regards Chen V [MCTS SharePoint 2010]



    • Edited by Chen VMVP Tuesday, September 8, 2015 12:28 PM
    • Proposed as answer by Mike Laughlin Tuesday, September 8, 2015 12:54 PM
    Tuesday, September 8, 2015 12:06 PM
  • Hi Chen,

    Thanks for the reply.

    I have already requested script in above link too.

    Tuesday, September 8, 2015 12:24 PM
  • Scrap the VBScript and use this as an excuse to learn PowerShell:

    https://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx

    Read this first to set your expectations of what you can expect from this forum, even if you disregard my first suggestion:

    https://social.technet.microsoft.com/Forums/scriptcenter/en-US/a0def745-4831-4de0-a040-63b63e7be7ae/posting-guidelines?forum=ITCG

    Check here for a starting point/examples:

    https://gallery.technet.microsoft.com/scriptcenter

    Let us know if you have any specific questions.


    Tuesday, September 8, 2015 6:21 PM
  • Hey Jony,

    I'd recommend you to move to get this report done by powershell. Think about it.


    Cheers, Alan.

    Tuesday, September 8, 2015 6:52 PM
  • The repository has many reports almost identical to this request.


    \_(ツ)_/

    Tuesday, September 8, 2015 7:02 PM
  • you required VBscript only if you are using mixed OS. Otherwise you can choose Powershell. from the request I hope this is for mixed environment
    Wednesday, September 9, 2015 4:51 AM
  • you required VBscript only if you are using mixed OS. Otherwise you can choose Powershell. from the request I hope this is for mixed environment

    This is slightly misleading, Windows Powershell can be installed on server 2003, XP operating systems to support powershell scripts.

    I see no reason that mixed operating systems warrant using vbscript unless we're talking about earlier operating systems :|


    Cheers

    Kriss Milne | MCSE | http://blog.krissmilne.tech | Twitter | LinkedIn

    Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

    Wednesday, September 9, 2015 4:55 AM
  • Hi Jonyee

    The primary purpose of these forums to assist the community with specific technical problems, advice and assistance not really to provide complete solutions, or write full scripts.

    There are numerous technical blogs, script repositories etc... that provide solutions to your query.

    As JRV stated your best place to start would be the Microsoft script gallery:
    Script Gallery 

    As also suggested, consider using Powershell instead of VBScript as it has significantly more flexibility and in my opinion a lower learning curve


    Cheers

    Kriss Milne | MCSE | http://blog.krissmilne.tech | Twitter | LinkedIn

    Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

    Wednesday, September 9, 2015 5:02 AM
  • Hi Joynee, 

    I have tried editing the script for you. It may require some more editing for running successfully.

    Option Explicit

    Const wbemFlagReturnImmediately = &h10
    Const wbemFlagForwardOnly = &h20

    Const PATH_TO_INPUT = "Machines.txt"
    Const PATH_TO_OUTPUT = "Serverinformation.csv"

    Dim fso
    Set fso = WScript.CreateObject("Scripting.FileSystemObject")
     
    Dim shl
    Set shl = WScript.CreateObject("WScript.Shell")
     
    Dim input
    Set input = fso.OpenTextFile(PATH_TO_INPUT)
     
    Dim output
    Set output = fso.CreateTextFile(PATH_TO_OUTPUT, True)
     
    output.WriteLine "Hostname,domain,PartOfDomain,operatingSystem,LastBootUpTime,DisplayName,State,Disk Drives"

    Dim wmiService
    Dim wmiResults
     
    Dim Hostname
    Dim Domain
    Dim PartOfDomain
    Dim operatingSystem
    Dim LastBootUpTime
    Dim DisplayName
    Dim State
    Dim drives
     
    Dim line
    Dim exec
    Dim pingResults 
    While Not input.AtEndOfStream
        line = input.ReadLine
        Hostname = ""
        Domain = ""
        PartOfDomain = ""
        operatingSystem = ""
        LastBootUpTime = ""
        DisplayName = ""
        State = ""
        drives = ""
         
        Set exec = shl.Exec("ping -n 2 -w 1000 " & line)
        pingResults = LCase(exec.StdOut.ReadAll)
         
        If InStr(pingResults, "reply from") Then
            WScript.Echo "Reply From: " & line 
            On Error Resume Next
             
            Set wmiService = GetObject("winmgmts:\\" & line & "\root\CIMV2")
         
            If Not Err.Number = 0 Then
                output.WriteLine line & ",Error: " & Err.Description
                WScript.Echo line & ",Error: " & Err.Description
                On Error GoTo 0
            Else
                On Error GoTo 0
                Hostname = line
     
                       
                Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
                
                 Dim item
                For Each item In wmiResults
                    Domain = Trim(item.Domain)
                    PartOfDomain = Trim(item.PartOfDomain)
                Next
                 
                Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
                 
                For Each item In wmiResults
                    operatingSystem = Trim(item.Name)
                    operatingSystem = Split(operatingSystem, "|")(0)
                    LastBootUpTime = Trim(item.LastBootUpTime)
                 Next
         
                Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_Service Where Name = 'Oracle12cAgent'")
         
                For Each item In wmiResults
                    DisplayName = Trim(item.DisplayName)
                    State = Trim(item.State)
                Next
                 
                Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType=3", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
         
                For Each item In wmiResults
                    drives = drives & Trim(item.DeviceID) & " " & Round(Trim(item.Size) / (1024^2), 2) & ";"
                Next
                output.WriteLine Hostname & "," & Domain & "," & PartOfDomain & "," & operatingSystem & "," & LastBootUpTimee & "," & DisplayName & "," & State & "," & drives
                WScript.Echo Hostname & "," & Domain & "," & PartOfDomain & "," & operatingSystem & "," & LastBootUpTime & "," & DisplayName & "," & State & "," & drives 
            End If
        Else
            output.WriteLine line & ",No Response"
            WScript.Echo line & ",No Response"
        End If
    Wend 
     
    output.Close
    input.Close
     
    Set wmiService = Nothing
    Set wmiresults = Nothing

    Thursday, September 10, 2015 6:38 AM