User Tools

Site Tools


microsoft:scripting:batchfiles:notes

Microsoft - Scripting - Batch files - Notes

Replace string in variable

%input:search=replace%

Source:Omgili - Batch Dos Replace Search String

To replace the : with - in %TIME%:

%TIME::=-%

To get the substring from character 3 and onward:

%DATE:~3%

Remove trailing slash in the script directory:

REM Store the script directory.
SET BASEDIR=%~dp0
REM Remove trailing slash.
SET BASEDIR=%BASEDIR:~0,-1%

Remove leading and trailing double-qoutes

Remove leading and trailing “ from a positional parameter, in this case the fourth parameter:

"%~4"

Source:Rob van der Woude's Scripting Pages - Batch files - Command line parameters

Replace string in file

Download FART.

Use:

fart.exe FILENAME SEARCHTEXT REPLACETEXT

Check for empty parameter

IF /I "%1" EQU "" GOTO USAGE

:USAGE
ECHO Missing parameter.

Excute a set of commands for a number of IP-addresses

Copy files to 10.1.1.1, 10.1.2.1, 10.1.3.1, etc. up to and including 10.1.8.1.
(1,1,8) = start at 1, increase in steps of 1, stop at 8.

FOR /L %%S IN (1,1,8) DO (

NET USE "\\10.1.%%S.1\C$" /user:"username" "password" /PERSISTENT:NO

COPY /Y "C:\Tools\Robocopy\robocopy.exe" "\\10.1.%%S.1\C$\Windows\system32"

IF NOT EXIST "\\10.1.%%S.1\D$\Scripts" MKDIR "\\10.1.%%S.1\D$\Scripts"

COPY /Y "C:\Scripts\script.cmd" "\\10.1.%%S.1\D$\Scripts"

NET USE "\\10.1.%%S.1\C$" /D

)

Netsh commands

Enable the incoming Remote Desktop connections rule:

%systemroot%\system32\netsh.exe advfirewall firewall set rule name="Remote Desktop (TCP-In)" new enable=Yes

Source:Microsoft Support - How to use the "netsh advfirewall firewall" context instead of the "netsh firewall" context to control Windows Firewall behavior in Windows Server 2008 and in Windows Vista

Add a rule to open port 1433 for SQL Server:

%systemroot%\system32\netsh.exe advfirewall firewall add rule name="SQL Server" dir=in action=allow protocol=TCP localport=1433

Add an incoming rule to only open tcp port 9999 in the firewall for programx.exe and only allow access from 10.1.1.1:

%systemroot%\system32\netsh.exe advfirewall firewall add rule name="Rule for program X" dir=in action=allow program="C:\Program Files\Program X\programx.exe" profile=any remoteip=10.1.1.1 localport=9999 protocol=TCP 

winhttp proxy

Show proxy:

netsh winhttp show proxy

Remove proxy settings:

netsh winhttp reset proxy

Set proxy:

netsh winhttp set proxy PROXY-SERVER-NAME:PORTNUMBER

Source: Microsoft Support - How the Windows Update client determines which proxy server to use to connect to the Windows Update website

http

Create or change local user

Create new user that can't change password:

NET USER /ADD newuser p@ssw0rd /passwordchg:no

Remove the new user from the users group:

NET LOCALGROUP users /del newuser

Add the new user to the Administrators group:

NET LOCALGROUP Administrators /add newuser

Set the password to never expire:

%SystemRoot%\system32\cscript.exe c:\temp\SetPwToNeverExpire.vbs newuser

See here for the .vbs.

Start command prompt with drive mappings as another user

SET USRACC=domain\account
"%SYSTEMROOT%\system32\runas.exe" /noprofile /user:%USRACC% "CMD /T:04 /K net use g: \"\\server.domain.com\share1\" & net use h: \"\\server.domain.com\share2\" & net use i: \"\\server.domain.com\share3\" & net use j: \"\\server.domain.com\share4\""

Capture command line output into a variable

FOR /F %S IN ('date.exe +%Y-%m-%d') DO SET ISO8601DATE=%S

For use in a batch file:

IF EXIST date.exe FOR /F %%S IN ('date.exe +%%Y-%%m-%%d') DO (SET DATEFN=%%S)

Source:Stack Overflow - How to get current datetime on Windows command line, in a suitable format for using in a filename?

Redirect all output

Add “2>&1” *AFTER* the file to redirec to:

test.bat > test.txt 2>&1

Source: Rob van der Woude's Scripting Pages - Batch How To ... - Display & Redirect Output

Determine Windows InstallLanguage

To use with for exemple net localgroup commands with localized group names.

@ECHO OFF
REM Determine language for the correct group names to use.
FOR /F "tokens=3" %%a IN ('"%SYSTEMROOT%\system32\reg.exe" QUERY HKLM\SYSTEM\CurrentControlSet\Control\Nls\Language /v Installlanguage') DO SET LANGUAGE=%%a

REM Assign group names.
REM Use within ( and ) instead of separated by && to prevent a space being added to the groupname.
IF %LANGUAGE% EQU 0409 (
        SET GUESTGRP=Guests
        SET PERFGRP=Performance Monitor Users
        SET USERGRP=Users
)
IF %LANGUAGE% EQU 0413 (
        SET GUESTGRP=Gasten
        SET PERFGRP=Prestatiemetergebruikers
        SET USERGRP=Gebruikers
)
 
ECHO %GUESTGRP%
ECHO %PERFGRP%
ECHO %USERGRP%

Source: stack overflow - How can I find the current Windows language from cmd?

microsoft/scripting/batchfiles/notes.txt · Last modified: 2023/08/02 17:31 by bas

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki