Showing posts with label Batch Scripts. Show all posts
Showing posts with label Batch Scripts. Show all posts

Wednesday, 13 February 2013

Migrate Active Directory Home Folder from Windows Server 2003 to 2008


 Copy all the data across to new server using xcopy


log onto the file server where all home folder resides and run:

xcopy souce dectination /I /C /E /Y

(remember ICEY for switches )

 /I           If destination does not exist and copying more than one file,
             assumes that destination must be a directory.

 /C           Continues copying even if errors occur.
 /E           Copies directories and subdirectories, including empty ones.
 /Y           Suppresses prompting to confirm you want to overwrite an



Get usernames of staff on current file server to be migrated from Active Directory Home Folder Path filter

dsquery * dc=testdomain,dc=com -filter "(&(objectCategory=Person)(objectClass=User)(HomeDirectory=\5c\5cFILEServer1\5cHomeFolder\5c*))" -attr samAccountName > report.txt

 (5c tells dsquery that '\' is not part of the command and string instead)

Modify the Active Directory Home Folder Path


create a batch file:



for /f "eol=; tokens=1" %%i in (
report.txt) do call :PROCESS %%i

goto :EOF


:PROCESS
set var=%*
 

dsquery * dc=testdomain,dc=com -filter "(&(objectCategory=Person)(objectClass=User)(HomeDirectory=\5c\5cFILEServer1\5cHomeFolder\5c%var%))" | dsmod user -hmdir \\FILEServer2\HomeFolder\%var%\

:Exit



Thursday, 24 January 2013

Auto delete files older than x number of days

Following script is ideal where you want to clear the space from backup repository and keep files created in last x number of day, I've been utilizing this to delete all SQL backup files *.bak from backup repository D:\foldername where anything older than 6 days should be deleted.

Create a batch file  Script.bat and Add it the Scheduled Tasks to run daily/weekly/monthly


forfiles /p "D:\foldername" /m *.bak /s /d -6 /c "cmd /c del @PATH"


Switches used:

/P    pathname
/M    searchmask
/S   recuse and check all the subdirectories
/D    date
Selects files with a last modified date greater than or equal to (+), or less than or equal to (-), the specified date using the "dd/MM/yyyy" format; or selects files with a last modified date greater than or equal to (+) the current date plus "dd" days, or less than or equal to (-) the current date minus "dd" days. A valid "dd"  number of days can be any number in the range of 0 - 32768. "+" is taken as default sign if not specified.


/C    command       Indicates the command to execute for each file.
                    Command strings should be wrapped in double quotes.

                    The default command is "cmd /c echo @file".

                    The following variables can be used in the command string:


                    @file    - returns the name of the file.
                    @fname   - returns the file name without extension.
                    @ext     - returns only the extension of the file.
                    @path    - returns the full path of the file.
                    @relpath - returns the relative path of the file.
                    @isdir   - returns "TRUE" if a file type is a directory, and "FALSE" for files.
                    @fsize   - returns the size of the file in bytes.
                    @fdate   - returns the last modified date of the file.
                    @ftime   - returns the last modified time of the file.

Internal CMD.exe commands should be preceded with "cmd /c".

Tuesday, 22 January 2013

Active Directory Health Check Script

netdom query fsmo > C:\temp\HealthCheck_FSMO_roles.txt
Dcdiag.exe /v > c:\temp\HealthCheck_dcdiag.txt
Dcdiag.exe /q > c:\temp\HealthCheck_dcdiag_erroronly.txt
Netdiag.exe /v > c:\temp\HealthCheck_Netdiag.txt
Netsh dhcp show server > c:\temp\HealthCheck_dhcp.txt
Repadmin /showreps > c:\temp\HealthCheck_rep_partners.txt
repadmin /replsum /errorsonly > c:\temp\HealthCheck_rep_erroronly.txt

Wednesday, 16 January 2013

Automate FTP file copy - Batch Script

To Automate FTP file copy create two files, Import.bat and Script.txt rename to scr when finished editing (Script.scr)

Import.bat

REM @echo off
cd\
C:\



del importedfile
ftp -s:
Script.scr
ping 127.0.0.1 -n 4 -w 1000 REM use ping to delay the copy of ftp file
xcopy C:\
importedfile   \\destination\share   /Y


Script.scr

open ftpsite
ftp_username
ftp_password
lcd "C:\"
cd ftp_folder
get
importedfile
bye

Run Import.bat to fetch importedfile to C: from ftpsite

SQL Flat File Daily Backup Job and Daily Checks script

Backup of MS SQL 2005/2008 Databases from SQL Maintenace job is way easier and quicker when it comes to restoring compared to BackupExec or similar products. 
Also SQL Agent maintains the log of scheduled maintenance task, successes and failures etc.

Following are the steps for creating automated SQL Backup Maintenance job and Batch script to check if your backup has been successful and drop you an email on daily basis
 

Create SQL Backup Maintenance Job
http://www.freetutorialssubmit.com/create-ms-sql-backup-maintenance-plan/1585

 
 Verify backup path and Backup file format


 

Create Batch script to monitor the Backup folders

Create a batch file sqlcheck.bat
  @echo off
FOR /F "TOKENS=1,2 DELIMS=/ " %%A IN ('DATE /T') DO SET mm=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('DATE /T') DO SET yyyy=%%B
FOR /F "TOKENS=1 DELIMS=/ " %%A IN ('DATE /T') DO SET dd=%%A

set efin=%mm%%dd%%yyyy%
if %dd% LSS 10 set dd=%dd:~-1%
set /A dd=%dd%-1
IF %dd% LSS 10 set dd=0%dd%


if %dd% equ 00 if %mm% equ 01 (
Set dd=31
Set mm=12

)

if %dd% equ 00 if %mm% equ 02 (
Set dd=31
Set mm=01

)

if %dd% equ 00 if %mm% equ 03 (
Set dd=28
Set mm=02

)

if %dd% equ 00 if %mm% equ 04 (
Set dd=31
Set mm=03

)
if %dd% equ 00 if %mm% equ 05 (
Set dd=30
Set mm=04

)
if %dd% equ 00 if %mm% equ 06 (
Set dd=31
Set mm=05

)
if %dd% equ 00 if %mm% equ 07 (
Set dd=30
Set mm=06


)
if %dd% equ 00 if %mm% equ 08 (
Set dd=31
Set mm=07

)

if %dd% equ 00 if %mm% equ 09 (
Set dd=31
Set mm=08

)

if %dd% equ 00 if %mm% equ 10 (
Set dd=30
Set mm=09

)

if %dd% equ 00 if %mm% equ 11 (
Set dd=31
Set mm=10

)

if %dd% equ 00 if %mm% equ 12 (
Set dd=30
Set mm=11

)


set ymd=%yyyy%%mm%%dd%

REM Backup file format Northwind20121217.bak

set y_m_d=%yyyy%_%mm%_%dd%
 
REM Backup file format Northwind_2012_12_17.bak
 

:END

REM Examples
dir \\RepositoryServer\d$\backup\*.bak |find "/"
dir \\
RepositoryServer\Backup\DatabaseName\*.bak | find "/"
dir \\
RepositoryServer\Backup\DatabaseName\*%ymd%*1400.bak | find "/"
dir \\
RepositoryServer\Backup\DatabaseName\*%ymd%*.bak | find "/"
dir \\RepositoryServer\Backup\DatabaseName\*%y_m_d%*1400.bak | find "/"

Schedule to run daily and email the report



download BMAIL.exe to email the report


Create second batch file sqlreport.bat to count number of lines to ensure all the databases are backed up and add it to Windows Scheduled Task:


sqlcheck.bat > sqlcheck.txt

REM following will count no of Databases backed up and add on the bottom of the txt file
for /f "tokens=* delims= " %%t in ('dir/b sqlcheck.txt') do (
for /f "tokens=* delims= " %%a in (%%t) do (
set /a N+=1
)
)
@echo ****************************** >> "SQLCheck.txt"
echo Total Backedup files count = !N! >>"SQLCheck.txt"
 

REM 120 are the number of databases my script was checking daily
Set /A p=120-%N%
echo Flat Files missed = %p% >>"SQLCheck.txt"


bmail -s smtpservername -t emailaddress@domain.com -f sendersaddress@domain.com-h -a "SQLCheck" -m SQLCheck.txt -c




Thursday, 10 January 2013

Common problems when Setting Up vCenter 5.1 Server Appliance

Configure Hostname, IP, Gateway, DNS and proxy for the appliance

login on the command line and run

/opt/vmware/share/vami/vami_config_net

Follow the menu to complete the steup
login console 

Unable to browse vCenter Server Appliance on Internet Explorer

 The first website to visit is the appliance web console accessible on https://vcsa-address:5480 as indicated in the console screen. IE6,7,8 was not able to open this website, but the latest Firefox 15 and IE9 had no issues when logging on with the default root password of vmware.
username: root  password: vmware

Friday, 14 December 2012

Delete Temp internet and other files from XP Desktop - Batch Script

rem @ECHO OFF
SET SRC1=C:\Documents and Settings
SET SRC2=Local Settings\Temporary Internet Files\
SET SRC3=Local Settings\History
SET SRC4=Local Settings\Temp
SET SRC5=Recent
echo About to delete files from Internet Explorer "Temporary Internet files"
FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC2%\*.*") DO RMDIR /S /Q "%%Y"
echo About to delete files from Internet Explorer "History"
FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC3%\*.*") DO RMDIR /S /Q "%%Y"
FOR /D %%X IN ("%SRC1%\*") DO FOR %%Y IN ("%%X\%SRC3%\*.*") DO DEL /F /S /Q "%%Y"
echo About to delete files from "Local settings\temp"
FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC4%\*.*") DO RMDIR /S /Q "%%Y"
FOR /D %%X IN ("%SRC1%\*") DO FOR %%Y IN ("%%X\%SRC4%\*.*") DO DEL /F /S /Q "%%Y"
echo About to delete files from "Recent" i.e. what appears in Start/Documents/My Documents
FOR /D %%X IN ("%SRC1%\*") DO FOR %%Y IN ("%%X\%SRC5%\*.lnk") DO DEL /F /S /Q "%%Y"
echo About to delete files from "Windows\Temp"
cd /d %SystemRoot%\temp
del /F /Q *.*
@echo Y|RD /S ""

Thursday, 13 December 2012

Uninstall Windows Patches with Batch Script




Example of Script

for /f "eol=; tokens=1" %%i in (C:\KBNames.txt) do call :PROCESS %%i

goto :EOF

:PROCESS
set KB=%*
start /wait %windir%\$NtUninstall%KB%$\spuninst\spuninst.exe /quiet /norestart
:Exit


Example of KBNames.txt

KB1234567
KB2345678
KB1234567
KB2345678
KB1234567
KB2345678

Monday, 26 November 2012

Terminal Service session not ending after application closes

Create a batch file and start the RDP connection program to run the batch file on startup



@echo off
start /wait [Notepad.exe] (without brackets)

logoff