Category Archives: System Management

Execute Batch File Commands During Specified Hours

OK. You want to run a command a certain way if it is between 9 AM and 12 PM. You want to run it a different way if it is between 12 PM and 3 PM. How do you accomplish this with a batch file?

The answer is nested IF statements and the %time% system variable. The %time% variable stores the time in a 24 hour format including hour, minute, second and millisecond:

09:02:45.80

This represents 9:02 AM at 45.80 seconds into the minute. I prefer to work with just the hour in situations like this. So I create a variable called %hour% for such batch files using the following command:

SET “hour=%time:~0,2%”

The following shows this operation at the Windows command line:

Extracting the hour from the time variable

It is also important to know that you have operator keywords used with the IF statements. They are as follows:

  • EQU – equal
  • NEQ – not equal
  • LSS – less than
  • LEQ – less than or equal
  • GTR – greater than
  • GEQ – greater than or equal

Using these keywords can be simpler to read than the traditional == or != operator types for the uninitiated who read your batch file.

With this in mind, the following batch file code will allow you to test for the hour range and run commands only during specified hours:

@ECHO OFF
SET hour=%time:~0,2%
IF %hour% GEQ 9 IF %hour% LEQ 12 (GOTO RUNIT)

ECHO Wrong time to run!
GOTO END

:RUNIT
ECHO Right time to run!

:END

Hopefully this helps you in such scenarios.

Execute a Batch File Only on A Specific Day

Have you ever wanted to run a batch file on a specific day, week, month, hour or minute? If so, you can do this with the Windows command line using the %date% or %time% system (environment) variables. To see the contents of these variables, execute the ECHO %time% and ECHO %date% commands at the Windows command line. The following screen shows the data stored in %date% and %time%.

ECHO DATA and TIME Variable

The ~ operator can be used to select a substring from such a variable. Notice that the day of the month is the 8th and 9th characters of the %date% contents at all times. Therefore you can go to the 7th character and then grab the next two and know that you’ve always gotten the day of the month (with days 1-9 represented as 01, 02, 03 …, 09).

With this information, considering the following batch file that uses the IF command and takes advantage of the ~ operator within the %date% variable:

IF %date:~7,2%==13 (GOTO :run_it) else (GOTO :no_run)

:run_it
CLS
REM run whatever command
ECHO Running the command...
DIR C:\
GOTO END

:no_run
REM Notify user that it is the wrong day
ECHO Wrong day!

:END

If this batch file is executed on a 13th day of any month, the DIR C:\ command will be executed. Otherwise, the notification of “Wrong day!” will be displayed for the user. From this example, you should be able to work out methods for determining the day of the month, month of the year and even hour of the day or minute of the hour.

Perfmon

The Perfmon command allows you to start the Windows Reliability and Performance Monitor in a specific standalone mode. Perfmon, also known as “Performance Monitor”, got its roots as a command called, “sysmon.exe” in Windows 95, 98 and XP. The perfmon command is used to view information about a systems; CPU, Disk, Network and Memory statistics.

Syntax (view syntax formatting guide):

perfmon </res|report|rel|sys>

Syntax Explanation

  • /res: Start Resource View.
  • /report: Start the System Diagnostics Data Collector Set and display a report of the results.
  • /rel: Start Reliability Monitor.
  • /sys: Start Performance Monitor.

Explanation with Examples

To view the performance monitor in a standalone view:

c:perfmon

To view the resource monitor in a standalone view:

c:perfmon /res

To view the reliability monitor built into Windows Vista and 7:

c:perfmon /rel

 

Pentnt

The Pentnt Command assists in the detection floating point division error (if present) in the Pentium chipset. When used with the –f option, pentnt enables forced emulation. When used with the –o options, pentnt disables forced emulation and turns on floating point hardware.

Syntax (view syntax formatting guide):

pentnt [-c] [-f] [-o]

Syntax Explanation

  • -c: Enables conditional emulation.
  • -f: Enables forced emulation.
  • -o: Disables forced emulation and re-enables floating-point hardware if it is present.
  • /?: Displays help at the command prompt.

Parameters used with this command must be prefixed with (hyphen) rather than / (slash).

Using the -c command-line option 

Floating-point emulation is forced on only if the system detects the Pentium processor floating-point division error at start time. If you select this parameter, you must restart the computer for the changes to take effect.

Using the -f command-line option Floating-point hardware is disabled and floating-point emulation is always forced on, regardless of whether the system exhibits the Pentium processor floating-point division error. This parameter is useful for testing software emulators and for working around floating-point hardware defects known to the operating system. If you select this parameter, you must restart the computer for the changes to take effect.

Using the -o command-line option If you select this parameter, you must restart the computer for the changes to take effect.

Explanation with Example

To enable floating-point emulation regardless of whether or not there is a floating-point division error:

pentnt –f

To disable forced emulation and re-enable floating-point hardware if it is present:

pentnt -o

 

Path

The Path command sets the command path in the PATH environment variable (the set of directories used to search for executable files). If used without parameters, path displays the current command path.

Syntax (view syntax formatting guide):

path [[<Drive>:]<Path>[;...][;%PATH%]]
path ;

Syntax Explanation

  • [Drive:]Path: Specifies the drive and directory to set in the command path.
  • [;]: Separates directories in the command path. If used without other parameters, ; clears the existing command paths from the PATH environment variable and directs Cmd.exe to search only in the current directory.
  • %PATH%: Appends the command path to the existing set of directories listed in the PATH environment variable.
  • [/?]: Displays help at the command prompt.

When you include %PATH% in the syntax, Cmd.exe replaces it with the command path values found in the PATH environment variable, eliminating the need to manually enter these values at the command prompt.

The current directory is always searched before the directories specified in the command path.

You might have files in a directory that share the same file name but have different extensions. For example, you might have a file named Accnt.com that starts an accounting program and another file named Accnt.bat that connects your server to the accounting system network. 
The Windows operating system searches for a file by using default file name extensions in the following order of precedence: .exe, .com, .bat, and .cmd. To run Accnt.bat when Accnt.com exists in the same directory, you must include the .bat extension at the command prompt.

If two or more files in the command path have the same file name and extension, path first searches for the specified file name in the current directory. Then it searches the directories in the command path in the order that they are listed in the PATH environment variable.

If you place the path command in your Autoexec.nt file, the Windows operating system automatically appends the specified MS-DOS subsystem search path every time you log on to your computer. Cmd.exe does not use the Autoexec.nt file.

When started from a shortcut, Cmd.exe inherits the environment variables set in My Computer/Properties/Advanced/Environment.

Explanation with Examples

To search the paths C:UserTaxes, B:UserInvest, and B:Bin for external commands:

path c:usertaxes;b:userinvest;b:bin

 

 

 

Ntbackup

The ntbackup command is a built-in backup utility that allows you to backup and restore full, differential and incremental backups of critical system files and data. Ntbackup was introduced in Windows NT in 1997 and is supported on Windows NT, 2000, XP and Windows 2003. Ntbackup is not available in Windows Vista or Windows Server 2008. Instead, you should use the wbadmin command and subcommands to back up and restore your computer and files from a command prompt.

It is important to note that you cannot recover backups that you created with ntbackup by using wbadmin. However, a version of ntbackup is available as a download for Windows Server 2008 and Windows Vista users who want to recover backups that they created using ntbackup. This downloadable version of ntbackup enables you to perform recoveries only of legacy backups, and it cannot be used on computers running Windows Server 2008 or Windows Vista to create new backups. To download this version of ntbackup, see http://go.microsoft.com/fwlink/?LinkId=82917.

 

NSLOOKUP Command Line

The Nslookup command line command allows you to displays information that you can use to diagnose Domain Name System (DNS) infrastructure. One thing you should note, before using this tool, you should be familiar with how DNS works. The Nslookup command prompt tool is available only if you have installed the TCP/IP protocol. Nslookup can be used directly from the command prompt or as a shell. If you run Nslookup with no parameters, it will enter shell mode, where you can type further commands as shown in the following image:

This image shows NSLOOKUP in interactive or shell mode. The shell mode commands are seen with the ? command.

The following syntax rules apply.

Syntax (view syntax formatting guide):

Nslookup [<-SubCommand ...>] [{<ComputerToFind> | -<Server>}]
Nslookup /exit
Nslookup /finger [<UserName>] [{[>] <FileName>|[>>] <FileName>}]
Nslookup /{help | ?}
Nslookup /ls [<Option>] <DNSDomain> [{[>] <FileName>|[>>] <FileName>}]
Nslookup /lserver <DNSDomain>
Nslookup /root
Nslookup /server <DNSDomain>
Nslookup /set <KeyWord>[=<Value>]
Nslookup /set all
Nslookup /set class=<Class>
Nslookup /set [no]d2
Nslookup /set [no]debug
Nslookup /set [no]defname
Nslookup /set domain=<DomainName>
Nslookup /set [no]ignore
Nslookup /set port=<Port>
Nslookup /set querytype=<ResourceRecordType>
Nslookup /set [no]recurse
Nslookup /set retry=<Number>
Nslookup /set root=<RootServer>
Nslookup /set [no]search
Nslookup /set srchlist=<DomainName>[/...]
Nslookup /set timeout=<Number>
Nslookup /set type=<ResourceRecordType>
Nslookup /set [no]vc
Nslookup /view <FileName>

Syntax Explanation

  • Nslookup /exit: Exits nslookup.
  • Nslookup /finger: Connects with the finger server on the current computer.
  • Nslookup /help: Displays a short summary of nslookup subcommands.
  • Nslookup /ls: Lists information for a DNS domain.
  • Nslookup /lserver: Changes the default server to the specified DNS domain.
  • Nslookup /root: Changes the default server to the server for the root of the DNS domain name space.
  • Nslookup /server: Changes the default server to the specified DNS domain.
  • Nslookup /set: Changes configuration settings that affect how lookups function.
  • Nslookup /set all: Prints the current values of the configuration settings.
  • Nslookup /set class: Changes the query class. The class specifies the protocol group of the information.
  • Nslookup /set d2: Turns exhaustive Debugging Mode on or off. All fields of every packet are printed.
  • Nslookup /set debug: Turns Debugging Mode on or off.
  • Nslookup /set defname: Appends the default DNS domain name to a single component lookup request. A single component is a component that contains no periods.
  • Nslookup /set domain: Changes the default DNS domain name to the name specified.
  • Nslookup /set ignore: Ignores packet truncation errors.
  • Nslookup /set port: Changes the default TCP/UDP DNS name server port to the value specified.
  • Nslookup /set querytype: Changes the resource record type for the query.
  • Nslookup /set recurse: Tells the DNS name server to query other servers if it does not have the information.
  • Nslookup /set retry: Sets the number of retries.
  • Nslookup /set root: Changes the name of the root server used for queries.
  • Nslookup /set search: Appends the DNS domain names in the DNS domain search list to the request until an answer is received. This applies when the set and the lookup request contain at least one period, but do not end with a trailing period.
  • Nslookup /set srchlist: Changes the default DNS domain name and search list.
  • Nslookup /set timeout: Changes the initial number of seconds to wait for a reply to a request.
  • Nslookup /set type: Changes the resource record type for the query.
  • Nslookup /set vc: Specifies to use or not use a virtual circuit when sending requests to the server.
  • Nslookup /view: Sorts and lists the output of the previous ls subcommand or commands.

Explanation with Examples

If ComputerToFind is an IP address and the query is for an A or PTR resource record type, the name of the computer is returned. If ComputerToFind is a name and does not have a trailing period, the default DNS domain name is appended to the name. This behavior depends on the state of the following set subcommands: domain, srchlist, defname, and search.

 

If you type a hyphen (-) instead of ComputerToFind, the command prompt changes to nslookup interactive mode.

 

The command-line length must be less than 256 characters.

Nslookup has two modes: interactive and noninteractive. 
If you need to look up only a single piece of data, use noninteractive mode. For the first parameter, type the name or IP address of the computer that you want to look up. For the second parameter, type the name or IP address of a DNS name server. If you omit the second argument, nslookup uses the default DNS name server.
If you need to look up more than one piece of data, you can use interactive mode. Type a hyphen (-) for the first parameter and the name or IP address of a DNS name server for the second parameter. Or, omit both parameters and nslookup uses the default DNS name server. Following are some tips about working in interactive mode:

To interrupt interactive commands at any time, press CTRL+B.

To exit, type exit.

To treat a built-in command as a computer name, precede it with the escape character ().

An unrecognized command is interpreted as a computer name.

 

Examples

Each command-line option consists of a hyphen (-) followed immediately by the command name and, in some cases, an equal sign (=) and then a value.

To change the default query type to host (computer) information and the initial time-out to 10 seconds:

nslookup -querytype=hinfo -timeout=10

 

 

Net Use

The Net use command allows you to connect of disconnect a computer from a shared resource as well as display detailed information about computer connections. The Net use command is also a good way to control all persistent net connections on a specified computer or resource. If you use the net use command without any specific parameters you will get a list of all the network connections on a specific computer.

Syntax (view syntax formatting guide):

net use [{<DeviceName> | *}] [\<ComputerName><ShareName>[<volume>]] [{<Password> | *}]] [/user:[<DomainName>]<UserName] >[/user:[<DottedDomainName>]<UserName>] [/user: [<UserName@DottedDomainName>] [/savecred] [/smartcard] [{/delete | /persistent:{yes | no}}]
net use [<DeviceName> [/home[{<Password> | *}] [/delete:{yes | no}]]
net use [/persistent:{yes | no}]

Syntax Explanation

  • DeviceName: Assigns a name to connect to the resource or specifies the device to be disconnected. There are two kinds of device names: disk drives (that is, D: through Z:) and printers (that is, LPT1: through LPT3:). Type an asterisk (*) instead of a specific device name to assign the next available device name.
  • \ComputerNameShareName: Specifies the name of the server and the shared resource. If ComputerName contains spaces, use quotation marks around the entire computer name from the double backslash (\) to the end of the computer name (for example, “\Computer NameShare Name“). The computer name can be from 1 to 15 characters long.
  • volume: Specifies a NetWare volume on the server. You must have Client Service for NetWare or Gateway Service for Netware (Windows Server) installed and running to connect to NetWare servers.
  • Password: Specifies the password needed to access the shared resource. Type an asterisk (*) to produce a prompt for the password. The password is not displayed when you type it at the password prompt.
  • /user: Specifies a different user name with which the connection is made.
  • DomainName: Specifies another domain. If you omit DomainName, net use uses the current logged on domain.
  • UserName: Specifies the user name with which to log on.
  • DottedDomainName: Specifies the fully-qualified domain name for the domain where the user account exists.
  • /savecred: Stores the provided credentials for reuse.
  • /smartcard: Specifies the network connection is to use the credentials on a smart card. If multiple smart cards are available, you are asked to specify the credential.
  • /delete: Cancels the specified network connection. If you specify the connection with an asterisk (*), all network connections are canceled.
  • /persistent: {yes | no}: Controls the use of persistent network connections. The default is the setting used last. Deviceless connections are not persistent. Yes saves all connections as they are made, and restores them at next logon. No does not save the connection being made or subsequent connections. Existing connections are restored at the next logon. Use /delete to remove persistent connections.
  • /home: Connects a user to the home directory.
  • net help <Command>: Displays help for the specified net command.

Explanation with Examples

Connecting and disconnecting from a network resource

  • Use net use to connect to and disconnect from a network resource, and to view your current connections to network resources. You cannot disconnect from a shared directory if you use it as your current drive or an active process is using it.

Viewing connection information; To view information about a connection, you can do either of the following:

  • Type net use DeviceName to get information about a specific connection.
  • Type net use to get a list of all the computer’s connections.

Using deviceless connections

  • Deviceless connections are not persistent.

Connecting to NetWare servers

  • After you install and run Client Service for NetWare, you can connect to a NetWare server on a Novell network. Use the same syntax that you use to connect to a Windows Networking server, except you must include the volume you to which you want to connect.

Using quotation marks

  • If the ServerName that you supply contains spaces, use quotation marks around the text (that is, “Server Name“). If you omit quotation marks, an error message appears.z

To assign the disk-drive device name E: to the Letters shared directory on the \Financial server:

net use e: \financialletters

To assign (map) the disk-drive device name M: to the directory Mike within the Letters volume on the \Financial server:

net use m: \financiallettersmike

To connect the user identifier Dan as if the connection were made from the Accounts domain:

net use d:\servershare /user:AccountsDan

To disconnect from the \FinancialPublic directory, type:

net use f: \financialpublic /delete

To connect to the resource memos shared on the \Financial 2 server:

net use k: “\financial 2” memos

To restore the current connections at each logon, regardless of future changes, type:

net use /persistent:yes

 

Net Print

The Net Print command displays and controls information about specified printer ques and jobs. Note the Net Print command has been depreciated in Windows 7 and Server 2008. However, you can perform many of the same tasks included in the Net Print command through Prnj0bs.vbs, Windows Management Instrumentation (WMI), or through the use of the new Windows PowerShell cmdlets.

Syntax (view syntax formatting guide):

Net print {\<ComputerName><ShareName> |
\<ComputerName> <JobNumber> [/hold | /release | /delete]} [help]

Syntax Explanation

  • \ComputerNameShareName: Specifies (by name) the computer and print queue about which you want to display information.
  • \ComputerName: Specifies (by name) the computer that hosts the print job you want to control. If you do not specify a computer, the local computer is assumed. Requires the JobNumber parameter.
  • JobNumber: Specifies the number of the print job you want to control. This number is assigned by the computer that hosts the print queue where the print job is sent. After a computer assigns a number to a print job, that number is not assigned to any other print jobs in any queue hosted by that computer. Required when using the \ComputerName parameter.
  • [/hold | /release | /delete]: Specifies the action to take with the print job. The /hold parameter delays the job, allowing other print jobs to bypass it until it is released. The /release parameter releases a print job that has been delayed. The /delete parameter removes a print job from a print queue. If you specify a job number, but do not specify any action, information about the print job is displayed.
  • help: Displays help for the Net print command.

Explanation with Examples

This example shows how to list the contents of the Dotmatrix print queue on the \Production computer:

Net print \ProductionDotmatrix

This example shows how to display information about job number 35 on the \Production computer:

Net print \Production 35

This example shows how to delay job number 263 on the \Production computer:

Net Print \Production263 /hold

This example shows how to release job number 263 on the \Production computer:

Net print \Production 263 /release

 

Net Localgroup

The Net Localgroup command allows you to add, display and modify local groups. When utilized with the /domain attribute the net localgroup command will add the specified group to the domain. Otherwise, the operation is performed on the local machine.

Syntax (view syntax formatting guide):

net localgroup [GroupName [/comment:"Text"]] [/domain]
net localgroup [GroupName {/add [/comment:"Text"] | /delete} [/domain]
net localgroup [GroupName Name […] {/add |  /delete} [/domain]

Syntax Explanation

  • GroupName: Specifies the name of the local group to add, expand, or delete. Used without additional parameters, net localgroup GroupName displays a list of users or global groups in a local group.
  • /comment:”Text Adds a comment for a new or existing group. The comment can contain up to 256 characters. Enclose the text in quotation marks.
  • /domain: Performs the operation on the primary domain controller of the current domain. Otherwise, the operation is performed on the local computer.
  • Name[ …]: Lists one or more user names or group names to add or remove from a local group.
  • /add: Adds a global group name or user name to a local group. You must first establish an account for users or global groups before you can add it to a local group with this command.
  • /delete: Removes a group name or user name from a local group.
  • net help Command: Displays help for the specified net command.

Explanation with Examples

  • Using /domain: /domain applies only to computers that are members of a domain. By default, server computers perform operations on the primary domain controller.
  • Using Name: Separate multiple entries with a space. Names can be local users, users on other domains, or global groups, but not other local groups. If a user is from another domain, preface the user name with the domain name (for example, SalesRalphr).
  • Grouping users: Use net localgroup to group users who use the computer or network in the same or similar ways. When you assign rights to a local group, each member of the local group automatically has the same rights.

The following example displays a list of all the local groups on the local server:

net localgroup

The following example adds a local group called Exec to the local user accounts database:

net localgroup exec /add

The following example adds a local group called Exec to the domain user accounts database:

net localgroup exec /add /domain

The following example adds the existing user accounts stevev, ralphr (from the Sales domain), and jennyt to the Exec local group on the local computer:

net localgroup exec stevev salesralphr jennyt /add

The following example adds the existing user accounts stevev, ralphr, and jennyt to the Exec group of a domain:

net localgroup exec stevev ralphr jennyt /add /domain

The following example displays users in the Exec local group:

net localgroup exec

The following example adds a comment to the Exec local group record:

net localgroup exec /comment:”The executive staff.”