script to map networkdrive base on username - windows

I have a .cmd script I want to map a network drive based on the username of the person running it. So something like this
net use K: \\nas001\users\[My_User_Name]
Except replace [My_User_name] with the name of the user running the script. Is this possible?

The environment variable %USERNAME% contains the actual user name, so you could use this, but I wouldn't advice this.
Windows has the concept op 'home drive', to which you can connect using:
NET USE K: /HOME
I think it's better to use that concept.

Related

How do I find DOMAIN\accountname$ of a remote AD-joined computer?

I want to store a remote computer's account name in a variable in the DOMAIN\Accountname format (preferably DOMAIN\Accountname$. I know the DNS name and samaccountname of the computer, and I want to run the script on a domain-joined computer (not a domain controller).
I cannot find any simple one-line approach to get the account name in that format. I can manually construct the string by concatenating the NETBIOS domain name with computer name (samaccountname), but I was assuming there is an easier and more robust way of doing this.
So is there any built-in approach of getting the computer name in the DOMAIN\Accountname$ format without having to manually constructing the string? I assume that's common task.
You can also look at the environmental variables. For this question, I would do the following...
$strUserAndDomain = ((Get-Content env:userdomain) + "\" + (Get-Content env:username))
Then you can user the value of $strUserAndDomain whenever you want.

Retrieve CASE SENSITIVE Windows command 'whoami' Username [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am using WinAutomation and I am trying to point to and open a file on any computer that my software is placed on without having to hard-code the username in the path, so I am looking for a way to retrieve the logged in Username, that will be case sensitive so I can grab that variable.
Using the Windows command line, (which I can run invisible to the user in the background) if the username in the file tree is "User Name", whoami will return "user name" and then,
If I point to a file C:\Users\%pcName%\Desktop\Masterlist.xlsx with that %pcName% not correctly capitalized it will not work...
I saw a question about Pythonic way to retrieve case sensitive path but I can't use Python for this. I am open to creative ways to get the case sensitive User Name but I keep seeing people deciding to enforce lowercase usernames as a solution.
Is there a way to find the case-sensitive logged in users name for use in file path references? I'm open to being creative at this point!!
First, let me make clear that your request is probably an XY issue. You're trying to achieve something and it looks like the solution is to fix the casing in the user name. However, there are several argument against that:
Windows file names are not case sensitive. It should not matter what casing you use. Maybe some other component compares these names in a case sensitive way where it shouldn't. In that case, the component should be fixed.
User names in Windows are not case sensitive. You can log on with any casing.
You're constructing a path C:\Users\%pcname%, which looks like %USERPROFILE%. However, a user profile is not necessarily in C:\Users. It could have been moved anywhere.
It's unclear to me, what exactly "does not work" and for which reason. You don't specify an error message which could let me diagnose the root cause, so we could fix the real problem.
You say that you can't use Python, but you do not specify any other programming language that you can use, so let me treat dos as an equivalent of batch.
Whatever your real problem is, echo %username% does what you ask for:
C:\Users\For example John>echo %username%
For example John
But be warned: in 90+% of the cases, this is not the correct approach.
In C#, you would use
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
for the user profile and
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
for the Desktop.
Winautomation has a function, "Get Special folder" that will return the filepath to the Desktop of the current PC as a variable. That variable can then be used as the root of whatever you are trying to access.
This is the correct way to reach the Desktop file path and does not require any special DOS usage.
'Desktop' is a User Shell Folder. The value for these folders is stored in the registry, with the right case :-).
The registry can be queried from the command prompt using 'reg query'.
If you just want the data value of the Desktop property, use the following:
for /f "tokens=2*" %a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop ^| findstr "REG_"') do #echo %b

How to compare a string with the user's password?

I am trying to make an AppleScript program that asks the user to input their password and check if the text returned of the variable is equal to their real password. I am not trying to gain administrator privilege, just trying to retrieve the user's password and store it in a string. The reason why I bother to check the variable against the real user password is I don't want the user just enter something random. Are there any ways to do this?
Thank you!
In short, you can't. I have looked into this quite a bit, and there is no way you can get the users pass. If you could, you could destroy their computer with a simple script. The "Keychain Application" keeps the passwords far away from any kind of script. Sorry to disapoint. :(
if you could, it would be like this:
set realPassword to administrator password of (get system info)
set theirPassword to null
if realPassword is theirPassword then
say "yay"
else
say "nope"
end if

How can I change the windows USERNAME environment variable string?

I am looking for a solution for this after struggling for most of the day. In short my dilemma seems simple. An effective solution appears elusive based on the Google and Stackoverflow hits I've had on this one.
(EDIT) I need to clarify the use-case. We may have 6 users needing to run certain scripts that use the "%USERNAME%" string to login to case-sensitive environments, Linux, MySQL, etc. One user-name is mixed-case. In the example I used my name: "Will" for the scripts it must-be: "will" (all lower-case).
The general question though is how can the %USERNAME% environment variable value be change when it is mis-entered?
At present my Windows 7 Professional username is "will" but the environment variable has this value:
echo. User = %USERNAME%
User = Will
I need it to come-out as:
User = will
rem * All lower-case
To be clear ...
I want to change (source) value Windows sets the USERNAME environment variable when I login.
I thought it would be easy to find the setting or config option to change that string -- Until I tried. I think this must happen frequently judging from questions related to getting a USERNAME to work with cygwin, etc.
The question
How to convert the value of %USERNAME% to lowercase within a Windows batch script?
Bothers me too, because that's a messy script to "fix" something basic in a login-environment.
I did a quick survey around the office as well. Some people have mixed case, one or two have lowercase. It seems arbitrary depending on what was typed-in when account was created or something.
Our admin guy looked up my account on the domain (no, not active directory) and is same as my login-screen, "will" (lowercase).
Finally I did a couple of experiments. I changed my username in the Users control panel
from: "will" to "xxwill"
Thinking that would surely update my USERNAME string. I logged out. I did it again and rebooted a second time. The result was extremely surprising (or maybe it shouldn't have been):
User = Will
rem * NO change for the "xxwill" name-change!
Some further grist. I created an account "dodo" and account "Dogdo" and changed the usernames to "Dodo" and "dogdo" respectively.
User = dodo
User = Dogdo
rem * NO change for name-changes!
I see that the USERNAME can't be altered that way. I can't edit the system environment variable in the Advanced options. It may be in the profile. But can you edit a profile or even look inside it with just Administrator powers? I don't know yet; I'm not sure that's my preferred route either since it is hacking with a bit of "output" from some original setting, somewhere.
I hope someone can set me straight so I don't need that script-solution.
If you want to change the windows USERNAME environment variable string, you can do this in the Advanced User Accounts Control Panel.
Open it by running netplwiz ("C:\Windows\System32\Netplwiz.exe").
Then select the name you want to change and click on the Properties button.
Now you get the properties of the selected user account: you can change the User name (, Full name & Description) here.
You'll (just) need administrator rights to do this.
Log out and log on, open a command prompt and try to output the value for %USERNAME%. You'll notice it has been changed according to your modification.
#ECHO OFF
SETLOCAL
PUSHD "%temp%"
COPY NUL "%username%" >NUL 2>NUL
FOR /f "delims=" %%a IN ('dir /L /b "%username%"') DO set "usernamelc=%%a"
del "%username%" >NUL 2>NUL
POPD
ECHO %USERNAME% --^> %usernamelc%
GOTO :EOF
Perhaps this may help. YMMV.
I don't think there is a way to do exactly what you are saying, but it might be achievable, you will know by playing around with the locations where usernames are defined.
• Firstly, there is the user folder C:\user\username1 and that is defined at user creation. This can (I think) be changed (I've only seen references to it, but there are many references, this is easy to find).
• Secondly, there is the Control Panel > User Accounts > Change My Name. This seems to tie in with your $env:USERNAME
• Finally, there is the lusrmgr.msc > Users. This is tied most closely to your "real" username, and aligns with [Security.Principal.WindowsIdentity]::GetCurrent()
I say "real" because I found this recently when trying to use ssh-keygen to create a public key and although my Display Name and $env:USERNAME were set to "Name1", ssh-keygen would only see "Name2" and I had to alter the name in lusrmgr.msc to fix this (a reboot was required).
This is an old post, but posting in case this can help anyone with similar issues.

Changing permissions and users that can view a directory in windows

Here is my dilemma. We have a server with a path, say \server1\data$\foo\bar\stuff. I'm using Windows 2005 Server for this. Now, for \stuff, I'd like to add something like 150 users that I have in a CSV file (can get it from Excel).
I can parse the CSV file rather easily (most languages have libraries for this), but I don't know anything about Windows permissions (used Linux for quite a bit of my career prior).
Can I do this in PHP?
You can do this from the command line via active directory commands through DOS or Powershell. I list some references and examples from references below.
I would suggest adding these users to a group. This example adds a user to a group:
dsmod group "CN=csvpeeps,OU=Distribution Lists,DC=Contoso,DC=Com" -addmbr "CN=jimmyCrackedCorn,CN=Users,DC=Contoso,DC=Com"
Reference: http://technet.microsoft.com/en-us/library/cc732423(WS.10).aspx
To create a group, see the dsadd command. Example of creating a group:
dsadd group cn=csvpeeps,cn=users,dc=northwindtraders,dc=com
One option on Windows, through DOS is Cacls, iCacls, and other variants. This example adds Read permissions to a resource for the group, 'Power Users':
CACLS \server1\data$\foo\bar\stuff. /E /G "csvpeeps":R
Reference: http://ss64.com/nt/cacls.html
I have read that CACLS can be buggy, so you may want to look into another variant of CACLS. I highly recommend http://ss64.com
If you don't know the exact OU/CN, etc for users or groups, you can check it out with something like this:
dsquery user -name jimmyCrackedCorn
Similarly for groups
dsquery group -name csvpeeps
Let me know if you get stuck and I can put more effort into this. Powershell is another option worth looking into.
Ok, here's my approach. After looking at the issue. I think PowerShell is the way to go. I have a CSV file as mentioned above which is delimited with commas and have a the following format:
Last Name, First Name
Smith, Bob
Corn Amy
Etc.
What I'd like to do is find out the AD usernames of these people (Amy Corn will be acorn according to our internal format) and then insert them into a specific directory so that read-only directory has those users that can view its contents. Any help is appreciated.

Resources