?
Solved

How do I create a script from a text file

Posted on 2008-10-30
8
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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