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




Tuesday, 15 January 2013

PowerShell Posters

PowerShell Poster for Beginners


 
 http://ramblingcookiemonster.files.wordpress.com/2012/09/powershell-basic-cheat-sheet2.pdf

PowerShell Poster for Advanced Users


http://ramblingcookiemonster.files.wordpress.com/2012/09/powershell-cheat-sheet.pdf

PowerShell Poster for VMWare PowerCLI 5.1


http://blogs.vmware.com/vipowershell/files/2012/09/PowerCLI_5_1_Poster.pdf

PowerShell Poster for Exchange 2010
 
 http://blogs.technet.com/b/matabra/archive/2011/10/21/powershell-for-exchange-2010-cheat-sheet.aspx



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

Mastering icacls.exe in 15 mins



http://www.bohack.com/wp-content/uploads/2009/12/icacls-gui.jpg


 icacls [path] [TABLE 1] [TABLE 2] domain\user:[TABLE 3] [TABLE 4]
[TABLE 5]

examples
icacls E:\Home Directories\%userDir%" /grant:r "MYDOMAIN\%userDir%":(OI)(CI)F
icacls Sales_Folder /grant FileAdmins:(D,WDAC)
icacls * /grant accountName:(NP)(RX) /T
-----------------------------------------


TABLE 1

/grant will grant user permissions or the add option in the GUI.
/remove will remove the user from the DACL and is equivalent to
 the remove option in the GUI.

Note: /remove:g will remove only grants for the user 
      and /remove:d will remove only denys for the user.

/deny is equivalent to the deny column in the GUI 
 and should always be used cautiously.
/setowner is a handy little command which allows you to change 
 the ownership of a file or folder to another user; 
 this is similar to the linux chown command.
/findsid which finds entries in a DACL for a specified SID or user.
/setintegritylevel is a bit out of the scope of this article 
 and may be in a future article.


-----------------------------------------
TABLE 2
/inheritance:e to enable same as a check box in the GUI.
/inheritance:d to disable and copy the permissions 
 from the parent same as the GUI.
/inheritance:r to disable and remove the 
 current permissions again same as the GUI.
-----------------------------------------
TABLE 3
This folder only
This folder, subfolders and files (OI)(CI)
This folder and subfolders (CI)
This folder and files (OI)
Subfolders and files only (OI)(CI)(NP)(IO)
Subfolders only (CI)(IO)
Files only (OI)(IO)
-----------------------------------------
TABLE 4
Simple Permissions

Full Control (F)
Modify (M)
Read & Execute (RX)
List Folder Contents (X,RD,RA,REA,RC)
Read (R)
Write (W)

Advanced Permissions

Full Control (F)
Traverse folder / execute file (X)
List folder / read data (RD)
Read attributes (RA)
Read extended attributes (REA)
Create file / write data (WD)
Create folders / append data (AD)
Write attributes (WA)
Write extended attributes (WEA)
Delete subfolders and files (DC)
Delete (D)
Read permissions (RC)
Change permissions (WDAC)
Take ownership (WO)
-------------------------------------------
TABLE 5
 /T  Traverse all subfolders to match files/directories. 
   
   /C  Continue on file errors (access denied)  Error messages are still displayed.
  
   /L  Perform the operation on a symbolic link itself, not its target.

   /Q  Quiet - supress success messages.

Friday, 4 January 2013

Get Server Serial Number remotely - Powershell & VBScript

Powershell

Get-WMIObject -Class "Win32_BIOS" -Computer computername | select SerialNumber 


VBSCRIPT

strComputer = "computername"
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colBIOS = objWMIService.ExecQuery _
    ("Select * from Win32_BIOS")
For each objBIOS in colBIOS
    Wscript.Echo "Manufacturer: " & objBIOS.Manufacturer
    Wscript.Echo "Serial Number: " & objBIOS.SerialNumber
Next

  

Thursday, 3 January 2013

Powershell XCopy equivalent

$dests=Get-Content -Path e:\dest.csv
$srcs=Get-Content -Path e:\src.csv
 foreach($src in $srcs)

{
   $x=0
   #Write-Host $srcs[$x] $dests[$x]
   Copy-Item $srcs[$x] $dests[$x] -Recurse -Force -Verbose
   $x=$x+1
}


dest.csv
\\server\d$\abc\
\\server\d$\xyz\

src.csv
\\fileserver\Users\abc\*
\\fileserver\Users\xyz\*

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 ""

Get disk space with Powershell

$computername='MyComputerName'

Get-WmiObject win32_logicaldisk -ComputerName $computername -Filter {freespace>0} | Format-Table @{l='Computername';e={$computername}},@{l='Drive';e={$_.DeviceID}},@{l='Size';e={(($_.Size/1024)/1024)/1024}}, @{l='FreeSpace';e={(($_.FreeSpace/1024)/1024)/1024}}

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