======Microsoft - Scripting - Batch files - Notes====== =====Replace string in variable===== %input:search=replace% Source:[[http://omgili.com/batch-dos-replace-search-string|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:[[https://www.robvanderwoude.com/parameters.php|Rob van der Woude's Scripting Pages - Batch files - Command line parameters]] =====Replace string in file===== Download [[https://sourceforge.net/projects/fart-it/|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:[[http://support.microsoft.com/kb/947709/en-us|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: [[https://support.microsoft.com/en-us/topic/how-the-windows-update-client-determines-which-proxy-server-to-use-to-connect-to-the-windows-update-website-08612ae5-3722-886c-f1e1-d012516c22a1|Microsoft Support - How the Windows Update client determines which proxy server to use to connect to the Windows Update website]] ====http==== [[https://learn.microsoft.com/en-us/windows-server/networking/technologies/netsh/netsh-http|Microsoft Learn - Netsh http commands]] =====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 [[microsoft:scripting:vbscript:scripts:setpwtoneverexpire|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:[[http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us|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: [[https://www.robvanderwoude.com/battech_redirection.php|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: [[https://stackoverflow.com/questions/1610337/how-can-i-find-the-current-windows-language-from-cmd|stack overflow - How can I find the current Windows language from cmd?]]