Sign in to follow this  
Followers 0
lafafmentvotre

Use Array to rename computer

11 posts in this topic

Hi everybody

I need some help to finalize a script to rename many computers with netdom command.

I block on array

I have a file inf like this (Rename_Computer_Auto.inf) :

oldcomputer_name_01, newcomputer_name_01

oldcomputer_name_02, newcomputer_name_02

oldcomputer_name_03, newcomputer_name_03

oldcomputer_name_04, newcomputer_name_04

......

And here is my script but i have problem tu use array

Break(1)
#region AUTOIT VARIABLES
#include
#include
#include
#include
#include
#include
#endregion
#region SCRIPT VARIABLES
Local $NewComputerName,$OldComputerName,$avArray
Local $ver="1.0"
Local $blank=''
Local $List=FileOpen('Rename_Computer_Auto.inf', 0)
$log_file='Rename_Computer_Auto_'&@YEAR&'-'&@MON&'-'&@MDAY&'.log'
$arraylist=""
Local $DomainName="my domain"
Local $AdminName="my account"
Local $Password="my password"
#endregion
#region SCRIPT
$GuiRun=GuiCreate("", 300, 60, -1, -1, $WS_POPUPWINDOW)
$label1=GUICtrlCreateLabel("Renaming in progress for :", 0, 10, 300, 20, $SS_CENTER)
$label2=GUICtrlCreateLabel("", 0, 30, 300, 20,$SS_CENTER)
GUICtrlSetFont(-1, 9, 800)
GUISetState(@SW_SHOW,$GuiRun)
For $i = 1 To UBound($arraylist) - 1 ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$avArray=StringSplit(FileReadLine($List,$i),",")
If @error = -1 Then ExitLoop
$OldComputerName=_ArrayToString($avArray, @TAB, 1, 1)
$NewComputerName=_ArrayToString($avArray, @TAB, 2, 2)
GUICtrlSetData($label2,$OldComputerName)
$ping=Ping($OldComputerName)
If $ping Then
RunAs($AdminName,$DomainName,$Password,0,@ComSpec & " /c netdom renamecomputer "&$OldcomputerName&" /NewName:"&$NewComputerName&" /ud:"&$DomainName&"\"&$AdminName&" /PasswordD:"&$Password&" /Force")
$intFile = FileOpen($log_file, 1)
FileWriteLine($intFile, $OldcomputerName&' was renamed '&$NewComputerName&' on '&@YEAR&'-'&@MON&'-'&@MDAY&' at '&@HOUR&':'&@MIN&':'&@SEC)
FileClose($intfile)
Else
$intFile = FileOpen($log_file, 1)
FileWriteLine($intFile, $OldcomputerName&' was not renamed '&$NewComputerName&' because Host is offline or unreachable')
FileClose($intfile)
EndIf
Next
GUIDelete($GuiRun)

Please could You help me ?

Sorry for my english

Share this post


Link to post
Share on other sites



Replace line

For $i = 1 To UBound($arraylist) - 1 ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
with
For $i = 1 To 99999
You exit the loop as soon as the line you want to read doesn't exist.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

you define

$arraylist=""

as an empty string (not an array)

and then you use it as an array (ubound)

For $i = 1 To UBound($arraylist)

First fill the array and then use it

Edit: Water has the more speedy fingers. :rolleyes:

Edited by Exit

My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post


Link to post
Share on other sites

Hello

Thank You

It's ok but I do not go out of the loop :-(

Share this post


Link to post
Share on other sites

Hello Exit

This is my problem, I do not control the array

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You are checking the wrong @error. It's the error value of StringSplit not of FileReadLine.

Change line

$avArray = StringSplit(FileReadLine($List, $i), ",")
If @error = -1 Then ExitLoop
to
$Line = FileReadLine($List, $i)
If @error = -1 Then ExitLoop
$avArray = StringSplit($Line, ",")
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

$avArray=StringSplit(FileReadLine($List,$i),",")
If @error = -1 Then ExitLoop

Stringsplit never returns @error = -1

you have to separate filereadline and stringsplit.

First test filereadline error and exit if <> 0

Edit: Water had the more speedy fingers again. :rolleyes:

Edited by Exit

My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post


Link to post
Share on other sites

Argggghhhhh

It is difficult to understand with all your answers

I do not understand what I write or remove

Share this post


Link to post
Share on other sites

Reply #6 and #7 describe the same problem.

Do what I suggest in post #6 and the problem should be solved.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Ok, it works

Many thanks

Share this post


Link to post
Share on other sites

Glad the problem could be solved :D

BTW: Maybe you should take some time to brush up your AutoIt knowledge. That problem was easily spotted by two users within two minutes. So it should be as easy for you too :rolleyes:


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0