Archive for the ‘Scripts’ Category

Posted by keith.wirch at November 3, 2016

Category: Batch, Quick Notes, Windows

Quick notes on working with Windows Services.  Windows Update will be our Guinea Pig.  Windows update service name is “wuauserv”.  You can get the service name of any service by getting the output of:

powershell get-service

Or if you prefer the GUI.  You can open the properties of a service via the services console.

Windows Update Service

Stopping and Disabling a Service at Startup

sc config wuauserv start= disabled
[options]
boot
system
auto
demand
disabled
delayed-auto</pre>
<pre>

Starting, Stopping, Checking Status of a Service

sc start wuauserv    #Start a Service
sc stop wuauserv     #Stop a Service
sc query wuauserv    #Check Status of a Service

 


 

Posted by keith.wirch at October 28, 2013

Category: Batch, Scripts

So we inherited this system from a department at work.  The department got downsized and all documentation was GONE!  When I say gone…. I mean there people are gone and everything that was documented for it.  We got no logins, no IP addresses, nothing.  It was a VMware environment so the benefits of physical server hacking was gone.

After a while we finally were able to get administrative access.  This was some wizardry by one of the other admins.  My part was discover what was on the network.  Network Discovery was turned off for security reasons.  So I used a hacking technique called a Ping Sweep .  This ping sweep allows us to fill our ARP table on the computer and see which computers responded to the arp request.  I’m not a fan of installing random software on servers and workstations so I wanted a script.  So my friend Brian and I got to work on this script.  Batch scripting is not typically my favorite but it works decently well.

@echo off
cls
@color 0A
echo
echo *********************
echo PING SWEEP
echo *********************
echo
for /l %%a in (0,1,255) do (
 for /l %%b in (1,1,255) do (
 start ping -n 1 192.168.%%a.%%b | find "Reply"
  echo 192.168.%%a.%%b
)

REM Used to break up ping so not to overload the workstation.
REM This is curently used for the 3rd octect in the IP Address. Change the variable or the number to change increments.

IF %%a EQU 5 pause
IF %%a EQU 10 pause
IF %%a EQU 15 pause
IF %%a EQU 20 pause
IF %%a EQU 25 pause
IF %%a EQU 30 pause
IF %%a EQU 35 pause
IF %%a EQU 40 pause
IF %%a EQU 45 pause
IF %%a EQU 50 pause
IF %%a EQU 55 pause
IF %%a EQU 60 pause
IF %%a EQU 65 pause
IF %%a EQU 70 pause
IF %%a EQU 75 pause
IF %%a EQU 80 pause
IF %%a EQU 85 pause
IF %%a EQU 90 pause
IF %%a EQU 95 pause

IF %%a EQU 100 pause
IF %%a EQU 105 pause
IF %%a EQU 110 pause
IF %%a EQU 115 pause
IF %%a EQU 120 pause
IF %%a EQU 125 pause
IF %%a EQU 130 pause
IF %%a EQU 135 pause
IF %%a EQU 140 pause
IF %%a EQU 145 pause
IF %%a EQU 150 pause
IF %%a EQU 155 pause
IF %%a EQU 160 pause
IF %%a EQU 165 pause
IF %%a EQU 170 pause
IF %%a EQU 175 pause
IF %%a EQU 180 pause
IF %%a EQU 185 pause
IF %%a EQU 190 pause
IF %%a EQU 195 pause

IF %%a EQU 200 pause
IF %%a EQU 205 pause
IF %%a EQU 210 pause
IF %%a EQU 215 pause
IF %%a EQU 220 pause
IF %%a EQU 225 pause
IF %%a EQU 230 pause
IF %%a EQU 235 pause
IF %%a EQU 240 pause
IF %%a EQU 245 pause
IF %%a EQU 250 pause
IF %%a EQU 255 pause
)

So because I like color in my scritps, Line 3 allows you to pick your color. I like Green. Enjoy!

The script will open multiple command windows with the sole purpose of running a Ping command.  You can remove the “start” command from line 11 and it will only ping one at a time.  This will be very slow but you will bog down the workstation so much.

Which brings me to the last part of the script.  (Lines 18-70)

****WARNING****
This script is VERY processor instensive by default.  Do not try to do anything else while this script is running.  It will slow everything to a crawl.

This is why the last part of the script exists.  Those IF states cause the script to hault waiting for you to continue.  This allows the worksation to have a stopping point to catch up.If these are not there the scipt will just constantly run and run until you have just ping all ranges in the Class B address.  Don’t kill your workstation.  Just modify the IF statements to fit your liking.

Line 11 is where you designate the first two octets of your class B.  This can be configured to a class C easily if you understand variables.  Comment if you need help doing that.

Posted by keith.wirch at October 16, 2013

Category: Powershell, Uncategorized

I find VBScript to be buggy and doesn’t flow very well for me.  I’m still decent at it but Powershell has been my scripting language of choice for a while.  Here is another little diddy I made out of Powershell.

I present to you…. File Delete!  Yea it’s a common thing you do I know but darn it, I don’t like using the admin share all the time to delete files off hundreds of computers.  I execute this script… kick back… watch the beauty of the text scroll up my screen and watch for any errors and investigate.  All while the user is using the computer and they never know!  Makes me feel like some sort of wizard… or ninja….

Ok, grown up time Keith.

So the script is similar to the rest.  Change the last line to your computers.txt file path.

Line 4 is then the file path you want to delete on your remote computer.  This script takes advantages of UNC paths for windows so when you enter this part in, just think of it as you would for a standard admin share.  Remember you will need access to the file location.  Powershell will use the credentials that was opened with your Powershell or Powershell ISE window.

Because I am also experimenting with using Log files, you’ll also need to make an area for the log file for this script.  Or you can delete that area.  I’m starting to like log file though in my scripts.

#MAIN
 function delete-remotefile {
      Process{
           $file = "\\$_\c$\path\deleted\folder\or\items"
           if (test-path $file)
           {
           echo "$_ File Exists"
           Remove-Item $file -force
           echo "$_ Files deleted"
           }
      }
 }

 # Reads list and pipes to function
 Get-Content C:\Scripts\FileDelete\Computers.txt | delete-remotefile >> C:\Scripts\FileDelete\Log.txt

Posted by keith.wirch at October 15, 2013

Category: Powershell

Small one but a good one.  This script will ping all hosts in a text file to see if they are up.  It’s great in the of case you want to see all of the stations in a classroom before you push an update or something.

Here it is.


$names = Get-Content "C:\Scripts\Computers.txt"

foreach ($name in $names) {

if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue ) {

Write-Host "$name is up" -ForegroundColor Magenta

}

else {

Write-Host "$name is down" -ForegroundColor Red

}

}

Line 1 is where you designate your text file of computers.  From there you just execute the powershell script and it will run something like the output below.  I’d reccomend making a bunch of “computer” files for each room you will routinly check like this and then only changeing line 1 to match what you need.

Again, some areas are blurred to protect the innocent!  MSPaint for the win my friends!

Capture

Enjoy!

Posted by keith.wirch at March 23, 2013

Category: Scripts, Visual Basic

Tags:

This is a script that can be used to push out files and folders to computer across a network using computer names located within a text file, one by one. The script takes the name from the file and turns it into \\\c$\path\to\destination\. It is executed through Visual Basic’s Scripting Host (VBS). It will output what is currently happening and the outcome of that computer. If it fails, it will continue on to the next computer.

For those of you who can read the comments decipher what to do, go for it. It’s quite simple. If you need further explanation, keep reading.

Const ForReading = 1
Const ForWriting = 2
Const OverwriteExisting = True

Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get list of Computers
Set objFile = objFSO.OpenTextFile("C:\Scripts\Computers.txt")
Set fso = CreateObject("Scripting.FileSystemObject")

' Put Errors in Text File
ErrorFile = "C:\Scripts\ErrorLog-FilePush.txt"
Set ErrorsFound = fso.OpenTextFile(ErrorFile, ForWriting, True)

'Keep going through the list until the end
Do Until objFile.AtEndOfStream
	strComputer = objFile.Readline
	WScript.Echo "Now working on " & strComputer
	strRemoteFile = "\\" & strComputer & "\c$\path\to\destination"
'To push folder make this "objFSO.CopyFolder"
'To push files make this "objFSO.CopyFile"
'Change filepath there were the source of the files are.
	objFSO.CopyFile "C:\path\to\source", strRemoteFile, OverwriteExisting
' If Computer is not found, goto next.  If is found, say yay!
	If Err = 0 Then
		Wscript.echo "Success.  Yay!"
'Otherwise print "Fail.  BOOO!!! , note it in ErrorFile and goto next"
	Else
		Wscript.echo "Fail.  BOOO!!!"
		ErrorsFound.WriteLine(strComputer & " Failed")
	End If
On Error Resume Next
Loop

ErrorsFound.WriteLine ("=======  Script Ended ========")

Step 1: Create a folder named “Scripts” in “C:\” This is decent place to keep scripts that you collect. We will be working from this scripts folder, so any more files you create can be placed here in the “Scripts” folder.

Step 2: Open notepad (or your favorite text editor like notepad++) and copy/paste the code from above. Save the file in your new folder.

Step 3: Make two more text files named “computers.txt” and “ErrorLog-FilePush.txt” and have them in the same folder.

Step 4: On line 18 change “path\to\destination” to the actual destination on your remote computers.

Step 5: On line 22 change “path\to\source” to your actual source file or folders.  Take a look at “objFSO.CopyFile”.  If you are pushing a whole folder to your destination, you need to change this too “objFSO.CopyFolder”.

Step 6: Once you have all that configured, open a command prompt in the C:\Scripts folder and execute the command “cscript FilePush.vbs” and the script will start.  Any errors that happen will show in ErrorLog-FilePush.txt.

The script should execute with something like this.  (Computer names are blocked out to protect the innocent.)

(Note: I didn’t put “cscript” infront of filepush.vbs because running cscript is default behavior of .vbs files on my workstation)

Capture

==========================================
TROUBLESHOOTING
==========================================

I’m getting File not found errors on lines 18 or 22!
Be sure you have the path correct.  I like copying and pasting from windows explorer.  When copying whole folders on like 22, you usually need to an extra backslash at the end.

I’m getting File not found errors on line 12!
This is because the script cannot find the error file to output errors too.  Make sure you created the files in Step 3 in the right place.

I’m getting access denied!
Make sure you have permissions on the remote coomputers.  The script will take your current permissions of the logged in user.  To use the permissions of another user, Shift+Right-Click “cmd” in the start menu and select “Run as different user”