The following scripts are simple enough to be run directly from the command line:
Restarting a service
for %a in (stop,start) do net %a "servicename"
This is about as easy a script you will get.
Get the MAC addresses of the Windows® computers on the domain
@echo off&(for /f "delims=\" %a in ('net view^|find "\\"') do for /f "tokens=4" %b in ('nbtstat -a %a^|find "MAC Address"') do echo %a %b)&echo on
Note the echo off and echo on at the beginning and end of the command. This gives us a clean output in the window. The rest of the command is enclosed in parenthesis for separation.
This could also be put in a batch file, but this demonstrates how easy it is to type useful commands when needed.
Scan a subnet for IP addresses and host names
This can be quite useful if you ever need a list of active hosts with their IP addresses on any subnet. The output is also in the correct format for a hosts file.
for /l %a in (1,1,254) do ping -n 1 -w 100 192.168.1.%a|find /i "reply"&&(for /f %b in ('nbtstat -a 192.168.1.%a^|find "<20>"') do echo 192.168.1.%a %b>>hostlist.txt)
*Note: if your network is slow you might consider changing the -w 100 switch (time-out) to a greater value.
Moving on, some simple scripts that need to be in a batch file:
Changing Internet Explorer settings
I use this script on my notebook computer to enable or disable the proxy settings in IE depending on where I am. If I am at home I don’t need a proxy, if I am in the office I do. I detect my location by checking my IP address. I know that the subnet in the office is 10.1.x.x, so if I find the string “10.1.” in the IPCONFIG output I need to enable the proxy settings. The settings are done via REGEDIT and .REG files.
Contents of setproxy.cmd
@echo off ipconfig|find /i "10.1." if errorlevel 1 ( regedit /s proxydisable.reg ) else ( regedit /s proxyenable.reg )
Contents of proxyenable.reg
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings] "ProxyEnable"=dword:00000001
Contents of proxydisable.reg
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings] "ProxyEnable"=dword:00000000
This is quite a simple, yet useful script. You could also do many other things with it like load applications that you only use at the office etc. etc. The script could also be expanded to allow for many different locations, each with it’s own subnet and proxy settings.
Checking for Terminal Server Shadow Settings
I did this script a couple of years back because I needed to know if any users could be shadowed in Terminal Services without being asked for permission (a big privacy issue). I wasn’t too enthusiastic about having to manually check each of the 2000+ user accounts for the setting, so I wrote a script to report any accounts that were in violation. I used the ADDUSERS utility from Microsoft® to extract the account information. I then had to manipulate the data file and add spaces to empty fields using MTR. Then I could use a FOR loop to look for the right string and report any findings.
Contents of shadowinf.cmd
@echo off echo This script lists users that can be shadowed without permission. echo Gathering user information, please wait.... addusers /d temp.csv /t %USERDOMAIN%>nul mtr -nb- temp.csv - "\r\r\n" = "\r\n" mtr -nb- temp.csv - "," = " ," for /f "tokens=1,23,24 delims=," %%a in (temp.csv) do ( if "%%a" == "[Global]" ( goto end ) else ( if not "%%a" == "[User]" if not "%%b" == "Remote control requires user's permission " echo %%bfor user %%a ) ) :end pause del temp.csv
You simply run the script and note the user accounts that have been reported. The output could also be piped to a file if needed. The output window looks something like this:
This script lists users that can be shadowed without permission. Gathering user information, please wait.... Remote control does not require user's permission for user jsmith Remote control does not require user's permission for user jdoe Press any key to continue . . .
That’s the last of my simple scripts for now. I may add more over time if I have any requests or if I have any very useful ones.
Please move on to the intermediate examples for some more complex scripts.