Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

VBS: Rechnernamen aus Datei auslesen

Empfohlene Antworten

Hi,

ich möchte gerne ein Skript so anpassen, dass eine Variable sich Werte aus einer Datei holt (.txt oder .csv).

Das Skript sieht bisher so aus:

---------------------------------------

On Error Resume Next

strComputer = "."

blnFullDNSRegistrationEnabled = True

blnDomainDNSRegistrationEnabled = True

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colNicConfigs = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

For Each objNicConfig In colNicConfigs

intDynReg = objNicConfig.SetDynamicDNSRegistration (blnFullDNSRegistrationEnabled, blnDomainDNSRegistrationEnabled)

If intDynReg = 0 Then

WScript.Echo VbCrLf & " Successfully set dynamic DNS registration."

ElseIf intDynReg = 1 Then

WScript.Echo VbCrLf & " Successfully set dynamic DNS registration." & VbCrLf & " Must reboot."

Else

WScript.Echo VbCrLf & " Unable to set dynamic DNS registration."

End If

Next

---------------------------------------

Damit kann man auf dem lokalen PC die DNS Registrierung aktivieren. Wenn ich unter strComputer einen Computernamen eintrage, dann auch für diesen PC.

Ich würde das aber gerne automatisieren, so dass das Skript sich selbständig die Namen aus einer Datei holt und damit arbeitet.

Was muss ich dafür tun?

Dim szBirthday, szName

' Standardwerte für einige Variablen
szConfigFile = "C:\temp\birthday.txt"

Set objFileSystem = CreateObject("Scripting.FileSystemObject")

' Config-Datei ermitteln, ...
If objFileSystem.FileExists(szConfigFile) Then
' ... öffnen...
Set objFile = objFileSystem.OpenTextFile(szConfigFile)

' ... und zeilenweise abarbeiten
Do until objFile.atEndOfStream

' Geburtstag und Name lesen
szBirthday = objFile.read(6)
szName = objFile.readLine

Loop
objFile.close[/PHP]

müsstest halt noch umbauen...

Danke, ich hab es jetzt mal so angepasst:

On Error Resume Next


Dim hostname

ConfigFile = "PFAD\rechner.txt"


Set objFileSystem = CreateObject("Scripting.FileSystemObject")


If objFileSystem.FileExists(ConfigFile) Then


   Set objFile = objFileSystem.OpenTextFile(ConfigFile)


   Do until objFile.atEndOfStream


      hostname = objFile.readLine


strComputer = hostname

blnFullDNSRegistrationEnabled = True

blnDomainDNSRegistrationEnabled = True


Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colNicConfigs = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")


For Each objNicConfig In colNicConfigs

   intDynReg = objNicConfig.SetDynamicDNSRegistration (blnFullDNSRegistrationEnabled, blnDomainDNSRegistrationEnabled)

  If intDynReg = 0 Then

    WScript.Echo VbCrLf & "    Successfully set dynamic DNS registration."

  ElseIf intDynReg = 1 Then

    WScript.Echo VbCrLf & "    Successfully set dynamic DNS registration." & VbCrLf & "    Must reboot."

  Else

    WScript.Echo VbCrLf & "    Unable to set dynamic DNS registration."

  End If


   Loop

   objFile.close 


Next

Jetzt bekomme ich allerdings immer den Fehler, dass das Loop nicht ohne Do stehen darf...

fehlt ja auch n endif :P

sorry ;)

Hmm, ja, ich bin jetzt nicht so die Leuchte was Programmieren angeht. Wo genau fehlt denn das End If?

So wie ich das jetzt umgestellt habe, springt er beim nächsten Durchlaufen der Schleife nicht automatisch in die nächste Zeile der Textdatei oder? Was müsste ich dafür noch ergänzen?

Habe jetzt das Skript lauffähig, hier die Lösung:

On Error Resume Next


Dim hostname

	ConfigFile = "PFAD\rechner.txt"


Set objFileSystem = CreateObject("Scripting.FileSystemObject")


If objFileSystem.FileExists(ConfigFile) Then


   Set objFile = objFileSystem.OpenTextFile(ConfigFile)


End If


   Do until objFile.atEndOfStream


      hostname = objFile.readLine


	  strComputer = hostname

	  blnFullDNSRegistrationEnabled = True

	  blnDomainDNSRegistrationEnabled = True



	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	Set colNicConfigs = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")


	  For Each objNicConfig In colNicConfigs

   		intDynReg = objNicConfig.SetDynamicDNSRegistration (blnFullDNSRegistrationEnabled, blnDomainDNSRegistrationEnabled)

		  If intDynReg = 0 Then

			 WScript.Echo VbCrLf & "    Successfully set dynamic DNS registration."

		  ElseIf intDynReg = 1 Then

		     WScript.Echo VbCrLf & "    Successfully set dynamic DNS registration." & VbCrLf & "    Must reboot."

		  Else

			 WScript.Echo VbCrLf & "    Unable to set dynamic DNS registration."

		  End If


Next

	Loop

	objFile.close

Was mir jetzt nur noch fehlt, wäre anstatt der Ausgabe eines Fensters mit der Meldung, das Schreiben, ob erfolgreich oder nicht in eine Datei.

Hat da jemand eine Idee?

definiere halt eine zusätzliche variable namens LogFile und schreib zeilenweise in die datei.

LogFile = "PFAD\blafoo.log"

Set meinLogFile = OpenTextFile(LogFile, ForAppending, false);

meinLogFile.WriteLine("logfile-eintrag");

meinLogFile.Close();

HTH,

s'Amstel

So hier jetzt das komplette und lauffähige Skript.

Es liest Rechner-IP-Adressen aus einer Datei aus, überprüft ob der Rechner überhaupt erreichbar ist. Falls ja werden die Einstellungen vorgenommen und in eine "OK-Datei" der Rechnername eingetragen. Falls der Rechner nicht erreichbar ist oder die Einstellungen nicht vorgenommen werden konnten, wird die IP-Adresse in eine "BAD-Datei" geschrieben und zusätzlich nochmals in eine "neue Rechner-Datei", die benutzt werden kann, um das Skript gezielt auf den fehlgeschlagenen IP-Adressen später nochmals auszuführen, z.B. weil einige Rechner beim ersten Durchlaufen des Skriptes aus waren.

On Error Resume Next


'Ping Funktion, überprüft ob Rechner überhaupt erreichbar

function Ping(byval strName)

dim objFSO, objShell, objTempFile, objTS

dim sCommand, sReadLine

dim bReturn


set objShell = WScript.CreateObject("Wscript.Shell")

set objFSO = CreateObject("Scripting.FileSystemObject")


'Set default return value

bReturn = false


'Create command line to ping and save results to a temp file

sCommand = "cmd /c ping.exe -n 3 -w 1000 " & strName & " > D:\temp_dyndns.txt"


'Execute the command

objShell.run sCommand, 0, true


'Get the temp file

set objTempFile = objFSO.GetFile("D:\temp_dyndns.txt")

set objTS = objTempFile.OpenAsTextStream(1)


'Loop through the temp file to see if "antwort" is found,

'if it is then the ping was successful

do while objTs.AtEndOfStream <> true

sReadLine = objTs.ReadLine

if instr(lcase(sReadLine),"antwort") > 0 then

bReturn = true

exit do

end if

loop


'Close temp file and release objects

objTS.close

objTempFile.delete

set objTS = nothing

set objTempFile = nothing

set objShell = nothing

set objFSO = nothing


'Return value

Ping = bReturn

end function



'Hauptprogramm

Dim hostname

	rechnerFile = "D:\...\dyndns_rechner.txt"


Set objFileSystem = CreateObject("Scripting.FileSystemObject")


If objFileSystem.FileExists(rechnerFile) Then


   Set objrechnerFile = objFileSystem.OpenTextFile(rechnerFile)


End If


   Set objergebnis_ok_File = objFileSystem.CreateTextFile("D:\...\dyndns_ok.txt",true)

   Set objergebnis_bad_File = objFileSystem.CreateTextFile("D:\...\dyndns_bad.txt",true)

   Set objneuFile = objFileSystem.CreateTextFile("D:\...\dyndns_rechner_neu.txt",true)


   Do until objrechnerFile.atEndOfStream


      hostname = objrechnerFile.readLine


		bPingtest = ping(hostname)

		If bPingtest = TRUE Then


			Dim objItem, colItems

			strComputer = hostname

	 		blnFullDNSRegistrationEnabled = True

	  		blnDomainDNSRegistrationEnabled = True



			Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

			Set colNicConfigs = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

			Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")


	  		For Each objNicConfig In colNicConfigs

   				intDynReg = objNicConfig.SetDynamicDNSRegistration (blnFullDNSRegistrationEnabled, blnDomainDNSRegistrationEnabled)

		  			If intDynReg = 0 Then

						For Each objItem in colItems

			 			objergebnis_ok_File.WriteLine(objItem.CSName & " - Erfolgreich!")


		  			ElseIf intDynReg = 1 Then

 			  			For Each objItem in colItems

	     	 			objergebnis_ok_File.WriteLine(objItem.CSName & " - Erfolgreich! Neustart erforderlich")


		  			Else

			 			objergebnis_bad_File.WriteLine(hostname & " ----------- Fehlgeschlagen!")

			 			objneuFile.WriteLine(hostname)


		  			End If

				Next


		Else

			objergebnis_bad_File.WriteLine(hostname & " --- Host nicht erreichbar!")

			objneuFile.WriteLine(hostname)

	 		objergbnis_bad_File.Close

	 	End If


	Loop

	objergebnis_ok_File.close

	objergebnis_bad_File.close

	objneuFile.close

	objrechnerFile.close

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.