Solved

How do I create a script from a text file

Posted on 2008-10-30
8
208 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Make sure that the IIS in the server allows active server pages to run.  
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Expert Comment

by:geekhelp4u
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

18 Experts available now in Live!

Get 1:1 Help Now