• Tag Archives Scripting
  • Script to delete Chrome cache on all PC’s listed in a text file

    Another problem that could be fixed with a script today. Moving machines and users to a different domain resulted in a problem for some people. Chrome is a very popular browser but a number of people have reported to support that Chrome takes a very long time to start and then open the first page after log in to windows.

    It was determined that deleting the cache directory from within the users Chrome application data folder resolved this.

    Waiting for every effected user to report the problem wouldn’t be a great course of action of course. It would be a lot better to proactively go after all Chrome installations and remove the cache directory.

    So that’s what I’ve done. The script does the following:

    1. Check to see if the computer is on the network by pinging it.
    2. List all the user directories in c:\users on each computer.
    3. Check to see if the Chrome cache directory exists.
    4. Deletes the cache directory.
    5. Logs the result including any encountered errors to a file called results.txt.

    I could have searched Active Directory directly of course but I want to be able to easily add and remove computers when needed. It also allows me to run the script on 10 computers to start off and ramp up to a few thousand by the end.

    It should go without saying that as the script uses the admin share, you need to have access to the workstations that you intend to administer.

    Option Explicit
    Const ForReading = 1
    Const ForAppending = 8
    Dim objFSO : Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    Dim objFile : Set objFile = objFSO.OpenTextFile(“Z:\Scripts\Chrome cleanup\AccountingsectionComputers.txt”, ForReading, False)
    Dim objWriteFile : Set objWriteFile = objFSO.OpenTextFile(“Z:\Scripts\Chrome cleanup\Result.txt”, ForAppending)

    Do Until objFile.AtEndOfStream
    Dim strComputer : strComputer = Trim(objFile.ReadLine)
    If PingMachine(strComputer) Then
    ‘Computer is pinging
    Dim objFolder : Set objFolder = objFSO.GetFolder(“\\” & strComputer & “\c$\users\”)
    Dim strUserParentPath : Set strUserParentPath = objFolder.SubFolders
    Dim strUserFolder
    For Each strUserFolder in strUserParentPath
    Dim strRemoteBasePath : strRemoteBasePath = “\\” & strComputer & “\c$\users\” & strUserFolder.name & “\AppData\Local\Google\Chrome\User Data\Default\Cache”
    If objFSO.FolderExists(strRemoteBasePath) Then
    On Error Resume Next
    objFSO.DeleteFolder strRemoteBasePath, True
    If Err.Number = 0 Then
    objWriteFile.WriteLine strRemoteBasePath & vbTab & ” DELETE SUCCESSFUL”
    Else
    objWriteFile.WriteLine strRemoteBasePath & vbTab & ” DELETE FAILED: ” & Err.Number & ” ” & Err.Description
    End If
    On Error GoTo 0
    Else
    objWriteFile.WriteLine strRemoteBasePath & vbTab & ” FOLDER DOES NOT EXIST”
    End If
    Next
    Else
    objWriteFile.WriteLine strComputer & vbTab & ” PING FAILED”
    End If
    Loop
    objFile.Close

    Function PingMachine(device_name)
    PingMachine = False
    Dim colItems : Set colItems = GetObject(“winmgmts:root\cimv2”).ExecQuery _
    (“Select StatusCode from Win32_PingStatus Where Address = ‘” & device_name & “‘”)
    Dim objItem
    For Each objItem in colItems
    If objItem.StatusCode = 0 Then PingMachine = True
    Next
    End Function

    If there are other scripts that you would like to see, please let me know in the comments section.


  • See who is logging onto your domain

    I was looking a migration from one domain to another a short while ago. Almost all the users accounts had been migrated over but there were a few hanging on.

    The migration tool wasn’t doing a great job of picking them up so I wrote a small script and added it to group policy for all servers and workstations. Now when someone logs in to the old domain a line is written to the log with their username, computer name and IP address.

    Using this the person responsible for the migration can get through the last few people on the old domain. This has saved a lot of time as before I wrote this th eprocess was very manual.


    Const ForAppending = 8
    Set WshShell = CreateObject("WScript.Shell")
    Set WshNetwork = CreateObject("WScript.Network")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("\\ServerName.DomainName.TLD\RemainingComputer\log.txt", ForAppending)
    objFile.WriteLine WshNetwork.ComputerName & "," & WshNetwork.UserName & "," & GetIpAddress
    objFile.Close

    Function GetIPAddress
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set IPConfigSet = objWMIService.ExecQuery _
    ("Select IPAddress from Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'")

    For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.IPAddress) Then
    For i = LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
    If Not Instr(IPConfig.IPAddress(i), ":") > 0 Then
    strIPAddress = strIPAddress & IPConfig.IPAddress(i) & " "
    End If
    Next
    End If
    Next
    GetIPAddress = strIPAddress
    End Function


  • VBScript to arrange files into folders.

    A few days ago I was presented with just over seven thousand PDF files. I had waited six weeks for these files so I was in a horrid mood when they arrived in one big batch with just ID’s in the file name. I had no way of processing them by customer.

    I had a spreadsheet that had the billing period, the customer name and the bil number in columns A, B and C respectively so I decided to write up a short script to process these files.

    Here’s a cut down version with a little bit removed to protect the various companies involved.


    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.Open ("D:\Home\Workarea\Invoices_PDF\invoices.xlsx")

    ' Starting at row 2 because row 1 is used for the column headings.
    intRow = 2
    strNewParentPath = "ByETB\"
    strOldParentPath = "Combined\"
    strBasePath = "D:\Home\Workarea\Invoices_PDF\"
    Do Until objExcel.Cells(intRow,1).Value = ""
    strPeriod = ConvertPeriod(objExcel.Cells(intRow, 1).Value)
    strProcessedCompanyName = ConvertCompanyToDirectoryName(objExcel.Cells(intRow, 2).Value)
    strFileName = "billno-" & objExcel.Cells(intRow, 3).Value & ".pdf"
    ' WScript.Echo "Moving " & strOldParentPath & strFileName & " To " & strNewParentPath & strProcessedCompanyName & "\" & strPeriod & "\"

    If NOT (FSO.FolderExists(strBasePath & strNewParentPath & strProcessedCompanyName)) Then
    WScript.echo "Folder " & strBasePath & strNewParentPath & strProcessedCompanyName & " didn't exist."
    FSO.CreateFolder strBasePath & strNewParentPath & strProcessedCompanyName
    End If

    If NOT (FSO.FolderExists(strBasePath & strNewParentPath & strProcessedCompanyName & "\" & strPeriod)) Then
    WScript.echo "Folder " & strBasePath & strNewParentPath & strProcessedCompanyName & "\" & strPeriod & " didn't exist."
    FSO.CreateFolder strBasePath & strNewParentPath & strProcessedCompanyName & "\" & strPeriod
    End If

    WScript.Echo "Copying from: " & strBasePath & strOldParentPath & strFileName & " Copying to: " & strBasePath & strNewParentPath & strProcessedCompanyName & "\" & strPeriod& "\"
    FSO.CopyFile strBasePath & strOldParentPath & strFileName, strBasePath & strNewParentPath & strProcessedCompanyName & "\" & strPeriod& "\"
    intRow = intRow + 1
    Loop
    objExcel.Quit

    Function ConvertCompanyToDirectoryName (CompanyName)
    if CompanyName = "" Then
    WScript.Echo "No ETB Name passed to ConvertCompanyToDirectoryName function"
    Exit Function
    End If

    If InStr(1, CompanyName, "DUBLIN") <> 0 Then
    CompanyName = "Dublin"
    End If

    If InStr(1, CompanyName, "LOUTH") <> 0 Then
    CompanyName = "Louth"
    End If

    If InStr(1, CompanyName, "DONEGAL") <> 0 Then
    CompanyName = "Donegal"
    End If

    If InStr(1, CompanyName, "LIMERICK") <> 0 Then
    CompanyName = "Limerick"
    End If

    If InStr(1, CompanyName, "GALWAY") <> 0 Then
    CompanyName = "Galway"
    End If

    If InStr(1, CompanyName, "CORK") <> 0 Then
    CompanyName = "Cork"
    End If

    If InStr(1, CompanyName, "CARLOW") <> 0 Then
    CompanyName = "Carlow"
    End If

    If InStr(1, CompanyName, "KERRY") <> 0 Then
    CompanyName = "Kerry"
    End If

    If InStr(1, CompanyName, "MEATH") <> 0 Then
    CompanyName = "WestMeath"
    End If

    If InStr(1, CompanyName, "KILDARE") <> 0 Then
    CompanyName = "Kildare"
    End If

    If InStr(1, CompanyName, "TIPPERARY") <> 0 Then
    CompanyName = "Tipperary"
    End If

    If InStr(1, CompanyName, "TIPP") <> 0 Then
    CompanyName = "Tipperary"
    End If

    If InStr(1, CompanyName, "LAOIS") <> 0 Then
    CompanyName = "Laois"
    End If

    If InStr(1, CompanyName, "MONAGHAN") <> 0 Then
    CompanyName = "Monaghan"
    End If

    ' Replace spaces with under lines.
    If InStr(1, CompanyName, " ") <> 0 Then
    CompanyName = Replace(CompanyName, " ", "_")
    End If

    ' Replaces / character with nothing.
    If InStr(1, CompanyName, "/") <> 0 Then
    CompanyName = Replace(CompanyName, "/", "")
    End If

    ConvertCompanyToDirectoryName = CompanyName
    End Function

    Function ConvertPeriod (Period)
    If Period = "" Then
    WScript.Echo "Period passed to ConvertPeriod function is blank."
    Exit Function
    end If

    If Period = "01/01/2016" Then
    Period = "Jan2016"
    End If

    If Period = "01/02/2016" Then
    Period = "Feb2016"
    End If

    If Period = "01/03/2016" Then
    Period = "Mar2016"
    End If

    If Period = "01/04/2016" Then
    Period = "Apr2016"
    End If
    ConvertPeriod = Period
    End Function