none
Replacing Text in Doc via VBScript w/o Error RRS feed

  • Question

  • Hi, all!  I have a script to replace text from an INS file, but I need it to not display an error AND end the wscript.exe process if it errors out, like when the path is not found on certain operating systems.  The script will be tied to a GPO to run at logon.

    Here is the script:

    Const ForReading = 1
    Const ForWriting = 2

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\Windows\System32\GroupPolicy\User\MICROSOFT\IEAK\install.ins", ForReading)

    Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        If InStr(strLine, "FavoritesDelete") = 0 Then
            strNewContents = strNewContents & strLine & vbCrLf
        End If
    Loop

    objFile.Close

    Set objFile = objFSO.OpenTextFile("C:\Windows\System32\GroupPolicy\User\MICROSOFT\IEAK\install.ins", ForWriting)
    objFile.Write strNewContents

    objFile.Close

    The way that it is, it works, but, again, errors out on some operating systems.  I tried On Error Resume Next, which hides the error message, but then pegs the wscript.exe process at 99%.  And the error message returns if I use the following code:

    If Err <> 0 Then
        Wscript.Quit
    End If

    Any help is appreciated.  Thanks!

    Brandon

    Thursday, January 9, 2014 9:59 PM

Answers

  • Ok. How about this:


    Const SystemFolder = 1
    Const ForReading = 1
    Const ForWriting = 2
    
    Dim FSO
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Dim FilePath
    FilePath = FSO.BuildPath(FSO.GetSpecialFolder(SystemFolder), "GroupPolicy\User\MICROSOFT\IEAK\install.ins")
    
    Dim Result, TS
    Result = OpenTextFile(FilePath, ForReading, TS)
    If Result <> 0 Then
      WScript.Quit Result
    End If
    
    Dim NewContents, Line
    NewContents = ""
    Do Until TS.AtEndOfStream
      Line = TS.ReadLine
      If InStr(1, Line, "FavoritesDelete", vbTextCompare) = 0 Then
         NewContents = NewContents & Line & vbNewLine
      End If
    Loop
    TS.Close
    
    Result = OpenTextFile(FilePath, ForWriting, TS)
    If Result <> 0 Then
      WScript.Quit Result
    End If
    
    TS.Write NewContents
    TS.Close
    
    Function OpenTextFile(ByVal FilePath, ByVal Mode, ByRef TS)
      On Error Resume Next
      Set TS = FSO.OpenTextFile(FilePath, Mode)
      OpenTextFile = Err.Number
    End Function
    

    Bill

    • Marked as answer by brandoncatz81 Tuesday, January 14, 2014 4:12 PM
    Friday, January 10, 2014 5:37 PM
    Moderator

All replies

  • What's the purpose of the script?

    Bill

    Thursday, January 9, 2014 11:31 PM
    Moderator
  • Any script will quit on any error.  Just run the script.

    If this is not what you are asking then you will need to study computers an how software based systems behave. When you understand that themnpost back with a real question.

    Understand that I am not trying to discourage you but that your question makes absolutely no sense as English or as a technical question.

    I recommend learning the basics of both scripting and of how computers work. This will lead you to being able to ask an understandable question.

    Sorry if you don't like this but you have to learn that you cannot do brain surgery with a hammer and chisel.


    ¯\_(ツ)_/¯

    Friday, January 10, 2014 3:13 AM
  • First off, you really should try using a bit of tact when attempting to communicate with other human beings, which I can see is not your forte.  Your asinine comment can only be taken in a very harsh and negative way.  "I don't understand what you are asking" was all that you needed to write, but instead you decided to make an attempt at belittling me in 4/5 paragraphs.  Not cool.

    I thought I made it fairly clear.  If my verbage isn't 100% accurate, my apologies.  I thought that this might be a place that wasn't exclusive to scripting snobs, but perhaps I was wrong.

    In any case, I simply want the aforementioned script to run silently in the background, regardless of success or failure.  Currently, if an error occurs (ie the referenced file/folder is not found) the user is shown an error message.  The "On Error Resume Next" helps, but then wscript.exe pegs to 99% as the script loops and stays there.  Again... I have a script (see original post)... I want no error message displayed if the script fails... and without any issues with the windows script host process.

    Friday, January 10, 2014 6:50 AM
  • Hi, Bill.  The purpose of the script is to find and delete a single line of text from a specific windows system file.  Specifically, it searches for a line containing "FavoritesDelete" in the install.ins file and removes it.  The workstation images at my work were created with a local policy enabled that will purge all IE favorites.  Since numerous systems have already been rolled out, they have asked that we handle disabling this feature via a group policy.  This seemed to be the best way without having to touch dozens and dozens of machines that have been deployed.  My apologies for not putting comments in the script.  My scripting skills are lackluster, at best, but I thought the Technet folks could assist, if not too much trouble.  Thanks for your time!
    Friday, January 10, 2014 6:59 AM
  • So you are asking how fo find a line in a file and remove that line.  Is that correct?

    Mu original comment still is true. THe fie will quit on any error and display the error. THat is the default behavior in VBScript.

    To disable this policy you need to change Group Policy.  We do not change GP at the workstation because it will not be effective.  Group Policy is centrally managed at the Domain Controller.  What you are trying to do at the workstation will likely not work reliably as domain GP will overwrite this.

    Group Policy has settings that manage this setting.

    You have been asked to do this via GP but are trying to use script.  Script is not GP.  GP is an extension to Active Directory and is managed using the Group Policy Management Console (GPMC).  GP is stored in Active Directory and written to the SYSVOL share on all DCs.

    The script as you have posted it will work as you have stated but it will not work on Unicode files.  I am pretty sure that all GP generated files are Unicode.

    The other issue you will have is that if you are using this script on workstations you are only setting the local policy which will not be run in a domain.  The domain policy will over write this which is why you were asked to use Group Policy to do this.

    You would do best by posting in the GP forum and allow them to help you understand how to use and set GP.

    Again I will point out that there is no substitute for learning the technology that you are working with.  If you are working with hundreds of machines a lack of correct knowledge can lead to some pretty costly disasters.  You have been asked to use GP to do this and you are trying to write a script.  This is not using GP to solve and enforce the rules.  It is an attempt to write a script to reverse a setting that has been set in the local policy in the deployment image.  Changing this will not be using GP to fix this and it will not dynamically enforce the rule that you have been asked to enforce.

    Here is how to manage IE with Group Policy: http://technet.microsoft.com/en-us/library/cc754047.aspx

    There is a single set of setting that control how the favorites are managed.  THis is compatible with all current versions of IE.

    http://technet.microsoft.com/en-us/library/cc772139.aspx

    Further questions on this should be posted to the GP forum.


    ¯\_(ツ)_/¯

    Friday, January 10, 2014 10:33 AM
  • One of the main purposes of Group Policy is so that you don't have to write scripts.

    Update your GPO to do the required settings, and all users affected by the policy will get the new settings.

    You can ask about Group Policy in the Group Policy forum.

    Bill

    Friday, January 10, 2014 3:26 PM
    Moderator
  • I suppose I need to go back to the beginning with this, as I have done the research and testing to know that this particular setting can not be disabled via GP if it was not enabled via GP.  IEAK contains the setting "Delete existing favorites and links, if present".  If this setting was turned on locally, the GP will not help.  It can either enable the setting or not enable the setting (not configured), but it will not disable it.  Hence the workaround, which took me some time to track down.  Considering the numerous posts on this issue across the net, it is safe to say that it is a fairly common issue, but not too many contain a valid fix.  Below you will find my original post in the GP forum where I posed this question and where I was lead to modifying the INS file as a fix.  I was just trying to streamline the process by using VB.

    In regards to the unicode file, the script worked on a couple of test systems, for whatever that is worth.

    This leads me back to my original question...

    My original GP post - Darn... can't post links as I am not "verified".  Anyway, look at my account on here and I am sure you will be see my one other post.

    Friday, January 10, 2014 4:18 PM
  • Thanks for the reply.  Please see my last response to JRV.

    Friday, January 10, 2014 4:21 PM
  • Hi,

     Darn... can't post links as I am not "verified". 

    The current verification thread is here:

    http://social.technet.microsoft.com/Forums/en-US/200c33c9-abe9-494a-953a-bf53fccb3c76/verify-your-account-11?forum=reportabug

    Post in there and an administrator will set your account as trusted in a few hours or so.


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Friday, January 10, 2014 4:40 PM
  • Thanks!
    Friday, January 10, 2014 5:10 PM
  • Ok. How about this:


    Const SystemFolder = 1
    Const ForReading = 1
    Const ForWriting = 2
    
    Dim FSO
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Dim FilePath
    FilePath = FSO.BuildPath(FSO.GetSpecialFolder(SystemFolder), "GroupPolicy\User\MICROSOFT\IEAK\install.ins")
    
    Dim Result, TS
    Result = OpenTextFile(FilePath, ForReading, TS)
    If Result <> 0 Then
      WScript.Quit Result
    End If
    
    Dim NewContents, Line
    NewContents = ""
    Do Until TS.AtEndOfStream
      Line = TS.ReadLine
      If InStr(1, Line, "FavoritesDelete", vbTextCompare) = 0 Then
         NewContents = NewContents & Line & vbNewLine
      End If
    Loop
    TS.Close
    
    Result = OpenTextFile(FilePath, ForWriting, TS)
    If Result <> 0 Then
      WScript.Quit Result
    End If
    
    TS.Write NewContents
    TS.Close
    
    Function OpenTextFile(ByVal FilePath, ByVal Mode, ByRef TS)
      On Error Resume Next
      Set TS = FSO.OpenTextFile(FilePath, Mode)
      OpenTextFile = Err.Number
    End Function
    

    Bill

    • Marked as answer by brandoncatz81 Tuesday, January 14, 2014 4:12 PM
    Friday, January 10, 2014 5:37 PM
    Moderator
  • Two problems here.  The file is Unicode and needs to be opened and closed as Unicode.

    The IE setting in GP will reset the value but not by disabling the setting.  Just enable the setting ad set it to not remove the links.

    Two places to go for this are the GP forum and the IEAK forum.


    ¯\_(ツ)_/¯

    Friday, January 10, 2014 6:16 PM
  • My preliminary testing with your script looks to be successful.  Thanks, Bill!
    Friday, January 10, 2014 6:37 PM
  • The IEAK reference maps every IEAK setting to a single or multiple GP settings.  Download IEAK 8 or later and you will get all of the GP settings.

    You can find the settings on any W7 system with IEAK installed.  I believe that the settings do not show if you have not installed IEAK.

    If the script that Bill posted works then you are set.  I would recommend researching how to override IE in a domain. It can be very useful.


    ¯\_(ツ)_/¯

    Friday, January 10, 2014 7:05 PM
  • I will definitely look in to this more.  Thanks, jrv.
    Tuesday, January 14, 2014 4:13 PM