Reputation: 13
I'm trying to figure out how to prompt the user for entering his user credentials and process the password inside a batch file.
After some fair amount of tinkering I came up with the following code:
@echo off
set password=
FOR /F "tokens=* USEBACKQ" %%F IN (`powershell -NoLogo -NoProfile -Command ^(Get-Credential "ag\%username%"^).GetNetworkCredential^(^).password;`) DO (
set password=%%F
)
echo %password%
This works fine if the user enters a non empty password. But if the user enters nothing or simply closes the dialog window by clicking "cancel", the variable "password" suddenly contains the string "ECHO ist ausgeschaltet (OFF)." instead of an empty string.
Does anyone have an idea how I can return an empty string for the password in this case?
Thanks in advance solid
Upvotes: 1
Views: 830
Reputation: 82337
In reality password
is not defined.
That you got ECHO ist ausgeschaltet (OFF)
is the output of the ECHO
command, as you call it with no data.
To see the real content, you could use set password
or ECHO(
, the strange opening parenthesis prevents problems when the content of your variable is empty
...
echo( %password%
set password
Btw. You should use the extended set syntax, to avoid problems with trailing whitepsaces.
set "password="
...
set "password=%%F"
Upvotes: 0
Reputation: 13690
An environment variable has always a value. If an empty string is assigned then the variable is removed (in the running process).
SET FOO=
The variable FOO is removed with the statement above.
So you need to check if a variable is defined and not empty.
if defined FOO echo FOO:%FOO%
if not defined FOO echo FOO is not defined
Upvotes: 2