Announcement

Collapse
No announcement yet.

Pull computer name, modify and insert in text

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pull computer name, modify and insert in text

    I have a .ws file that needs a line edit on 500+ machines.

    The line is: WorkStationID=XXXXXXXX

    Currently, each machine has a unique WorkStationID but I need these ID's modified.

    I need the WorkStationID to equal the current computer name with the first character of the name changed to the letter A and the letter A added to the end.

    Ex...

    ComputerName= 123alpha1

    I need WorkStationID=A23alpha1A

    Any takers on this challenge?

    Thanks

  • #2
    Re: Pull computer name, modify and insert in text

    Originally posted by s01010101 View Post
    I have a .ws file that needs a line edit on 500+ machines.

    The line is: WorkStationID=XXXXXXXX

    Currently, each machine has a unique WorkStationID but I need these ID's modified.

    I need the WorkStationID to equal the current computer name with the first character of the name changed to the letter A and the letter A added to the end.

    Ex...

    ComputerName= 123alpha1

    I need WorkStationID=A23alpha1A

    Any takers on this challenge?

    Thanks
    You want to use a computer Startup Script to do the edits?
    Is the .ws file in exact the same folder on every computer?


    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Pull computer name, modify and insert in text

      I was thinking of using psexecute to run a vbscript or batch file to do this.

      I'm open to using a startup script.

      The .WS is in the same directory on every machine.

      In fact, I'm going to mass copy a new revision of the .ws file to all the machines and then I'm going to need to modify that line.

      I've done some basic scripting and I'm able to modify the .ws as if it were a .txt

      I just don't have the detailed experience or know how to get the syntax together to drop the computer name in with the paramaters specified.

      Thanks for your reply.

      Comment


      • #4
        Re: Pull computer name, modify and insert in text

        hostname >> c:\hostname.txt

        (Drops my computer name in to a txt file on the machine)

        This script will change the first character to an A if it's a 1

        Const ForReading = 1
        Const ForWriting = 2

        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFile = objFSO.OpenTextFile("C:\PROGRA~1\IBM\CLIENT~1\EMUL ATOR\PRIVATE\Test.ws", ForReading)

        strText = objFile.ReadAll
        objFile.Close
        strNewText = Replace(strText, "WorkStationID=1ComputerTest", "WorkStationID=AComputerTest")

        Set objFile = objFSO.OpenTextFile("C:\PROGRA~1\IBM\CLIENT~1\EMUL ATOR\PRIVATE\Test.ws", ForWriting)
        objFile.WriteLine strNewText
        objFile.Close


        I was thinking I could copy the computer name from the text file, change the 1st character to an A, add the final A and then drop it in the WorkStationID= line.

        I'm probably way off though. (I have worked on it)

        Comment


        • #5
          Re: Pull computer name, modify and insert in text

          Originally posted by s01010101 View Post
          change the 1st character to an A, add the final A and then drop it in the WorkStationID= line.
          This isn't too hard if you know the mid function. It is used to get substrings out of a string. So let's just catenate A, most parts of the computer name and another an A together.

          Code:
          strNewText = "A" & mid(strText, 2, len(strText)-1) & "A"
          -vP

          Comment


          • #6
            Re: Pull computer name, modify and insert in text

            and... also add a regular expression, then you'll get this,
            Code:
            Const ForReading = 1
            Const ForWriting = 2
            
            Set wshShell = WScript.CreateObject( "WScript.Shell" )
            
            sComputerName = wshShell.ExpandEnvironmentStrings _
               ("%COMPUTERNAME%")
            sTargetFile = wshShell.ExpandEnvironmentStrings _
               ("%PROGRAMFILES%\IBM\CLIENT ACCESS\EMULATOR\PRIVATE\Test.ws")
            
            SearchPattern = "\bWorkStationID([ \t]+)?=([ \t]+)?.*\b"
            ReplacementStr = "WorkStationID=" & "A" & mid(sComputerName,2) & "A"
            
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            Set objFile = objFSO.OpenTextFile(sTargetFile, ForReading)
            
            strNewText = ReplaceTest _
                 (objFile.ReadAll, SearchPattern, ReplacementStr)
            objFile.Close
            
            Set objFile = objFSO.OpenTextFile(sTargetFile, ForWriting)
            objFile.Write strNewText
            objFile.Close
            
            Function ReplaceTest(str1, patrn, replStr)
              Dim oRE
              Set oRE = New RegExp
              oRE.Pattern = patrn
              oRE.IgnoreCase = True
              oRE.Global = True
              ReplaceTest = oRE.Replace(str1, replStr)
            End Function
            This vbscript sample should be launched on each client locally (you can run it as startup script OR, use psExec).

            Note:
            The name of each client computer is unique in the domain. But if you would drop the first letter it might not longer be a unique name that you set in this IBM-scriptfile!

            \Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              Re: Pull computer name, modify and insert in text

              Great!!

              This helps me tremendously!!

              I'll have to modify it to configure dual .ws files and a few other things but this looks like it!!!

              Thank alot!!!!

              I'll follow up with my final script....

              Comment


              • #8
                Re: Pull computer name, modify and insert in text

                A few edits:
                1) The replacement string new value is now added between quotes: WorkStationID="AomputerA"
                2) I've modified the 'searchfor' pattern (!)
                3) Script adjusted for easy support for more than one .ws file

                Code:
                Const ForReading = 1
                Const ForWriting = 2
                
                Set wshShell = WScript.CreateObject( "WScript.Shell" )
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                
                sComputerName = wshShell.ExpandEnvironmentStrings _
                   ("%COMPUTERNAME%")
                
                Dim strNewText
                SearchPattern = "(\bWorkStationID([ \t]+)?=([ \t]+)?)((.*"")|(.*\b))"
                ReplacementStr = "WorkStationID=""A" & mid(sComputerName,2) & "A"""
                
                
                ' *** Filename is "Test.ws" ***
                sTargetFile = wshShell.ExpandEnvironmentStrings _
                   ("%PROGRAMFILES%\IBM\CLIENT ACCESS\EMULATOR\PRIVATE\Test.ws")
                If objFSO.FileExists(sTargetFile) Then
                   ParseContent objFSO.OpenTextFile(sTargetFile, ForReading)
                   SaveContent objFSO.OpenTextFile(sTargetFile, ForWriting)
                End If
                
                
                wscript.quit
                
                Sub ParseContent(ByVal objFile)
                   With objFile
                     strNewText = ReplaceTest _
                       (.ReadAll, SearchPattern, ReplacementStr)
                     .Close
                   End With
                End Sub
                
                Sub SaveContent(ByVal objFile)
                   With objFile
                     .Write strNewText
                     .Close
                   End With
                End Sub
                
                Function ReplaceTest(str1, patrn, replStr)
                  Dim oRE
                  Set oRE = New RegExp
                  oRE.Pattern = patrn
                  oRE.IgnoreCase = True
                  oRE.Global = True
                  ReplaceTest = oRE.Replace(str1, replStr)
                End Function
                \Rems
                Last edited by Rems; 13th January 2010, 00:36.

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment

                Working...
                X