Jump to content

Use Array to rename computer


Recommended Posts

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

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

$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

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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...