Windows – Confirm System Uptime

When running windows system check scripts it’s nice to know when your server was last rebooted.  The command shown bellow generates this information.

net stats srv | findstr "since"

Advertisements

Windows – Delete Old Files

A few cautionary warnings… this script will delete files from whatever location you specify as LOGPATH.  Use with care. As a best practice I always create my logs with a pre-fix. ex: CheckUpTime-.log which can then be used in the deletion script. This will ensure that you only delete logs that start with “CheckUpTime”. 

:: Delete log files older then 14 days
set LOGPATH=C:Logs

:: Echo’s logs which will be deleted (use this before using the command which contains “del”)
forfiles -p %LOGPATH% -m CheckUpTime-*.* -d -14 -c “cmd /c echo 0x22@Path@File0x22”

:: Actual Delete Command (remove :: to run command)
::forfiles -p %LOGPATH% -m CheckUpTime-*.* -d -14 -c “cmd /c del /q @path”

Grep equivalent for Windows (String Parsing)

The built in windows command FindStr mirrors the capabilities of the Unix command Grep.

Findstr /?
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
        [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
        strings [[drive:][path]filename[ …]]
  /B         Matches pattern if at the beginning of a line.
  /E         Matches pattern if at the end of a line.
  /L         Uses search strings literally.
  /R         Uses search strings as regular expressions.
  /S         Searches for matching files in the current directory and all
             subdirectories.
  /I         Specifies that the search is not to be case-sensitive.
  /X         Prints lines that match exactly.
  /V         Prints only lines that do not contain a match.
  /N         Prints the line number before each line that matches.
  /M         Prints only the filename if a file contains a match.
  /O         Prints character offset before each matching line.
  /P         Skip files with non-printable characters.
  /OFF[LINE] Do not skip files with offline attribute set.
  /A:attr    Specifies color attribute with two hex digits. See “color /?”
  /F:file    Reads file list from the specified file(/ stands for console).
  /C:string  Uses specified string as a literal search string.
  /G:file    Gets search strings from the specified file(/ stands for console).
  /D:dir     Search a semicolon delimited list of directories
  strings    Text to be searched for.
  [drive:][path]filename Specifies a file or files to search.

Example of using Findstr to search for multiple strings in succession within a log file.
findstr “ERROR Caused Fail” c:logslog.log

This command will search for the String ERROR then Caused, and Fail while parsing down a file. So if the file looked like this:
Line1: Error Script Failed
Line2: test1
Line4: Caused by process down
Line5: test2
Line6: Error2

The FindStr output would look like this:
Error Script Failed
Caused by Process down
Error2

Windows Host Up Time

Find out when a Windows host was last restarted using native windows commands.  

Run net stats srv
Server Statistics for \LAP1

Statistics since 9/16/2008 9:14 AM

Sessions accepted 1
Sessions timed-out 0
Sessions errored-out 0

Kilobytes sent 0
Kilobytes received 0

Mean response time (msec) 0

System errors 0
Permission violations 0
Password violations 0

Files accessed 0
Communication devices accessed 0
Print jobs spooled 0

Times buffers exhausted

Big buffers 0
Request buffers 0

 

The command completed successfully.

Pipe to findstr to filter out unecessary stats.

net stats srv | findstr “since”
Statistics since 9/16/2008 9:14 AM

Batch – Common Variables & Shortcuts

:: Keep variables local
SETLOCAL (Define at beginning of script)
ENDLOCAL (Release variables at end of script)

:: Set Date Variable
set CurrentDATE=%date:~4,2%-%date:~7,2%-%date:~10,4%
set CurrentTime=%time:~0,2%.%time:~3,2%

Output: 09-15-08

:: Current Host Name
set name=%hostname%

::Current working directory
%CD% – Gives current working directory with no preceding \
%~dp0 – Gets current working directory with a preceding \

Lightweight .bat to .exe file conversion

Bat_To_exe_Converter allows conversion and creation of executables from Windows Batch Files. No installation is necessary, just unpack the zipped executable and point it to the .bat file you would like converted.

Bat_to_Exe_Converter – f2ko App.

Required Information:

  1. Batch File
Optional Parameters:
  1. Add binaries or files to executable
  2. Icon file
  3. File Version
  4. Product Version
  5. Company
  6. Description
  7. Copyright
And most importantly… Console Application vs. Ghost Application (hidden from user)