The estimated reading time 2 minutes
Replace drivemapping in sysvol for multiple users
Hi there,
as a consultant I see lots of customer environments. In every environment there is another method of mapping networkdrives. Today I want to focus on a environment where every user has it’s own script ( yes I saw this quite often).
As you can imagine it is not easy to change drivemappings for all these guys.
The solution is quite simple: powershell which saves you hours of working on login scripts.
So where do we find the loginscripts mapped by the user?
My testenvironment only contains 7 loginscripts
Some files are similar but none is like the other.
If you want to change the fileserver or consolidate the structure you have to change all files at one time. In my case it is possible, but if you have 100 loginscripts you need lots of time and patience.
Or you can change all files with powershell easily.
Let’s see how I did the job:
start ISE on your computer with adminrights to sysvol /netlogon and change to the netlogon folder with
Set-Location \\demo01.it-koehler.com\NETLOGON get-childitem
you should now see all your loginscripts
Now we are ready to search in all loginscripts for some patterns (only view)
$searchstring = "fs01" $paths = (gci -File |foreach-object{if ((select-string -inputobject $_ -Pattern "$searchstring")){($_).FullName}}) $paths
I’m now able to see, that “fs01” is only available in test02.cmd
Next step is to remove this network map to “fs01” and give it “fs03”, so here we go:
$searchstring = "fs01" $paths = (gci -File |foreach-object{if ((select-string -inputobject $_ -Pattern "$searchstring")){($_).FullName}}) $paths foreach($path in $paths){ Set-Content $path -Value ((get-content $path | select-string -pattern "$searchstring" -notmatch)) gci -File $path | Add-Content -Value ("@net use v: /delete /y") gci -File $path | Add-Content -Value ("@net use v: \\fs03.demo01.it-koehler.com\data") }
the script will eliminate the complete line which contains “fs01 and will add the new server
see the result in test02.cmd
if you want to have a complete overview above all scripts (content of all files in one txt file) you can use this script
$profilebat = Get-ChildItem -File $profilebat foreach ($bat in $profilebat){ $bat.FullName | Out-File C:\temp\profile-bat-content.txt -Encoding utf8 -Append Get-Content -Path $bat.FullName | Out-File C:\temp\profile-bat-content.txt -Encoding utf8 -Append }
Quite simple and it does the job.
If you have questions leave them as comment and if you like the aricle please push the like button.
Have fun with your loginscripts.