Solved

How do I create a script from a text file

Posted on 2008-10-30
8
240 Views
Last Modified: 2012-05-05
This is probably a very beginner question for all you experts BUT for me it is GREEK so...

I have a script I copied from someone and I want it to run on my domain to collect info.  I have it in a text file...  now what do I do?  I thought I could just change the extension from .txt to .vbs but that doesn't seem to work.
0
Comment
Question by:darovitz
8 Comments
 
LVL 7

Expert Comment

by:pr0t0c0l12
ID: 22846982
what is the script for?  lets pretend the script is a function, then you need to add it to your actual project.  To do that open your project and then paste it on the code section.  

It all depends what the code contains and how you want to reference it from your program.  

Let us know.
0
 

Author Comment

by:darovitz
ID: 22846997
Here is the script.  It is looking at all servers in my domain and then putting 10.35.50.1 as primary DNS Server and 10.35.50.19 as Secondary DNS server.

This is my first attempt at any kind of scripting so I have no idea what I am doing:


On Error Resume Next
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell : Set objShell = CreateObject("Wscrfipt.Shell")
Dim objFile : Set objFile = objFSO.OpenTextFile("c:\computers.txt") 
counter = 1
Do While Not objFile.AtEndOfStream
	currentserver = objFile.ReadLine
	Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & currentserver & "\root\cimv2")
	Set colNicConfigs = objWMIService.ExecQuery  ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
	For Each objNicConfig In colNicConfigs
		If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
			For Each strDNSServer In objNicConfig.DNSServerSearchOrder
				If strDNSServer = "10.35.50.1" Then
					If counter = 1 Then
						'It's the Primary DNS Server
						objShell.Run "netsh -r " &currentserver &" interface ip delete dnsserver " &chr(34) &"local area connection" &Chr(34) &" 10.35.50.1",1,True
						objShell.Run "netsh -r " &currentserver &" interface ip set dns name=" &chr(34) &"local area connection" &Chr(34) &" static 10.35.50.1",1,True
					ElseIf counter = 2 Then
						'Its the Secondary DNS Server
						objShell.Run "netsh -r " &currentserver &" interface ip delete dnsserver " &chr(34) &"local area connection" &Chr(34) &" 10.35.50.19",1,True
						objShell.Run "netsh -r " &currentserver &" interface ip set dns name=" &chr(34) &"local area connection" &chr(34) &" 10.35.50.19 index=2",1,True				
					End If
				End If				
				counter = counter + 1
			Next
		End If
	Next
Loop

Open in new window

0
 
LVL 7

Expert Comment

by:pr0t0c0l12
ID: 22847119
You can put it in a web page such as here!

Make sure that you declare your variables (such as counter, currentserver, objWMIService, colnicconfigs, etc...) before you run it otherwise it will give you a run time error.  

Good luck.
<html>
<body>
<%
 
On Error Resume Next
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell : Set objShell = CreateObject("Wscrfipt.Shell")
Dim objFile : Set objFile = objFSO.OpenTextFile("c:\computers.txt") 
counter = 1
Do While Not objFile.AtEndOfStream
        currentserver = objFile.ReadLine
        Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & currentserver & "\root\cimv2")
        Set colNicConfigs = objWMIService.ExecQuery  ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
        For Each objNicConfig In colNicConfigs
                If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
                        For Each strDNSServer In objNicConfig.DNSServerSearchOrder
                                If strDNSServer = "10.35.50.1" Then
                                        If counter = 1 Then
                                                'It's the Primary DNS Server
                                                objShell.Run "netsh -r " &currentserver &" interface ip delete dnsserver " &chr(34) &"local area connection" &Chr(34) &" 10.35.50.1",1,True
                                                objShell.Run "netsh -r " &currentserver &" interface ip set dns name=" &chr(34) &"local area connection" &Chr(34) &" static 10.35.50.1",1,True
                                        ElseIf counter = 2 Then
                                                'Its the Secondary DNS Server
                                                objShell.Run "netsh -r " &currentserver &" interface ip delete dnsserver " &chr(34) &"local area connection" &Chr(34) &" 10.35.50.19",1,True
                                                objShell.Run "netsh -r " &currentserver &" interface ip set dns name=" &chr(34) &"local area connection" &chr(34) &" 10.35.50.19 index=2",1,True                                
                                        End If
                                End If                          
                                counter = counter + 1
                        Next
                End If
        Next
Loop
 
%>
</body>
</html>

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:pr0t0c0l12
ID: 22847121
Make sure that the IIS in the server allows active server pages to run.  
0
 
LVL 1

Expert Comment

by:geekhelp4u
ID: 22847137
Try this...

open up "My Computer"

Click on "tools" and select "folder options"

click on "view"

scroll down to "Hide extensions for known file types" and make sure it is unchecked...

if it was checked, if the file is a .txt and you change it to a .vbs then it changes it to a .vbs.txt thus is still a .txt file.

with it unchecked, it will replace the .txt extension with the .vbs extension

you can also open the file now in note pad and select "file save as"

change the save as type from .txt to "All files"

save the file as name.vbs
0
 
LVL 7

Expert Comment

by:pr0t0c0l12
ID: 22847155
That will work too.  You can run both in a server to collect data from remotely or like geekhelp4u said to run in windows.  You can schedule a task and make it run what you are trying to do...

Good luck.
0
 

Author Comment

by:darovitz
ID: 22847231
I understand how to change the extension, I just don't know how to run it.  I  named it changedns.vbs and when I click it nothing happens. There is no text file in the C root either?

Thanks for explaining.
0
 
LVL 17

Accepted Solution

by:
Jared Luker earned 125 total points
ID: 22847355
WScript is probably your default scripting host.

If you go to a command prompt you can type:

cscript changedns.vbs

That script does not have any output, so it will not output anything to the screen while it runs.  

You can make cscript the default by typing:

cscript //h:cscript

That way if you double click on a script or run it from the command line it will run in a DOS box.

I would comment out that "on error resume next" on line 1 so that you can see if the script is erroring out.  If it is, then you can try to figure out how to fix the errors.  Then you should see the changes being made on your servers.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question