I found this answer on superuser.com, where it was migrated from stackoverflow.com. On StackOverflow, it was marked as “Off-topic” and closed. So long as the answer was preserved, we can be grateful.
The superuser.com thread contains much more than I’m covering here, so if what I cover doesn’t address your question, please, follow the yellow brick link.
Suppose you’re working for a company that uses Active Directory to control access to the intranet. There are many benefits to such an arrangement, but there are also a few disadvantages. Meaningless group policies that enforce and control individual preferences number among the latter. The worst one I encountered was a policy that forced my desktop wallpaper picture position to “Stretch.” Since the policies did not force me to put up with the official Company Wallpaper, there was no earthly reason for such enforcement. I complained multiple times, but was told that “Oh, we can’t help you, because we get the policies from another Department,” the name of which I was not, apparently, allowed to know (“Section G,” anyone?).
Eventually, they gave that one up, but only for the newer computers on the network. For my old machine, I was forced to write a bat file (“Euuuwww, ick”) to set the Picture position in the registry. Luckily, I no longer have to use it on the new machine.
Another meaningless policy (at least for developers) is one that I encountered recently while using PowerShell. HOMEDRIVE was set to H: instead of the correct C:. For other environment variables, I could use workarounds in my profile file like this:
Set-Variable -Force -name HOME "C:\Users\$user"
But I could set that all day and the group policy would instantly override it:
» Set-Variable -Force -name HOMEDRIVE "C:"
» echo $env:HOMEDRIVE
H:
And (much worse):
» resolve-path ~
Path
----
H:\
Ick. This meant I couldn’t do simple things like cp foo.bat ~/. which is perfectly legal with PowerShell. Except that when I tried it, the H: drive would end up with more bytes to mark against my (teensy) quota.
The solution is rather elegant. All you have to do is add a little bat file to your startup directory. Here’s where the startup directory is:
For you:
%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
For all users:
%SystemDrive%:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
If you can’t find AppData, enable the viewing of hidden files on the Folder Options cmdlet in your Control Panel.
Once you have your startup directory (your own, not the one for all users), create a bat file in it called “homedrive.bat” in it containing the following lines:
net use h: /delete
net use h: "\\localhost\C$\Users\%USERNAME%"
That should do it. You can also use %SystemDrive% instead of C, and your login name instead of %USERNAME%. Substitute whatever is appropriate for h:, of course.
What this does is delete the HOMEDRIVE mapping set by the group policy, and replace it with something actually useful. If you do a net use in your PowerShell command window, you should see something like this:
» net use
New connections will be remembered.
Status Local Remote Network
-------------------------------------------------------------------------------
OK H: \\localhost\C$\Users\ivanlaningham
Microsoft Windows Network
What has happened is that you’ve just mapped your local C: drive as a network drive. Thus H: is identical to C:. That means cp foo.bat ~/. will do the right thing.
Priceless. And elegant.
No comments:
Post a Comment