
In the batch you use for the sqlcmd backup execution, I used an existing question/answer from smurf (Jul. You can set the dbSecure.txt as hidden file. Now clear the PowerShell commands history with ::ClearHistory() On a PowerShell console, type | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\TEST\dbSecure.txt" I share what I found (and I'm gonna use this code until I get a better way, like advised in his comment).

Sure, I messed something up in the code or in the understanding.ĭoes anybody know how could I handle this please ? The end of my batch is logically sqlcmd -I -S %SERVER% -U %USER% -P %PASSWORD% -i "C:\TEST\backup_database.sql" SecureStringToBSTR($Password)^)^) " `) DO ( So I tried this way FOR /F "tokens=* USEBACKQ" %%F IN (`powershell -command " (::PtrToStringAuto(:: Powershell -command " $SecurePassword = Get-Content 'C:\TEST\dbSecure.txt' | ConvertTo-SecureString "Īnd then FOR /F "tokens=*" %%F IN (powershell -noninteractive -command " ($UnsecurePassword = (New-Object PSCredential "user",$SecurePassword).GetNetworkCredential().Password) ") DO (


My problem is that I want to get back clear text from securestring (using PowerShell command) and then set it as the %PASSWORD% variable in my batch :įollowing advises and similar questions on internet, I tried this REM Powershell to create a $Password variable - its secure value is stored in dbSecure.txt I easily set the %SERVER% and %USER% variables in my batch SET SERVER=localhost\instance_name Using PowerShell, I successfully created a secure txt file which stores the plain text encrypted SQL needed password : 'MyPassword' | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\TEST\dbSecure.txt" But I don't want to store a clear password in that batch. My goal is to create a batch file which performs a SQL (express) DB backup every time it's executed.
