简体   繁体   中英

Using $env:username in a logon script is not working

I have this setup with a GPO to run as a logon script, obviously to map a drive for clients.

net use H: \\server1\share\$env:username

Dead simple. If I put in a specific username in place of the variable then it works. Also, if I run this script directly on a client machine it works.

Do you get an error? Does it helps if you wrap the path in double quotes?

"\\server1\share\$env:username"

That should work with or without the quotation marks. Are the clients all Windows 7? I believe that Powershell logon/startup scripts don't work on earlier versions, even if you have Powershell installed, because it's an add-on rather than a native shell. If you're using downlevel clients, you can invoke the powershell script from a cmd batch file. But if you're going to do that, might as well skip Powershell and just run net use H:\\\\server1\\share\\%username% from your batch file.

Run this in a .bat file as a login script (use a FQDN for the server name):

net use H: \\server.example.com\share\%USERNAME%

If that not works, your GPO is just not yet applied. You can force that by running on the machine gpupdate /force . For testing make sure that the GPO is applied to the right OU to the group "Authenticated Users" (computers are in the group too). And if that is not working, you can also "Enforce" the GPO. Don't forget to do a gpupdate /force after any change to the GPO or it settings.

You can also try a .js logon script like:

var WSH = new ActiveXObject("WScript.Shell");

    // Map share
    WSH.Run("net use H: \\\\server.example.com\\share");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM