MSFN Forum: [Help] VBscript gets info from text file - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Help] VBscript gets info from text file Rate Topic: -----

#1 User is offline   D8TA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 136
  • Joined: 18-February 05

Posted 16 November 2006 - 12:11 PM

I have this vbscript that creates a scheduled task to reboot a PC at 11:59pm on every Saturday. What I would like to do is have this script read from a text file that will contain all of the PC numbers so as the script runs it will read the text file and apply the scheduled task to other remote PC on the text document.

Here is the script I have
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create _
	("shutdown.exe -r -t 120", "********235900.000000-300", _
		True , 32, , , JobID) 
Wscript.Echo errJobCreated



#2 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,020
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 16 November 2006 - 03:47 PM

Try this I can not test to see if it works on network I have only tested it for run time errors.

The Script I tested this return the contents of ComputerLists.txt
This returned the contents of the text file in my test.

Quote

'/-> 4128 Replaces This For The Mesage Box Popup = 0 + 32 + 4096
 Const ForReading = 1
 Dim Act, Cnt, Fso, objNewJob, objWMIService, StrComputer, TS, TXT
  Set Fso = CreateObject("Scripting.FileSystemObject")
  Set Act = CreateObject("Wscript.Shell")
'/-> Place The Ful Path Here For The Text File, Unless It Ran 
'/-> From The Same Location As This Script
  TXT = "ComputerLists.txt"
'/-> Build The ComputerLists.txt For The Collection
  Set TS = Fso.CreateTextFile("ComputerLists.txt")
  Do 
   Cnt = Cnt + 1 
   If Cnt > 9 Then 
	TS.WriteLine "Computer Name " & Cnt 
   ElseIf Cnt = 9 Then 
	TS.WriteLine "Computer Name 0" & Cnt
   Else 
	TS.WriteLine "Computer Name 0" & Cnt
   End If 
  Loop Until Cnt = 12 
  TS.Close
'/-> Collects All The ComputerLists.txt Name Here  
  Set TS = Fso.OpenTextFile("ComputerLists.txt", ForReading)
   Do Until TS.AtEndOfStream
	StrComputer = TS.ReadLine
	Act.Popup "Preparing To Run Shutdown On This Computer : " & StrComputer,3,"Shut Down",4128
   Loop
  TS.Close
  Act.Popup "Completed Shut Down Script" & vbCrLf &_
  "For All Network Computers", 5, "Finish", 4128


Script that you can try.

Quote

'/-> 4128 Replaces This For The Mesage Box Popup = 0 + 32 + 4096
 Const ForReading = 1
 Dim Act, Fso, objNewJob, objWMIService, StrComputer, TS, TXT
  Set Fso = CreateObject("Scripting.FileSystemObject")
  Set Act = CreateObject("Wscript.Shell")
'/-> Place The Full Path Here For The Text File, Unless It Ran 
'/-> From The Same Location As This Script
 TXT = "ComputerLists.txt"
'/-> Collects All The ComputerLists.txt Name Here  
  Set TS = Fso.OpenTextFile(TXT, ForReading)
   Do Until TS.AtEndOfStream
	StrComputer = TS.ReadLine
	Act.Popup "Preparing To Run Shutdown On This Computer : " & StrComputer,3,"Shut Down",4128
   Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & StrComputer & "\root\cimv2")
   Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
	errJobCreated = objNewJob.Create("shutdown.exe -r -t 120", "********235900.000000-300",True , 32, , , JobID) 
	Act.Popup  errJobCreated,5,"Err Job Created",4128
   Loop
  TS.Close
  Act.Popup "Completed Shut Down Script" & vbCrLf &_
  "For All Network Computers", 5, "Finish", 4128


#3 User is offline   D8TA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 136
  • Joined: 18-February 05

Posted 16 November 2006 - 03:58 PM

This collects the computers name? I don't think I explained myself very well. The script I posted will create a scheduled task that will reboot the PC at 11:59pm EST every Saturday. I have a text file with all of our PCs. What I want to do is run this vbscript using the text file. I have another script I use and I can use the following to apply the script to all of the PCs. I use msclient.vbs textfile.txt and it will install the Microsoft client on all the PC in that text file. I will try the script you posted smokinggunman, I may not be reading it right. But I would like to use SchTask.vbs textfile.txt and have the script run against all PCs in the text file. I tried using the same thing as I have in the msclient.vbs but it doesn't work. I hope that helps clear things up.

Thanks again smokinggunman. By the way, I've seen several of your scripts do you have a book or a place you have all your scripts located? I am really trying to learn this vbscript thing but sometimes it just goes right over my head.

Could I add this?
Const ForReading = 1
Dim Fso	  : Set Fso = CreateObject("Scripting.FileSystemObject")
Dim TextFile : TextFile = "Test.txt"
Dim StrLine, Ts
  Set Ts = Fso.OpenTextFile(TextFile,ForReading)
  
   Do Until Ts.AtEndOfStream
	StrLine = Ts.ReadLine
	WScript.Echo StrLine
   Loop


Would this read the Test.txt file and apply the SchTask.vbs to all PCs in the Test.txt file?
Const ForReading = 1
Dim Fso	  : Set Fso = CreateObject("Scripting.FileSystemObject")
Dim TextFile : TextFile = "Test.txt"
Dim StrLine, Ts
  Set Ts = Fso.OpenTextFile(TextFile,ForReading)
  
   Do Until Ts.AtEndOfStream
	StrLine = Ts.ReadLine
	WScript.Echo StrLine
   Loop
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create _
	("shutdown.exe -r -t 120", "********235900.000000-300", _
		True , 32, , , JobID)
Wscript.Echo errJobCreated

This post has been edited by D8TA: 16 November 2006 - 04:05 PM


#4 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,020
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 16 November 2006 - 04:46 PM

The top script was a demo in how to get the values from a text file back into the Script running.
It creates a text file then it read it line by line then a message box appears for each line of the
text file with it as a new varible.

The second script should in theory connect to StrComputer which get it varible from the text file line

Quote

StrComputer = TS.ReadLine
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & StrComputer & "\root\cimv2")

Quote

Short Falls
1:\ There no Error control If The Computer you want is not On Line
2:\ This only checks for the Computer Name On each line so if there is 2 or more names on the text file line then it will fail.
Would this read the Test.txt file and apply the SchTask.vbs to all PCs in the Test.txt file?

Quote

Const ForReading = 1
Dim Fso	  : Set Fso = CreateObject("Scripting.FileSystemObject")
Dim TextFile : TextFile = "Test.txt"
Dim StrLine, Ts
  Set Ts = Fso.OpenTextFile(TextFile,ForReading)
  
   Do Until Ts.AtEndOfStream
	StrLine = Ts.ReadLine
	WScript.Echo StrLine
   Loop
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create _
	("shutdown.exe -r -t 120", "********235900.000000-300", _
		True , 32, , , JobID)
Wscript.Echo errJobCreated

This will work only for 1 computer, it will fail if there is more then 1 computer because the loop will only hold 1 name.
The 2 scripts I posted pass each line of the text file as a new varible each time it goes threw the loop, that can be used as the varible for the shutdown part.

This is where I go to learn about VBS Scripts
The Hey, Scripting Guy! Archive

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy