Solved

How do I create a script from a text file

Posted on 2008-10-30
8
219 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
 
LVL 7

Expert Comment

by:pr0t0c0l12
ID: 22847121
Make sure that the IIS in the server allows active server pages to run.  
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

861 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now