|
|
|||||||
hi i have a script which changes the ip-adress in our network. and i have a txt.-file where are in each line is the computername, the new ip-adress, the new DefaultGateway. ( computername; 168.18.68.11; 168.18.68.254) and the script has to make 3 variables out of one line, and then the next line and so on can someone help me |
||||||||
|
|
|||||||
Something like this? Code:
|
||||||||
|
|
|||||||
thanks i used your example in my script, but it doesn't work. sorry it's partly in german |
||||||||
|
|
|||||||
oops i forget to post my script ;----------------------------------------------------------------------------- ;Programm initialisiern ;----------------------------------------------------------------------------- Color y+/b ;Übergabe-Parameter (zu Testzwecken, ansonsten auskommentieren) $SoftwareSource = "U:\kix\" ;Konstanten definieren $ScrName = "ip1.script" $INIFile = $SoftwareSource+"\NicAD2.ini" $LOGFile = "U:\kix\remoteip.log" $Liste = "U:\kix\liste.txt" ;Variablen initialisieren $ScriptError = 0 $regerr = 0 ;Log-File öffnen $ret = Open(1, $LOGFile, 5) ;wenn Log-File nicht vorhanden, dann neues Log-File erzeugen ;----------------------------------------------------------------------------- ;PC-Namen und neue IP-Adresse auslesen ;----------------------------------------------------------------------------- $ = open(2, $Liste) ;opens the file liste.txt $Line = split(readline(1),"; ") while not @error $PC = trim($line[0]) $ret = writeline (1, "PC-Name ausgelesen...$PC") $newIP = trim($line[1]) $ret = writeline (1, "neue IP ausgelesen...$newIP") $newGW = trim($line[2]) ? "Computer: " $pc ? "New IP: " $newIP ? "New GW: " $newGW $Line = split(readline(1),"; ") Gosub "Sub_Network" Loop ;Ende ;----------------------------------------------------------------------------- ;Programm beenden ;----------------------------------------------------------------------------- :ENDE ;Logfile schließen $ret = WriteLine (1, "---"+$ScrName+" done----------------------------------"+@CRLF+@CRLF) $ret = Close (1) ;Logfile anzeigen (nur zu Testzwecken) Shell "cmd.exe /C start %SystemRoot%\notepad.exe "+$LOGFile Sleep 1 ;sonst wird das Logfile zu schnell gelöscht Exit $ScriptError ;----------------------------------------------------------------------------- ;Netzwerkkarten auslesen und ggf. konfigurieren ;----------------------------------------------------------------------------- :Sub_Network ;Variablen initialisieren $Index = 0 $RootKeyNICs = "\\$PC\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\" $subkey = EnumKey($RootKeyNICs, $Index) ;nach Netzwerkkarten suchen While @ERROR = 0 ;Variablen initialisieren $EthID = "" $NIC_Config = "" $NIC_NoConfig = "" ;Netzwerkkarten-ID (ServiceName) auslesen $EthID = ReadValue($RootKeyNICs+$subkey,"ServiceName") ;wenn Netzwerkkarte vorhanden, If $EthID <> "" ;Netzwerkkarten-Description auslesen $Description = ReadValue($RootKeyNICs+$subkey,"Description") $ret = WriteLine (1, "Netcard: $Description"+@CRLF) $ret = WriteLine (1, "NetcardID: $EthID"+@CRLF+@CRLF) ;überprüfen, ob Netzwerkkarte bekannt und für Konfiguration definiert $NIC_Config = ReadProfileString($INIFile, "Umzustellende Netzwerkkarten" ,$Description) If $NIC_Config = "1" $ret = WriteLine (1, " Netzwerkkarte erkannt und fuer Konfiguration definiert"+@CRLF) Else ;überprüfen, ob Netzwerkkarte bekannt, aber nicht für Konfiguration definiert $NIC_NoConfig = ReadProfileString($INIFile, "Nicht-umzustellende Netzwerkkarten" ,$Description) If $NIC_NoConfig = "1" $ret = WriteLine (1, " Netzwerkkarte bekannt, aber nicht fuer Konfiguration definiert"+@CRLF+@CRLF) Else ;Netzwerkkarte unbekannt, => in INI-File als neue Netzwerkkarte eintragen $ret = WriteLine (1, "Netzwerkkarte nicht bekannt, bitte in Datei "+$INIFile+" eintragen."+@CRLF+@CRLF) ;$ret = WriteProfileString($INIFile," Neue Netzwerkkarten",$Description,$EthID) $ret = WriteLine(1, "[Neue Netzwerkkarte]"+@CRLF+$Description+"=1"+@CRLF) $ScriptError=2 GoTo ENDE EndIf EndIf EndIf ;wenn Netzwerkkarte für Konfiguration definiert If $NIC_Config <> "" ;entsprechende Aktionen ausführen Select Case $action="ip" ;IP-Adresse ändern $ret = WriteLine (1, " IP-Adresse wird nun geändert..."+@CRLF+@CRLF) Gosub "SUB_ConfigureIP" Case $action="info" $ret = WriteLine (1, " keine Aktion durchgeführt (info)..."+@CRLF) EndSelect EndIf If $regerr>0 $ret = WriteLine(1, "RegError: es konnten "+$regerr+" Registry-Einträge nicht vorgenommen werden"+@CRLF) $ScriptError=6 GoTo ENDE EndIf ;nächsten Subkey auslesen $ret = WriteLine (1, @CRLF) $Index = $Index+1 $subkey = EnumKey($RootKeyNICs, $Index) Loop Return ;Ende ;----------------------------------------------------------------------------- ;Subroutine für IP-Adressenänderung ;----------------------------------------------------------------------------- :SUB_ConfigureIP $NIC_Parameter="\\$pc\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\"+$EthID+"\Parameters\Tcpip" $regerr = $regerr + (WriteValue($NIC_Parameter, "IPAddress", "$newIP", "REG_MULTI_SZ")<>0) $regerr = $regerr + (WriteValue($NIC_Parameter, "DefaultGateway", "$newGW", "REG_MULTI_SZ")<>0) $ret = writeline (1, " neue IP-Adresse:...$newIP"+@CRLF+@CRLF) $ret = writeline (1, " neuer Gateway:...$newGW"+@CRLF+@CRLF) $TCPIP_Parameter="\\$pc\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"+$EthID $regerr = $regerr + (WriteValue($TCPIP_Parameter, "IPAddress", "$newIP", "REG_MULTI_SZ")<>0) $regerr = $regerr + (WriteValue($TCPIP_Parameter, "DefaultGateway", "$newGW", "REG_MULTI_SZ")<>0) $ret = WriteLine (1, " IP-Change von "$PC" abgeschlossen"+@CRLF) Return ;Ende |
||||||||
|
|
|||||||
Are you sure you're reading from the right file? Code:
|
||||||||
|
|
|||||||
i think so... $Liste = "U:\kix\liste.txt" |
||||||||
|
|
|||||||
Let me rephrase, you are not reading from the correct file. You need to change your $Line assignments from Code:
to Code:
Right now, you're trying to read from the file you are writing to. |
||||||||
|
|
|||||||
ok, thanks a lot |
||||||||
|
|
|||||||
hi, reading the list works now, the script takes 3 variabels out of one line, but it makes only the first line. the loop doesn't work code: $liste = "U:\kix\liste.txt" $ = open(2, $liste) ;opens the file liste.txt $Line = split(readline(2),"; ") while @ERROR = 0 $PC = trim($line[0]) $ret = writeline (1, "PC-Name ausgelesen.........$PC"+@CRLF) $newIP = trim($line[1]) $ret = writeline (1, "neue IP ausgelesen.........$newIP"+@CRLF) $newGW = trim($line[2]) $ret = writeline (1, "neuer Gateway ausgelesen...$newGW"+@CRLF+@CRLF) ? "Computer: " $pc ? "New IP: " $newIP ? "New GW: " $newGW ? "" $Line = split(readline(2),"; ") Gosub "Sub_Network" :PCIP Loop ;Ende |
||||||||
|
|
|||||||
If I recall correctly, there was a bug some time back that caused loop structures to fail when a subroutine was called . To test this comment out the 'Gosub "Sub_Network"' and see if the loop displays all the values you expect. If it does, just convert the subroutine to a UDF. |
||||||||
|
|
|||||||
you recalled correctly, because now it works. how can i convert in udf? |
||||||||
|
|
|||||||
The UDF will look something like this: Code: Function ConfigureIP($sPC,$sEthID,$sNewIp,$sNewGW) Now, instead of the gosub, call it like this: Code: $regerr=ConfigureIP($ps,$EthID,$newIP,$newGW) |
||||||||
|
|
|||||||
ok, i will try it |
||||||||
|
|
|||||||
I wasn't able to convert my subroutine into a UDF so I tried something else. i changed the while-loop command in a do-until command but it still doesn't work. so instead of gosub I use now goto Code:
and under goto network Code:
now the loop-function works, but when the script arrives the end of the textfile i get a "ERROR : array reference out of Bounds!" |
||||||||
|
|
|||||||
I think the problem is that your subroutines may be setting @error which when you is then used in your loop instead of the @error from the $Line assignment. Can you try putting the $Line assignment right before your "Loop" statement (right after the ":PCIP") |
||||||||
|
|
|||||||
GOTOs are an even greater sin than GOSUBs and should not be used. |
||||||||
|
|
|||||||
i tried it, but it doesn't. it's still the same problem . i put the PCIP under the "do" Code:
now the whole scripts works, except the end. it changes all IP-Adresses, and takes the whole list. but instead of going to end when the list worked up, i get this error |
||||||||
|
|
|||||||
This is what i meant. And Les is right, you really should try to avoid goto's where ever possible (which is basically anywhere) Code:
|
||||||||
|
|
|||||||
You should NEVER jump into a loop with GOTO! The sooner you learn how not to spaghetti code with GOTO, the better. |
||||||||
|
|
|||||||
Code: I wasn't able to convert my subroutine into a UDF Why You should have been able to drop the code I provided into your script without modification. (Well, maybe a little bug fixing, I couldn't test it here) Add the "Function ... EndFunction" code to the end of your script. Replace your Gosub with the function call and you're done. |
||||||||
|
|
|||||||
See also the CheckNICs() UDF. |