Category Archives: BAT Files

Any code related to bat files and logonscripts

Windows 7 | Delete Offline File Cache CSC Folder

After carrying out a domain migration on a PC the “offline files cache” still retains a local cache for the previous domain.

Within “Offline Files” (Control Panel) there is an option to “Delete temporary files” but this does not remove the “All offline files” cache located in the “C:\Windows\CSC” folder

The workaround to fully remove these files (and start a new offline sync) is to add the “FormatDatabase” registry entry which forces deletion.

This command will add the registry entry. After which reboot the system and all offline files will be removed:

reg add HKLM\SYSTEM\CurrentControlSet\services\CSC\Parameters /v FormatDatabase /t REG_DWORD /d 1

Formatting Device | “diskpart”

Formatting a USB Flash Drive using the “diskpart” utility:

Diskpart also resolves issues with formatting within Windows GUI:

Error: “Format Cannot Run because the volume is in use by another process” & “Will not format – unknown capacity

CMD

diskpart
list volume
select volume X
clean
create partition primary
format fs=ntfs quick label=DATA
assign letter X

VMware | Enable SNMP

Although SNMP traps can be configured within Windows VMs installed on the VMWare host it can also be configured to report directly from ESXi. This configuration helps monitoring software such as Solarwinds or Spiceworks.

Configure ESXi SNMP via CLI (with Prompts):

Download/Install: VMware vSphere CLI 5.1

@echo off
echo Enter the FQDN of the host:
set /p hostname=
echo Enter the root password:
set /p password=
set user=root
"C:\Program Files (x86)\VMware\VMware vSphere CLI\bin\vicfg-snmp.pl" --server %hostname% --username %user% --password %password% -c ESX -t 200.200.200.200@161/ESX --enable
"C:\Program Files (x86)\VMware\VMware vSphere CLI\bin\vicfg-snmp.pl" --server %hostname% --username %user% --password %password% -show >> c:\ESXi_SNMP.log
pause

Continue reading

Lync 2013 Deployment

Excellent post on the deployment of Lync 2013 via Script: Unifiedme.co.uk

I customized the Lync installation using the Office /admin application (see above) and then pushed this out to users via script/GPO. This is a very basic script to check whether Lync is already installed, if not then will run the setup.exe using the custom installer in the “updates” folder.

REM Automated Installer for Lync 2013
REM Note the setup.exe uses a custom .MSP to install lync with settings, registry keys and product activation.
 
@echo off
if exist "C:\Program Files (x86)\Microsoft Office\Office15\lync.exe" goto quit
if exist "C:\Program Files\Microsoft Office\Office15\lync.exe" goto quit
if exist "C:\Program Files (x86)\Microsoft Lync\communicator.exe" goto quit
:install
\\server\Software\SW_DVD5_Lync_2013_32-BIT_X64_English_MLF_X18-54527\setup.exe
:quit
exit

I’d be interested to know if there is a much better way to do this as it really is very basic and doesn’t include error handling.

Adding “Trusted Sites” to IE10 | Regedit | BAT | GPO

I’ve been looking for a simple method to add trusted sites into Internet Explorer 10 since they removed the “Internet Explorer Maintenance” (“The Internet Explorer Maintenance (IEM) snap-in is replaced by the Internet Explorer 10 preference extension”) options from the Server 2012 GPO.

Unfortunately a number of the methods for “adding trusted sites” prevent the local user from being able to customise the list with any additional sites. The method below is the quickest I have found to add a site to the “trusted sites” whilst also allowing users customisation. The following will add “work.edwardsd.co.uk” to the trust sites list:

*.BAT File Method:

REM See http://technet.microsoft.com/en-us/library/cc732643.aspx for Reg Commands/Switches
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\edwardsd.co.uk\work" /v "http" /t REG_DWORD /d 00000002 /Y

*.REG Method:

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\edwardsd.co.uk\work]
"https"=dword:00000002

Note: The dword number represents the following values:

  • 1: Intranet sites
  • 2: Trusted Sites
  • 3: Internet
  • 4: Restricted Sites.
"https"=dword:00000001
"https"=dword:00000002
"https"=dword:00000003
"https"=dword:00000004

GPO Method:
The alternative option would be to use a GPO, usually my preference however I found this a little bit more complex so opted for the *.BAT option. This is a top site for GPO guides: GPO to configure IE zones sites (remember if you use this method the users can’t make changes themselves, but great for a controlled environment.

Delete folder/files | CL

Methods to delete files via command line: Normally I take ownership of the folder/files (takeown):

takeown /f c:\folder\location /r /d y
  • /r Performs a recursive operation on all files/folders.
  • /d Suppresses the confirmation prompt.
  • y Yes Take ownership of the directory.

Change Permission (icacls):

icacls c:\folder\location /grant administrators:F /t
  • /t Performs a recursive operation on all files/folders.

Delete Process (del): 

del c:\folder\location\*.* /s /q
  • /q Quiet mode, do not ask if ok to delete on global wildcard
  • /s Delete specified files from all sub-directories (DELTREE).
  • /f Force deleting of read-only files.

Delete Process (RD):

RD /S /Q c:\folder\location\
  •  /q Quiet mode, do not ask if OK to delete on global wildcard
  • /s Delete specified files from all sub-directories.

Saving Windows Disk Space | Removing “Installer\$PatchCache$”

Running out of the diskspace on C:\ I found the C:\WINDOWS\Installer\$PatchCache$ to be massive! I used JamSoftware “treesize” to view what was taking up the space.

From research this folder can be safely deleted, however you MUST NOT delete the top level “Installer” folder “C:\WINDOWS\Installer” (only the sub-folders inside it)

A quick method for deleting this directory is:

Stop “Windows Installer Service”

net stop msiserver

Run the “Remove Directory” command (/q Quiet: /q | Removes all directories and files: /s)

rmdir /q /s %WINDIR%\Installer\$PatchCache$

Start “Windows Installer Service”

net start msiserver

Reference: http://goo.gl/SW5M9

Robocopy (FileServer Migration)

I often use Robocopy when migrating/copying data from file servers.

When running the copy process to transfer data from Windows Server 2003 to Windows Server 2008 I usually run robocopy from the destination server (usually the newer server) in order that it will use version XP027 (5.1.10) There are a number of flaws in XP026 and also requires a seperate download Windows 2003 Resource Kit to obtain it.

This is the main syntax I use to perform a full copy.

robocopy D:\Source E:\Destination /MIR /R:1 /W:1 /COPYALL

Example, when copying from another server (using UNC):

robocopy \\myserver\e$\data E:\data /MIR /R:1 /W:1 /COPYALL

Although “/MIR” is mirroring the data, this will not actually copy the ACL permissions, therefore you will need to ensure that /COPYALL is included. I’ve copied a whole load of data before without including this only to find I have had to recopy to correct the ACL.

/COPYALL : Copy ALL file info (equivalent to /COPY:DATSOU) including timescamps, permissions, ACL, other attributes.
/MIR : MIRror a directory tree – equivalent to /PURGE plus all subfolders (/E)
/R:n : Number of Retries on failed copies – default is 1 million.
/W:n : Wait time between retries – default is 30 seconds.

Sometimes the copy may set the top level folder to “hidden” This can be un-hidden using the “attrib” command:

attrib -h -s E:\data

-s : Removes the system file attribute.
-h : Removes the hidden file attribute.

Continue reading

Re-registering VSS | FIXVSS08.BAT

I had a number of errors within VSS on Windows Server 2008 R2, these were stopping my backup jobs from running and causing different issues to occur, to resolve these I used the following script to reinitiate VSS settings. This was taken from the following article: Symantec

Note: This will disabled services if running on a live server, so best to complete out-of-hours.

Continue reading

Script using x86/x64 detection | BAT

@ECHO OFF
REM ###################### START SCRIPT ######################
if exist "C:\Program Files (x86)" goto end
REM x86 >> c:\somelog86file.txt
robocopy "C:\Program Files (x86)\Test" D:\Test >> c:\somelogfile.txt
exit
:end
REM x64 >> c:\somelog64file.txt
robocopy "C:\Program Files\Test" D:\Test >> c:\somelogfile.txt
exit

Extract/Install MSI from EXE | CL

Extract MSI from EXE via CMD Line:

myfile.exe /extract:D:\MyExtractFileLocation

Install MSI via CMD Line:

msiexec /i /myfile.msi /qn ACCEPTEULA=1

For Quiet Install use: /qn

Create a CustomPowerSave using PowerCFG (*.BAT)

This set of commands will create a new powersaving mode in XP with the name “CustomPowerSave” and set it as default. This has not been tested in Windows 7. You can drop this code below into a “CustomPowerSave.bat” file and run. This will also need some tweaking and testing to get working correctly.


POWERCFG /CREATE CustomPowerSave
POWERCFG /CHANGE CustomPowerSave /monitor-timeout-ac 5
POWERCFG /CHANGE CustomPowerSave /disk-timeout-ac 10
POWERCFG /CHANGE CustomPowerSave /standby-timeout-ac 20
POWERCFG /CHANGE CustomPowerSave /hibernate-timeout-ac 0

POWERCFG /GLOBALPOWERFLAG off /OPTION RESUMEPASSWORD
POWERCFG /SETACTIVE CustomPowerSave

Get switch options using:
cmd -> powercfg /?

Popup Test

I was using this to test GPOs were run at user login. if the popup displayed then i knew the object had been executed.

bat: START CMD /C "ECHO Popup Test && PAUSE"

exe: popup-test.exe

vbs (will call and execute the exe from the specified location):

Set wshShell = WScript.CreateObject ("WSCript.shell")
wshshell.run "%userprofile%\desktop\popup-test.exe", 6, True
set wshshell = nothing

Exchange *.Log File Removal (BAT)

It is BAD BAD practice to try and delete the exchange 2007 log files! It should not be done ever!

Running into space issues on exchange needed to remove 80,000+ *.log files. These would be removed by doing a backup, but without any space to run the backup there’s not a lot of options.

I ran this command to perform a removal of the log files in the mailbox DB, I had to use this script the job as the windows would crash as soon as multiple files were highlighted and selected for deletion:

  • CD /D E:\Exchange\Mailbox\First Storage Group
  • del /s *.txt

In order to move logs onto another drive use this command:

  • CD /D E:\Exchange\Mailbox\First Storage Group
  • move *.log “K:\Exchange_Logs_Move”

DO NOT DELETE / REMOVE THFOLLOWING FILES:

  • E00.chk
  • E00.log
  • Mailbox Database.edb

Adding Printer via Net Use LPT

Command Line:

NET USE LPT1: \\server\printername /persistent:yes

If this error occurs: “System error 85 – device name already in use” – Meaning the LPT port has already been mapped.

Check what LPT is in use:

NET USE

Change LPT1 to LPT2

Standard Logon Script (*.BAT)

@echo off
cls
REM #############################################################
NET TIME /DOMAIN /SET /Y
echo User: %username%=20
echo Computer: %computername%=20
date /T
time /T

REM Creates a Folder on the server based on the *username*

if not exist \\*server*\Users\%username% mkdir \\*server*\Users\%username%

REM copies a BAT file to the local PC which allows users to simple do
"start -> run -> ip" and displays there ip address in a dos window.

XCOPY \\*server*\NETLOGON\Logon_Software\ip.bat %systemroot%\system32 /y /i

BAT CMD for creating a share on the system:

net share Data="S:\Data" /remark:"Share on Server"

Static Routes (Add/Delete)

To add a static IP route:

  • Start -> Run -> “CMD”:
  • route add 192.168.xxx.xxx MASK 255.255.255.0  192.168.xxx.xxx /p

To remove a static IP route:

  • Start -> Run -> “CMD”:
  • route delete destination (ie: route delete 192.168.xxx.xxx)

where: destination specifies either an IP address or network name for the remote TCP/IP host computer or network.
For example, to delete a static route to the 10.0.0.0 network, you type the following at a command prompt:
route delete 10.0.0.0

To print static routes:

  • Start -> Run -> “CMD”:
  • route print

IP Display in Command Prompt (*.BAT)

For users that cant find the IP!! Add this bat file to the system32 folder, this allows users to simple run the file by “start -> run -> ip”

“ip.bat”
@echo off
:: Quick IP – Simply displays local IP address using “run -> ip”
:: Date: Sep 2010
:: Author: de
:: ===========//=============
ipconfig
pause

To automatically add the above file to the system32 during startup create another *.bat file and use this code. Remember for Windows7 they will need admin privileges to access the system32 folder.

“Loginscript.bat”
XCOPY **ServerDomain***NETLOGONLogon_Softwareip.bat %systemroot%system32 /y /i