Miscellaneous Batch Scripting

@ In DOS version 3.3 and later, hides the echo of a batch command. Any output generated by the command is echoed. The at-sign can be prefixed to any DOS command, program name, or batch file name within a batch file.

@[command]
    examples @ {Seperates sections of the batch file without diplaying the DOS prompt.}

@echo OFF {Hides the echo off report.}
 
%DIGIT Replaceable batch parameters which are defined by the user when the batch is executed. The parameters are separated by spaces, commas, or semicolons.

%digit {Digit: any digit from 0 to 9. %0 has the value of the batch command as it appears on the command line when the batch is executed. %1 represents the first string typed after the batch commmand. Each occurrence of %digit is replaced by the corresponding string from the batch command line.}
    examples MYBATCH DOC A:
COPY *.%1 %2
{Copies all .DOC files in the default directory to drive A:}
 
%VARIABLE% Replaces the DOS environment variable name with its environment value.

%variable% {Variable: a string of uppercase characers in the environment associated with a string value. Variable is created in the environment by using SET.}
    examples %PATH% {Returns the value of PATH, the current search path, which is executable.}

echo %PATH% {Displays the value of PATH, the current search path.}

%PROMPT% {Returns the value of PROMPT, the current prompt string, which is executable.}

echo %PROMPT% {Displays the value of PROMPT, the current prompt string.}

echo The current search path is: %PATH% {Displays the message including the current search path.}

set USER=John
if %USER%= =John goto LABEL
{Since the value of USER does equal “John”, the control is transferred to the label, LABEL.}
 
CALL Loads and executes a batch file from within a batch file as if it were a external command. When a second batch file completes, control is returned to the calling file.

call [drive:][path]filename [batch-parameters]
Before DOS version 3.3:
command /c [drive:][path]filename [batch-parameters]
 
CLS Clears the video display screen, setting the cursor in the upper left-hand corner.

cls
 
ECHO Controls whether commands and comments within a batch file are displayed.

echo [ON|OFF|message|.]
    examples echo {Displays echo status}

echo ON {Restores normal display activity.}

echo OFF {Halts display of DOS prompt and commands.}

echo Processing… {Displays “Processing…” on the screen.}

echo %USER% {Displays the value of USER on the screen.}

echo. {Displays a single blank line on the screen.}

echo ^L > prn {Sends an ASCII control-code (form feed) to the printer. Press <Ctrl> plus <L> to type the ^L character.}

echo Y|Del *.* {Answers the DEL “Are you sure” question automatically.}
 
FOR Repeats the operation of a DOS command for each member of a list. Use CALL to execute a batch file as a command.

for %%argument in (list) do command {Argument: any letter from A to Z. List: a sequence of strings separated by spaces or commas. Wildcards are allowed.}
    examples for %%d in (A,C,D) do DIR %%d *.* {Displays the directories of drives A, C, and D sequentially.}

for %%f in (*.TXT *.BAT *.DOC) do TYPE %%f {Types the contents of all .TXT, .BAT, and .DOC files in the current default directory.}

for %%P in (%PATH%) do if exist %%P*.BAT COPY %%P*.BAT C:BAT {Copies all batch files which exist in any directory on the DOS command search path into the directory C:BAT.}

for %%f in (*.PAS) do call compile %%f {Compiles all .PAS files in the current default directory.}
 
GOTO Transfers control within a batch file to a line identified by a label. The label must be of the form “:LABEL“.

goto LABEL
:LABEL
 
IF Tests a condition and executes a command only if the condition is TRUE. But if the NOT modifier is present, the command will be executed only if the condition is FALSE.

if [not] condition command {Condition: errorlevel number; string1= =string2; or exist filename. Command: any DOS command, batch command, batch file name, or program name.}
    examples if [not] errorlevel number command {Errorlevel: an exit code returned by a program or an external command. The following DOS commands return an exit code: BACKUP, RESTORE, FORMAT, REPLACE, and XCOPY. Number: a numerical value (integer) against which the exit code is compared. The condition is TRUE if the exit code returned by the previous program is greater than or equal to number. The condition is FALSE if the exit code is less than number.}

BACKUP C:*.* A: /s
if errorlevel 3 goto TROUBLE
{If the BACKUP command exits with a code of 3 or higher, control will be transferred to the label TROUBLE.}

if errorlevel 3 if not errorlevel 4 echo ERROR #3 occurred
if errorlevel 4 if not errorlevel 5 echo ERROR #4 occurred
{Nested if statements that determine the exact error number.}
 

if [not] string1= =string2 command {The condition is TRUE if both strings are identical. The comparison is case sensitive. If either string is blank, a syntax error occurs.}

if (%1)= =(LTRS) CD C:WORDLTRS {If the first parameter is LTRS, the change directory to LTRS.}

if “%1″= =”” goto ERROR {If there is no parameter, then control is transferred to label ERROR.}

if not %2X= =X DIR %2*.* {If there is a second parameter, then display all the files contained in the directory %2.}

if not “%3″= =”” if not “%3″= =”b” if not “%3″= =”B” goto BADPARAM {If there is no third parameter or if it is anything other than b or B, then go to label BADPARAM.}
 

if [not] exist filename command {The condition is TRUE if filename can be located. The filename can include drive and path specifications. Wildcards are allowed.}

if exist D:%1nul CD %1 {Tests for the existence of directory %1 even if it contains no files, then changes to that directory if it exists.}

if not exist A:FLASH.EXE COPY C:PROJECTSFLASH.EXE A: {Copies FLASH.EXE to drive A, but only if it doesn’t exit there already.}
 
PAUSE Pauses the running of a batch file and displays the message “Press any key to continue …” on the screen. If the optional message is included, it will be displayed first. Use pause to optionally terminate the batch file with <Ctrl-Break> at a safe place. The optional message is not displayed when echo is OFF, so the message must be echoed on the preceding line.

pause [message]
    examples pause {Displays “Press any key to continue …”.}

pause < nul {Waits with no comment.}

pause Do you want to continue? {Displays “Do you want to continue?” with “Press any key to continue …” on the next line.}
 
REM Adds remarks to a batch file.

rem [remark]
    examples @rem {Hides the remark from display.}
 
SET Set will view the DOS environment or create, change, or delete environment values.

set [variable=[value]] {Variable: a string of characters, unbroken by spaces, which are converted to uppercase letters in the environment. Value: a string of characters, case specific, associated with variable.}
    examples set {Display the entire DOS environment.}

set USER=John {Sets the value of USER to the string, “John”.}

set USER= {Removes USER from the environment.}

set PATH=C:;C:DOS {Sets C:;C:DOS as the current search path.}

set PATH=%PATH%;C:TEST {Appends ;C:TEST to the current search path.}
 
SHIFT Shifts any parameter on the command line one position to the left. Use SHIFT to refer to multiple parameters by one name or to use more than ten parameters on a single command line.

shift
    examples :LOOP
COPY %1 A:
shift
if not (%1)==() goto LOOP
{Beginning with the first parameter, all the parameters listed on the command line are iterated and a file, the value of the parameter, is copied to A:.}
 
Miscellaneous

command > nul {Redirects command output to oblivion.}

command > file {Redirects command output to file.}

command >> file {Appends command output to file.}

command < file {Redirects file output to command.}

PATH {Displays “PATH=” followed by the value of PATH, the current search path.}

PATH directories {Sets directories as the current search path.}

PATH = directories {Sets directories as the current search path.}

PATH; {Disables extended command searching and confines the searching to the default directory.}

PROMPT {Resets the prompt string to its default, $n$g.}

CD {Displays the current directory and its path.}

. {Represents the default directory (If PATH=D:;C:SYS;C:. then current directory will be searched after D: and C:SYS).}

.. {Represents the parent of the default directory (C:TOOLSWPLTRS.DOC is the same as ..WPLTRS.DOC).}

%% {A literal “%”.}
 
Other Resources

Source

Manipulating Registry from a Batch file

Manipulating Registry from a Batch file

REGEDIT /S addsome.REG                Adds registry settings from a file

REGEDIT /E d:pathfilename.REG "HKEY_XXXXWhatever Key"   Exports a a registry hive to file

Example of a registry import file

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionWinlogon]
“AutoAdminLogon”=”1”
“DefaultDomainName”=”LABDOMAIN”
“DefaultUserName”=”user”
“DefaultPassword”=”password”

Deleting a registrykey

[-HKEY_CURRENT_USERDummyTree]      Deletes the entire tree DummyTree

[HKEY_CURRENT_USERDummyTree]       Deletes ValueToBeRemoved from DummyTree
“ValueToBeRemoved”=-

Source

Enable SSH

On Cisco Pix

hostname host
domain-name domain.se

ssh <ip> <mask> outside
ssh <ip> <mask> inside
ssh timeout 30

ca generate rsa key 1024
show ca mypubkey rsa
ca save all

On Cisco ASA

hostname host
domain-name domain.se

ssh <ip> <mask> outside
ssh <ip> <mask> inside
ssh timeout 30

crypto key generate rsa modulus 1024

FSMO Roles

FSMO (Flexible Single Master Operations)

Two good reasons for investigating FSMO

a) Curiosity to know how this Single master operation works

b) To know what to do if you lose one of the 5 vital roles.

Background information

For most Active Directory operations, Windows 2003 uses the multiple master model.  The benefit is you can add a computer, or change a user’s password on any domain controller.  For example, if you have three domain controllers, you can physically create a new computer in the NTDS.dit database on any of the three.  Five minutes later, the new computer object will be replicated to the other two domain controllers.

Technically, the multiple master model uses a change notification mechanism.  Occasionally problems arise with duplicate operations, and as a result orphaned objects appear in the ‘LostAndFound’ folder.  The point of FSMO is that a few operations are deemed so critical that only one domain controller can carry out that process.  Emulating a PDC is the most famous example of such a Single Master Operation; creating a new child domain would be another example.

In FSMO, the Flexible word simply means that you can move the role to a more suitable domain controller.

The five FSMO roles are:

  1. PDC Emulator – For NT 4.0 BDC’s.  But also for synchronizing time and creating group policies.
  2. RID Master – Each object must have a globally unique number.  The RID master makes sure each domain controller issues unique numbers when you create objects like users.
  3. Infrastructure Master – Responsible for checking Universal group membership in multiple domain forests.
  4. Domain Naming Master – Ensures that each child domain has a unique name.
  5. Schema Master – Operations that involve expanding user properties e.g. Exchange 2000 adds the mailbox property to users.

Three of the FSMO roles (1-3) are held in each domain, whilst two (4-5) are unique to the entire forest.

Changing the FSMO rolesFSMO Roles

RID, PDC, Infrastructure (1. 2. and 3.)

You can plan a switch of Operation Master by using the Change button in the diagram right, taken from Active Directory Users and Computers, Right Click Domain, Properties, Operations Masters.

Domain Naming Master (4.)

To see the Domain Naming Master (4), check out Active Directory Domains and Trusts, Operations Master..

Schema Master (5.)

The Schema Master (5) is the most difficult FSMO to find.

1) Register the Schema Snap with this command: RUN regsvr32 schmmgmt.dll;

2) Run MMC, Add Remove Snap-in, Add Active Directory Schema

3) Select Active Directory Schema, Right Click, Operations Master.

If you ever run DCPROMO to demote a domain controller, watch out for a check box that says ‘This is the last domain controller in the domain’.  If that box is UNchecked the wizard will automatically move any FSMO roles to another domain controller.

Troubleshooting

If you find problems with domain controller connections timing out then:

a) Check DNS settings on the TCP/IP properties.

b) Check that all the FSMO servers are up and running.

c) Try this need command using NetDom:
    netdom query fsmo.

d) If you need a copy of netdom, check here.

How to back up and restore http://companyweb data in Windows Small Business Server 2003

How to back up and restore http://companyweb data in Windows Small Business Server 2003

Article ID : 829112
Last Review : May 4, 2005
Revision : 5.0

SUMMARY

This article discusses how to back up a Windows SharePoint Services http://companyweb database in Microsoft Small Business Server 2003 and how to restore the database back to the original server. You can do the backup and the restore operations by using Microsoft Windows NT Backup or by using the Stsadm.exe command-line tool.

For these steps to work, you must meet the following criteria:

You must restore the database to the same server where you performed the backup operation.
The following Windows SharePoint Services database and log files must be on the server:

STS_Config.mdf
STS_Config_log.ldf
STS_Servername_1.mdf
STS_Servername_1_log.ldf

back to the top

Back up and restore a Windows SharePoint Services http://companyweb database by using Windows NT Backup

1. To back up the Windows SharePoint Services http://companyweb database by using Windows NT Backup, follow these steps:

a. Click Start, point to All Programs, point to Accessories, point to System Tools, and then click Backup.
b. In the Backup or Restore Wizard, click Advanced Mode, and then click the Backup tab.
c. Click to select the check box of the folder where the Windows SharePoint Services databases are installed. By default, this is Program FilesMicrosoft SQL ServerMSSQL$SharePointData.

Note This is the default installation folder for Windows SharePoint Services databases.

d. Specify a destination for the backup, and then click Start Backup.
2. To restore a Windows SharePoint Services http://companyweb database by using Windows NT Backup, follow these steps:

Note This action overwrites the existing Windows SharePoint Services databases. Microsoft recommends that you back up the current Program FilesMicrosoft SQL ServerMSSQL$SharePointData folder before you restore files.

a. Click Start, point to All Programs, point to Accessories, point to System Tools, and then click Backup.
b. In the Backup or Restore Wizard, click Advanced Mode.
c. Click the Restore and Manage Media tab.
d. Select the tape drive or other backup storage device that contains the http://companyweb database(s) that you backed up in step 1.
e. Click to select the check box of the folder where the Windows ShareP
oint Services databases are installed. By default, this is Program FilesMicrosoft SQL ServerMSSQL$SharePointData .
f. On the Tools menu, click Options.
g. Click the Restore tab, click to select the Always replace the file on my computer check box, and then click OK.

h. Verify that Restore Files to is set to Original location, and then click Start Restore. Click OK to start restoring your data.

Note After you click Start Restore, you can click Advanced in the Confirm Restore dialog box. Microsoft recommends that you do not make any changes to the default advanced restore options until you know that the restore was successful.

For more information about advanced restore options, visit the following Microsoft Web site:

i. When the restore is complete, click Close in the Restore Progress dialog box, and then click Yes to restart the server.

back to the top

Back up and restore a Windows SharePoint Services http://companyweb database by using the Stsadm.exe command-line tool

1. To back up a Windows SharePoint Services http://companyweb database by using Stsadm.exe, follow these steps:

a. Click Start, and then click Run.
b. In the Open: box, type cmd, and then click OK.
c. At the command prompt, type the following, and then press ENTER:

cd %programfiles%Common FilesMicrosoft Sharedweb server extensions60BIN
d. At the command prompt, type the following, where Backup_Path is the path of the backup file, and then press ENTER:

stsadm -o backup -url http://companyweb -filename Backup_Path

For example, if you type stsadm -o backup -url http://companyweb -filename c:backup.dat, you will back up http://companyweb to the Backup.dat file. This file is located in the root directory of drive C.

Note This command backs up the http://companyweb top-level Web site and its subwebs only. If you have additional top-level Web sites, you must use Stsadm.exe to back up each top-level site.

e. Type exit, and then press ENTER.
2. To restore a Windows SharePoint Services http://companyweb database by using Stsadm.exe, follow these steps:

a. Click Start, and then click Run.
b. In the Open: box, type cmd, and then click OK.
c. At the command prompt, type the following and then press ENTER:

cd %programfiles%Common FilesMicrosoft Sharedweb server extensions60BIN
d. At the command prompt, type the following, where Backup_Path is the path of the backup file, and then press ENTER:

stsadm -o restore -url http://companyweb -filename Backup_Path -overwrite

For example, if you type stsadm -o restore -url http://companyweb -filename c:backup.dat -overwrite, you will restore http://companyweb from the Backup.dat file. This file is located in the root directory of drive C.

e. Type exit, and then press ENTER.

Example script to create daily backups of the http://companyweb database

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and the tools that are used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, visit the following Microsoft Web site:

For additional information about the support options available from Microsoft, visit the following Microsoft Web site:

The following example script uses a numbered value for the day of the week. The script then appends this number to the name of the backup file that the script creates in the X:Backups location. To use this script, copy the following code to a Notepad file, and then customize the text string that specifies the backup file (x:backupscompanyweb). Next, save the Notepad file by using the file name Wssback.vbs, and then create a scheduled task to run this script daily.

dim dayweekdim strBackupdayweek = datepart("w",(date))strBackup = """C:Program FilesCommon FilesMicrosoft Sharedweb server extensions60BINstsadm""" + _"-o backup -url http://companyweb -filename x:backupscompanyweb" + cstr(dayweek) + ".dat -overwrite"wscript.echo "Backing up Companyweb"Set WshShell = WScript.CreateObject("WScript.Shell")Return = WshShell.Run(strBackup, 0, true)Set WshShell = Nothing

To add this script as a scheduled task, follow these steps:

1. Open Control Panel.
2. Double-click Scheduled Tasks.
3. On the File menu in Scheduled Tasks, point to New, and then click Scheduled Task.
4. Type a name for the scheduled task, such as Companyweb Backup.
5. Double-click the new task to open the task properties.
6. In the Run box, type cscript Drive:Wssback.vbs.

Note In this step, Drive is the location of the Wssback.vbs file.

7. Click Set password, type the password for the user account that the task will use in the Password box and in the Confirm password box, and then click OK.
8. Click the Schedule tab, configure the appropriate schedule, and then click OK.
9. To test the scheduled task, right-click the task, and then click Run. After the task runs, visit the location that you specified for the backup file to verify that the script created the file.

back to the top

REFERENCES

For more information about the Windows SharePoint Services Software Development Kit (SDK), visit the following Microsoft Web site:

For more information about the Windows SharePoint Services Administrator’s Guide, visit the following Microsoft Web site:

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

829113 How to restore a Windows SharePoint Services CompanyWeb database after the intranet component and SQL Server or MSDE 2000 have been removed on Windows Small Business Server

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

827701 How to perform a disaster recovery operation of SharePoint Services Companyweb and SharePoint Services Databases

back to the top


APPLIES TO
Microsoft Windows Small Business Server 2003 Premium Edition
Microsoft Windows Small Business Server 2003 Standard Edition
Microsoft Windows SharePoint Services
Keywords: 
kbhowtomaster kbbug KB829112

Link to this Article

Forcing GPO Refresh

Question: How can I force Group Policy to refresh on a Windows Server 2003 or Windows XP machine?

Answer:

To manually force Group Policy to refresh under Windows 2000, you use the command

secedit /refreshpolicy machine_policy/user_policy

Microsoft has replaced this command in Windows 2003 and XP with the command

gpupdate 

You can run this command without any switches to update both machine and user policies. When you run Gpupdate on Windows 2003, the machine will display the following text:

Refreshing Policy...User Policy Refresh has completed.Computer Policy Refresh has completed.To check for errors in policy processing, review the event log.

The last line doesn’t appear on XP machines. To update only the user command components, type

gpupdate /target:user

To load only the computer command components, type

gpupdate /target:computer

The optional switches that you can use with the Gpupdate command are

  • /Force. This switch loads all policy settings rather than just those that have changed.
  • /Wait:<time>. This switch specifies the amount of time to wait for the policy processing to finish before returning to the command prompt.
  • /Logoff. This switch causes the user to log off after Group Policy refreshes.
  • /Boot. This switch causes a reboot after Group Policy refreshes.
  • /Sync. This switch synchronously (i.e., in the background) applies the next boot or user logon policy (the system will prompt you to log off or reboot, depending on the /target setting).