Showing posts with label vbscript. Show all posts
Showing posts with label vbscript. Show all posts

Sunday, October 21, 2012

Upgrade vSphere 4.1 to 5.1 – VMFS Upgrade

This will be the final post on upgrading vSphere 4.1 to 5.1.  In this post, I will be upgrade my datastore from VMFS-3 to VMFS-5.

Before rushing to upgrade from VMFS-3 to VMFS-5, I recommend reading the following tech paper to have an understand of what you will be getting from the upgrade.

http://www.vmware.com/files/pdf/techpaper/VMFS-5_Upgrade_Considerations.pdf

The upgrade can be done online and it is non-disruptive.  There is no need to shutdown virtual machines having virtual disks on the datastore to be upgraded.  However, it is always recommended to backup the virtual machines before the upgrade.

To upgrade the datastore from VMFS-3 to VMFS-5, all the ESXi hosts accessing the datastore must be ESXi 5.0 and above.  Select the datastore to be upgraded and click on the Upgrade to VMFS-5 link.

Wednesday, April 25, 2012

VBScript to extract and document AD Site Information

I have written a vbscript to help me to extract my AD Site Information with hundreds of Sites, Subnets, Domain Controller Servers and Site Links.  The extracted information will be populated into an Excel spreadsheet on the fly.

Here is a sample of the Excel spreadsheet.

AD Site Information

Friday, December 30, 2011

VBScript–Unable to delete computer account

I discovered this morning that a vbscript use to delete inactive computer accounts is not working.  Below is the script snippet that does the deletion.

Set objContainer = GetObject("LDAP://OU=InactiveComps,OU=Clients,DC=deInfoTech,DC=Org)

Do Until objRecordSet.EOF
    strComputerDN = objRecordSet.Fields("distinguishedName") 
    Set objComputer = GetObject("LDAP://" & strComputerDN)
    objContainer.Delete "computer", "cn=" & objComputer.cn 
    objRecordSet.MoveNext
Loop

Below is the error message that I received when trying to run the script.

Thursday, December 01, 2011

Windows Scheduled Tasks Inventory

Here is another script which goes through each my Windows Servers and list out scheduled tasks and the credentials use to run them.  I needed to know what are the scheduled tasks running on each server and most importantly what accounts are used to run them.

image

Initially, I was looking at using the Win32_ScheduledJob WMI class.  However, it does not work well for me because it only represents a job/task created with the AT command.  Job/Task created with the Scheduled Task Wizard from the Control Panel cannot be found in this class.  Check http://msdn.microsoft.com/en-us/library/windows/desktop/aa394399(v=vs.85).aspx for more information.
So with some searching, I found this script http://msdn.microsoft.com/en-us/library/windows/desktop/aa446865%28v=vs.85%29.aspx which display task name and status.  I modified the script to get what I needed .
On Error Resume Next
Dim rootFolder
Dim taskCollection
Dim numberOfTasks
Dim taskDefinition
Dim principal
Dim registeredTask

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objServerFile = objFSO.OpenTextFile("servers.txt", ForReading)
strFileName = "ScheduleTasks.txt"
Set objFileResult = objFSO.OpenTextFile(strFileName, 8, True, 0)
objFileResult.WriteLine "Server Name" + vbTab + "Service Account" + vbTab + "Task Name"

Do Until objServerFile.AtEndOfStream
    strServerName = objServerFile.Readline
    ' Create the TaskService object.
    Set service = CreateObject("Schedule.Service")
    call service.Connect(strServerName)
    Wscript.echo "Connecting to " & strServerName
    'Catch the error connecting to the server
    If Err.Number <> 0 Then
        Wscript.echo strServerName + vbTab + "Error Connecting" + vbTab + Err.Description
        objFileResult.WriteLine strServerName + vbTab + "Error Connecting" + vbTab + Err.Description
        Err.Clear
    Else
        ' Get the task folder that contains the tasks.
        Set rootFolder = service.GetFolder("\")
        Set taskCollection = rootFolder.GetTasks(0)
        numberOfTasks = taskCollection.Count
        If numberOfTasks = 0 Then
            Wscript.Echo "No tasks are registered."
                objFileResult.WriteLine strServerName + vbTab + "None" + vbTab + "No Task"
        Else
            WScript.Echo "Number of tasks registered: " & numberOfTasks
   
                'Loop trhough the task collection to get the task name and account used to run the task
            For Each registeredTask In taskCollection           
                Set taskDefinition = registeredTask.Definition
                 Set principal = taskDefinition.Principal
            WScript.Echo "Task Name: " & registeredTask.Name
            Wscript.Echo "User Name: " & principal.UserId
                objFileResult.WriteLine strServerName + vbTab + principal.UserId + vbTab + registeredTask.Name          
                Next
        End If   
    End If
Loop
'Clean up
objFileResult.Close
objServerFile.Close
 
The script will read from a file servers.txt to get the list of servers you want to go through.
Example of servers.txt:
servers.txt
deServer1
deServer2
 
Open a command prompt with an account that has administrative rights over the servers that you want to go through.  Run the script using cscript.exe.
C:\scripts>cscript ScheduleTasksInventory.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Connecting to deServer1
Number of tasks registered: 3
Task Name: At1
User Name:
Task Name: WebTest
User Name: deInfoTech\WebAdmin
Task Name: Note
User Name: SYSTEM
Connecting to deServer2
Number of tasks registered: 1
Task Name: movetoBackup
User Name: SYSTEM
 
The results will be written to ScheduleTasks.txt which is tab delimited.  Below is a sample of the results opened in Excel.
 
Server Name Service Account Task Name
deServer1   At1
deServer1 deInfoTech\WebAdmin WebTest
deServer1 SYSTEM Note
deServer2 SYSTEM movetoBackup
 
Using the Schedule.Service object, it does list out the task created using AT Command but the account used to run the task is missing.  Nevertheless, this does not seem to be a problem because it looks like tasks created using AT Command are running using the NT Authority\System account.  Again, this has saved me the hassle and time needed to go through each server manually.

Monday, November 21, 2011

Windows Service Accounts Inventory

You have a list of service account names and a list of server names.  You need to know which service account is used in which server.  You can logon to each server and go through the Services MMC if there are not too many servers and service accounts.  However, if you have too many servers or service accounts to check, the following vbscript might be of a little help.

You can download a copy of this vbscript from http://www.mediafire.com/?jfh5w4774w6ayru.

On Error Resume Next

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Open the servers.txt containing the server names for reading
Set objServersFile = objFSO.OpenTextFile("servers.txt", ForReading)

'The results will be written to SvcAcctServers.txt
strFileName = "SvcAcctServers.txt"
Set objResultFile = objFSO.OpenTextFile(strFileName, 8, True, 0)
objResultFile.WriteLine "Server Name" + vbTab + "Service Account" + vbTab + "Service Name"

'Loop through all the server names in the servers.txt file
Do Until objServersFile.AtEndOfStream
    strServerName = objServersFile.Readline

   
    Wscript.Echo "Connecting to " + strServerName
   
    'For each server, connect to the \root\cimv2 WMI namespace
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strServerName & "\root\cimv2")

    'Catch the error connecting to the WMI namespace
    If Err.Number <> 0 Then
        objResultFile.WriteLine strServerName + vbTab + "Error Connecting" + vbTab + "Error"
        Err.Clear
    Else
        'Open the services.txt file containing all the service accounts for reading
        Set objServiceAcctsFile = objFSO.OpenTextFile("services.txt", ForReading)
        Wscript.Echo "Checking " + strServerName + "..."

        'Loop through the services.txt file
        Do Until objServiceAcctsFile.AtEndOfStream
            strServiceName = objServiceAcctsFile.Readline
           
            'Get all the Windows services on the server by quering the win32_Service class
            Set colServices = objWMIService.ExecQuery _
                ("Select * from win32_Service")

            'Loop through all the Windows services
            For each objService in colServices
                'If the service account name of the Windows Service matched the service account name in the services.txt
                If InStr(1,objService.StartName, strServiceName , 1) > 0 Then
                        objResultFile.WriteLine strServerName + vbTab + strServiceName + vbTab + objService.Name
                End If
            Next       
        Loop
    End If
    'Clean up
    objServiceAcctsFile.Close
Loop
'Clean up
objResultFile.Close
objServersFile.Close

You need to two input files to run this script.  The first file is servers.txt which contains the name of all your servers.  The second file is services.txt which contains the name of all the service accounts.

Example of servers.txt and services.txt:

servers.txt services.txt
deServer1
deServer2
deServer3
deServer4
deServer5
deServer6
deServer7
deServer8
ArcSvc
AppsSvc
BackupSvc
MSSQLSvc
SOClusterSvc
SOMSSQLSvc
SFClusterSvc
SFMSSQLSvc

From the command prompt, run the vbscript using cscript.exe.  Make sure that the command prompt is open using an account that has administrative access to the server because the script impersonate the account to connect to the server.

C:\scripts\Services>cscript ServiceAcctsInventory.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Connecting to deServer1
Checking deServer1...
Connecting to deServer2
Checking deServer2...
Connecting to deServer3
Checking deServer3...
Connecting to deServer4
Checking deServer4...
Connecting to deServer5
Connecting to deServer6
Checking deServer6...
Connecting to deServer7
Checking deServer7...
Connecting to deServer8
Checking deServer8...

C:\scripts\Services>

The results will be written to SvcAcctServers.txt which is tab delimited.  Below is a sample of the results opened in Excel.

Server Name Service Account Service Name
deServer1 ArcSvc AMS
deServer1 ArcSvc MMS
deServer1 ArcSvc StorageNode
deServer3 MSSQLSvc MSSQLSERVER
deServer3 MSSQLSvc SQLSERVERAGENT
deServer3 SFClusterSvc ClusSvc
deServer3 SFMSSQLSvc MSSQLSERVER
deServer3 SFMSSQLSvc SQLSERVERAGENT
deServer5 Error Connecting Error
deServer7 BackupSvc BackupExecAgentBrowser
deServer7 BackupSvc BackupExecDeviceMediaService
deServer7 BackupSvc BackupExecJobEngine
deServer7 BackupSvc BackupExecManagementService
deServer7 BackupSvc BackupExecRPCService

deserver2, deserver4, deserver6 and deserver8 do not use any of the service accounts to run its Windows Services so they do not appear in the results.  There is an error connecting to deServer5 and it is most likely caused by permission issues, WMI service not working or non Windows systems.

Thursday, August 25, 2011

Errors when using vbscript to retrieve mailbox size information

One of my colleague was having problem running a vbscript which uses the WMI root\microsoftexchangev2 namespace and Exchange_Mailbox class to generate a mailbox size report for all the mailboxes on an Exchange 2003 server.

The script will create a log file to log the activities and a text file to store the result.  At first she double clicked on the vbs file to execute the script in the Exchange server itself.  So the script get handled by the Windows-based script host (Wscript.exe).  She waited for more than 10 minutes but the log file and text file remains empty except for some headers.  So she double clicked on the vbs file again to retry and this time she received an error message (Code: 800A0046).

image

This error was quite misleading and she thought that she lacked some permissions to connect to the Exchange server.  However, it was actually the locking of the log and text files that was causing the error 800A0046.  The first execution of the script which is handled by wscript.exe encountered an error but thanks to the “On Error Resume Next”, it went into an infinite loop causing the log and text files to be locked.  Checking the Windows Task Manager revealed that wscript.exe is still running.

image

I terminated the wscript.exe process and comment out the “On Error Resume Next” line.  I advised her to re-run the script using command-based script host (CScript.exe).  Re-run of the script showed a different error with an error code of 0x80041013.

image

Reviewing the script showed that it failed to create an instant of the Exchange_Mailbox class.  A quick check on the Exchange services, I noticed that the “Microsoft Exchange Management” service was stopped.

image

Starting the “Microsoft Exchange Management” service resolved the issue and she was able to generate the mailbox size report.

Tuesday, December 14, 2010

Getting Manager and Direct Reports of an AD Account

If you ever needs to retrieve the manager and direct reports of an AD account, this vbscript snippet might come in handy.

Set objUser = GetObject(LDAP://cn=Bill,ou=Users,dc=deinfo,dc=net)

WScript.Echo "Title: " & objUser.title
WScript.Echo "Department: " & objUser.department
WScript.Echo "Company: " & objUser.company
WScript.Echo "Manager: " & objUser.manager
‘ this get the manager of the AD account

‘ directReports is multi-value so you need to loop through it to get all the values out
For Each strValue in objUser.directReports
WScript.Echo "Direct Reports: " & strValue
Next

Of course if you need to retrieve these information for a lot of accounts (e.g. all accounts within an OU or domain), you will need to query the OU or domain to and loop through all the accounts. I guess you get the idea.