Solved

How do I create a script from a text file

Posted on 2008-10-30
8
226 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
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 …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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