Wednesday, July 1, 2026

Hide password getting printed in log during batch file execution for code migration

 Hi,

To avoid the password getting printed in the log during code migrations using batch file, we need to mask the password when DBA enters the password during file execution. Below is a sample code which helps to achieve the requirement. 

Note : To ensure whether the received password is correct I have reprinted in the script which you can comment.


@echo off
setlocal enabledelayedexpansion

echo Enter Password:
:: Call PowerShell to securely prompt, mask characters, and return plain text
for /f "delims=" %%A in ('powershell -Command "$secret = Read-Host -AsSecureString; $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secret); [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)"') do (
    set "PASSWORD=%%A"
)

echo Password: !PASSWORD!

pause




No comments: