Jump to content

Computername to .XML file


 Share

Recommended Posts

Hi Guys,

I am new here and a newbie using autoIT. Just wondering if you can help me to create a script for getting the computer name and input this to a .xml file. I have hundreds of computer and will kill me if i do this manually. lol

Here's the scenario,

I am working on a script which will get the last 7 digits of my computer name and put it on .xml file. The .xml file is located on the Program files. Below is the .xml file:

<?xml version="1.0"?>
<Settings Version="" 
          SchemaVersion="1.0.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
          xmlns="http://avaya.com/OneXAgent/Settings"
          DeleteOnExit="false" 
          DeleteWorkLogOnExit="false" 
          DeleteContactsOnExit="false"           
          WelcomeMessage="" 
          UploadInterval="10">
          <Login >
                <Telephony    ReadOnly="false" >
                      <Server ReadOnly="false"  MaximumAttempts="2">
                                <DialingRules ReadOnly="true"
                                       OutsideLineAccessNumber=""
                                       CountryCode=""
                                       AreaCode=""
                                       AccountCode=""
                           LongDistancePrefix=""
                                       InternationalPrefix=""
                                       InternalExtensionLength="7"
                                       NationalCallLength=""
                           SendonLocalCalls="false"
                           SendonLongDistanceCalls="false" 
                           SendonInternationalCalls="false" 
                           CommaCharPauseValue="2"/>
                                <CallServerAddress1 ReadOnly="true" Value=""/>
                      <CallServerAddresses ReadOnly="true">
          <ServerAddress ReadOnly="true" Value="" />                            
                          </CallServerAddresses>
                </Server>
                      <User ReadOnly="false" 
                            Station="" 
                            Password=""   
                            AutoLogin="true" 
                            SavePassword="true" 
                            AutoAnswerSupportRequired="true" />
                      <Type ReadOnly="true" 
                            Mode="MyComputer" 
                            SelectedPhoneNumberId=""/>
                      <License ReadOnly="true" Type="Agent"/>
                      <PhoneNumbers ReadOnly="true"/>              
                </Telephony>
                <Agent ReadOnly="false" 
                       Login="" Password="" 
                       AutoLogin="false" 
                       SavePassword="true" 
                       EnterAuxOnLogin="true" />
                <Im ReadOnly="true" >
                       <Server ReadOnly="false" 
                             ServerAddress1="" 
                             Enabled="false"/>
                       <User ReadOnly="false" 
                             LoginId="" 
                             Password="" 
                             Domain=""  
                             AutoLogin="false" />
                 </Im>
          </Login>
          <WorkHandling ReadOnly="false">
              <In ReadOnly="true" AutoIn="false" />
              <Accept AutoAccept="false" ReadOnly="true"  />
              <Complete AutoComplete="true" ReadOnly="false" >
                <FollowUp IsTimedFollowUp="false" ReadOnly="true">
                  <TimedFollowUp TimerPeriod="30" 
                                 AllowExtension="false" ReadOnly="true" />
                </FollowUp>
              </Complete>
              <Hold AutoHold="true" ReadOnly="true"  />
              <Ready AutoReady="true" ReadOnly="true" ReasonCode="0"  />
              <Conference Consult="true" ReadOnly="true" />
              <Transfer Consult="true" ReadOnly="true" />
          </WorkHandling>
          <ContactLog DaysToKeepLog="5" 
                ContactLogPerScreen="25" 
                ReadOnly="false" >
              <Voice LogIncoming="true" LogOutgoing="true" />
              <Im LogIncoming="true" 
                    LogOutgoing="true" 
                    SaveIncomingTranscript="true" 
                    SaveOutgoingTranscript="true" />
              <Tty LogIncoming="true" 
                    LogOutgoing="true" 
                    SaveIncomingTranscript="true" 
                    SaveOutgoingTranscript="true" />
          </ContactLog>
          <Directory ReadOnly="true" />
          <OutlookContact ReadOnly="true" TimeOut="30" />
          <ClickToDial ReadOnly="false" Active="true" />
          <DesktopSharing ReadOnly="true" Enabled="false"/>
          <Video>
              <Basic EnableVideoCall="true" 
                   AllowVideoFilePlay="true" 
                   BroadcastVideoAuto="false"/>
              <Advanced AudioBufferSize="20"
                   PerformanceMode="MotionSmoothness" 
                   CpuPriority="Balanced"/>
          </Video>
          <Im>
                <Permissions AllowIMToNonContacts="true" ReadOnly="false"/>
                <Greeting Text="Hello" />
                <IdleTime Time="15"/>
          </Im>
          <Tty ReadOnly="false" >
                <Greeting Text="" ReadOnly="false"/>
          </Tty>
          <VoiceMail ReadOnly="true" 
               EnableMessageAccess="true" 
               Action="Dial" 
               PhoneNumber="" 
               Path="" Url="" />
          <LaunchApplication ReadOnly="true" />
          <Profile ReadOnly="true" />
          <Logging ReadOnly="false" 
               LogLevel="DEBUG" 
               Appender="LocalLogging_GeneralFormat" 
               CentralLoggingHost="" 
               DaysToKeepLog="30"/>
</Settings>

I want to put the last seven computer name to the "station" (red font in the .xml code). Can you please help me to code:

1. Get the Last 7 Computer NAme

2. Put it on the "station" on the .xml file.

Btw, i have this code which will get the 7 last digit of the computer name, but don't know if this works because i have use this code on regwrite command before:

$ext = stringright (@ComputerName, 7)

;msgbox (1, "test", $ext)

Advance thanks to anyone who can help me. :) 

Link to comment
Share on other sites

  • Moderators

Hi, jhedpena. The simplest way, if the file will be static, is something like this:

#include <File.au3>

$station = StringRight(@ComputerName, 7)
    _FileWriteToLine(@DesktopDir & "\File.xml", 34, '                            Station="' & $station & '"', 1)

Otherwise, you could read the file into an array, and then loop through the array looking for "station" and replace the line. Someone will probably wander along to give you a nice regex to handle it faster, but as these make my eyes bleed I'm not going to attempt it :) 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Howdy,

  This one looked familiar so I thought I'd put in my 2 cents. I personally prefer the Find and Replace method when changing stuff like this.

So another way, not better not simpler possibly a little more adaptive:

#include <File.au3>
$ext = stringright (@ComputerName, 7)
Local $find = 'Station=""'
Local $replace = 'Station="' & '"' & $ext &'"'



Local $filename = "C:\Users\YOUNAME\Desktop\test.xml"

Local $retval = _ReplaceStringInFile($filename, $find, $replace)
If $retval = -1 Then
    MsgBox(0, "ERROR", "The pattern could not be replaced in file: " & $filename & " Error: " & @error)
    Exit
Else
    MsgBox(0, "INFO", "Found " & $retval & " occurances of the pattern: " & $find & " in the file: " & $filename)
EndIf
Link to comment
Share on other sites

That's fine if the structure is fixed, but I'd use the xml dom to add/edit this...it's sure fire.  There is no "station" to replace...paste in the node you want to update+

$xml = @DesktopDir & "\some.xml"
Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.Load($xml)

$oStationAttribute = $oXML.SelectSingleNode("//User/@Station")
$oStationAttribute.Value="whatever you want it to be"

$oXML.save($xml)
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

updated to include an example above

Example of checking if the attribute exists first...you can do the same type of thing with adding the node, if it's not present (diff method, google it)

$xml = @DesktopDir & "\some.xml"
Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.Load($xml)

$oStationAttribute = $oXML.SelectSingleNode("//User/@Station")

If IsObj($oStationAttribute) Then
    ; Change value of attribute when present
    $oStationAttribute.Value="whatever you want it to be"
Else
    ; Add attribute if not
    $oXML.SelectSingleNode("//User").setAttribute("Station","whatever you want it to be")
EndIf

$oXML.save($xml)
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...