My goal is to create a batch file which performs a SQL (express) DB backup every time it's executed. But I don't want to store a clear password in that batch.
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"
I easily set the %SERVER% and %USER% variables in my batch
SET SERVER=localhost\instance_name
SET USER=toto
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 : Following advises and similar questions on internet, I tried this
REM Powershell to create a $Password variable - its secure value is stored in dbSecure.txt
powershell -command " $SecurePassword = Get-Content 'C:\TEST\dbSecure.txt' | ConvertTo-SecureString "
and then
FOR /F "tokens=*" %%F IN (powershell -noninteractive -command " ($UnsecurePassword = (New-Object PSCredential "user",$SecurePassword).GetNetworkCredential().Password) ") DO (
SET PASSWORD=%%F
)
...but it doesn't work
So I tried this way
FOR /F "tokens=* USEBACKQ" %%F IN (`powershell -command " ([System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::
SecureStringToBSTR($Password)^)^) " `) DO (
SET PASSWORD=%%F
)
… but it doesn't work neither.
The end of my batch is logically
sqlcmd -I -S %SERVER% -U %USER% -P %PASSWORD% -i "C:\TEST\backup_database.sql"
Sure, I messed something up in the code or in the understanding. Does anybody know how could I handle this please ?