Command Prompt as Admin with %username% variables of actual user - windows

Trying to set some firewall rules for a machine on a domain. It requires admin permissions through cmd prompt to set them, however I want to use the %username% variable as the command requires a specific folder stored in the logged in users appdata to be allowed.
Is there anyway to work around this?
As when launching cmd prompt as admin, it uses my admin username in %username% rather than the logged in user and I'm hoping to automate this. Needs to be done through cmd/powershell/batch.

Have managed to resolve it myself. I've managed to save the variable to a file, and call that file into a second command prompt session elevated as admin and use a different variable.
First:
echo %USERNAME%>C:\dir\file.txt
Second:
cd C:\dir\
set /P uname=<uname.txt
echo %uname%

Related

how to stop echo command from displaying variables which are not set?

I am working on an application that requires my users to share their fully-qualified-domain-name of their windows machine.
To help my users to extract their machine's FQDN, I want to share simple command line steps that they can copy/paste and execute on their terminals to get the result.
I was thinking of below command to extract local machine's FQDN:
echo %COMPUTERNAME%.%USERDNSDOMAIN%
But there are few problems of this command.
It gives output in ALL CAPS. (I can live with it)
It gives incorrect output if the variable is not set.
For example:
If USERDNSDOMAIN value is not set, then, you'll get following output:
echo %COMPUTERNAME%.%USERDNSDOMAIN% //<- Run this on cmd prompt
ClientComputerName.%USERDNSDOMAIN% //<- wrong output: Notice '%USERDNSDOMAIN%' is appended in o/p
Is there any way to stop echoing a variable if it's value is not set?
Please note that I want to extract "fully qualified domain name" of my windows machine through CMD prompt only.
You can get the FQDN name using PowerShell.
=== Get-FQDN.bat
#ECHO OFF
FOR /F %%A IN ('powershell -NoLogo -NoProfile -Command ^
"([System.Net.Dns]::GetHostByName($Env:COMPUTERNAME)).HostName"') DO (
SET "THEFQDN=%%A"
)
ECHO %THEFQDN%
If you have multiple users, then you surely have some way to get programs and batch files installed on them. Once this batch file script is installed into a directory on the user's PATH, it is a one-line command.
Get-FQDN
1st, please note that the "USER DNS Domain" is NOT the domain the computer is joined to, it is the domain the USER who is logged in belongs to.
If you log in as a user from a trusted domain, or a child or parent domain, then it will display that domain.
So, if you log in as a LOCAL account it will be blank (likely you are running into this)
There is a fairly simple way to get the actual computer domain however, by using NLTest. (For the like of me I could never figure out why Microsoft didn't pre-populate a variable with this info.)
At the CMD Line simply dump this into the command prompt (I believe you will need to run with admin privileges but I haven't tested):
FOR /F "tokens=3" %_ IN ('nltest /DOMAIN_TRUSTS /PRIMARY ^|FIND /I "0:"') DO #(ECHO.%COMPUTERNAME%.%_)
The result will be in all caps because that is how Microsoft displays this info.
Here is an example output:
MYLAPTOP.USERS.MYDOMAIN.LOCAL
But on-re-read you want something the users know how to do themselves, so ymmv if you could just send a reference email, or hand it to them each time they need it.
If you just wan this info and other info easily available you could use BGInfo or other options like that to set the desktop background.
Alternatively you could change the logon scripts to generate a simple text file with all the info each time the user logs on, and placed in a certain folder you tell them to look in.

Can I declare a password/key through parameters in windows scheduler?

I am running few scripts in powershell and cmd through windows scheduler. I have to provide my credentials and access keys in both the cmd and powershell scripts.
Is there anyway where I can provide my credentials through parameters like in Continous integration Tools? or any other way?
ex:
SET Username=Usrnme
Set Password=pswd //shouldn't be visible
curl.exe --basic --u usrnme:pswd -X get "https:www.google.com" -k
I don't want everybody who has access to the system to view the username and password.
Please suggest.
Here are your options:
Put the user name and password in a file and read it into batch script variables using for /f command
If you not using "simple filesharing" (= home edition versions of windows) you can set security so only one specific user has access to a password file or leave it in batch script and make that file's security so only one specific user has access.
Any text you are passing on a curl command line it is visible to any process while your curl.exe process is running. Another task can list running tasks eg from command prompt wmic process where ^(name^="curl.exe"^) get commandline would show it. I don't know how to obscure that.
You can use the Powershell Credential Manager to safely store the password on local computer. Then you can add the parameter to the script that represents credential target. See the example here

Command prompt executing command differently than batch file and Visual Studio post-build script

I have a post-build script in Visual Studio that I am trying to run so that we can make remote deployments easier. Earlier in this script (you don't see it here), we hard-code the user in the net use execution below. So all we're doing here is having the user input the server name and database to deploy to and then connect to the network share of that server.
What I've found though is that the code below works fine only if you run it with Command Prompt. It opens up a new window and has the user put in the server and database and then the net use command will correctly show the server the user input, all in the same window.
If you try to use this in a batch file or in Visual Studio though, the net use command shows blank for the server name.
I appreciate any insight anyone might have on this!
START CMD /C "CALL SET /P SERVERNAME=Please Enter The Server Name: & CALL SET /P DATABASE=Please Enter The Database Name: & CALL CMD /C net use \\%SERVERNAME%\d$ /persistent:no * /user:%user%"
Follow advise of #bgalea. Where is %user% defined? Did you mean %username%? Of course this scheme will not work if you plan to have builds done by a build agent later.
The reason it is not working is that variables are expanded at load time. Consequently, %SERVERNAME% is expanded when the line is loaded...BEFORE the user has entered a value. Try changing to this:
SETLOCAL ENABLEDELAYEDEXPANSION SET /P SERVERNAME=Please Enter The Server Name: & SET /P DATABASE=Please Enter The Database Name: & \\!SERVERNAME!\d$ /persistent:no * /user:%username%"
That way you will be using the run time value of SERVERNAME.

How to run different winlogon shell for different user in windows?

on Windows winlogon shell i specified to run a start.bat file which starts a program, now i want to run on windows logon specific programs for specific user.
for example, for user1 when windows starts it runs program1. for user2 when windows starts it runs program2. how can i do this ? if write on .bat how to know which user logined?
You can use the username variable: %USERNAME% for finding the name of the user currently logged in.
Modify the start.bat file, use if-else conditionals for running programs based on username.
Run start.bat at logon.

Setting permanently variables in windows %username%

i need to set permanently the %username% variable on windows is that possible?
I mean if I do on command prompt set username=UPPERCASE I can see that the variable is changed, however as expected this does work just in that command prompt, If I open another one the username variable is the original.
I tried to find the correspondent registry value but I did not find it.
I need some automatic way to do it.
Thanks!
When each process starts, the process gets a copy of the parent process' environment variables. So, if you used Windows Explorer to start your command prompt. You get a copy of Explorer.exe's environment variables, but when you edit it in cmd.exe, you don't edit the value for the rest of the system.
That said, Windows provides an event that processes can subscribe to so they can be told that there is a new value for environment variables. If you are interested, I can try to dig it up. I've used it before for the Path environment variable and think it may apply to your problem.
Sounds like you should create a bat file. This will prompt the user for their username and put it in the %USERNAME% environment variable.
SET /p USERNAME=What is your UserName?
putty /user:%username%

Resources