Prologue
Ace Fekay here again!
Once again, As many of you know, I’m an avid Active Directory and Exchange server engineer/architect, and an MVP in Active Directory. And why am I posting simple stuff, you ask. Well, because we need to use this stuff day to day, that’s why.
Yea, this may be simple, but you’d be surprised who may struggle with it, like I did. I had to get help from a colleague who put the bulk of this together. I first had an idea with my beginner’s mentality to do it a little differently, but when I saw what he suggested, I said, hmm, I still have lots to learn.
I hope this, and my future scripts, especially with Office 365, help you out.
Scope
After migrating shares from one server to another server using a Robocopy script (that I’ll post later), we needed to change the drive mappings in the logon scripts in the Netlogon share.
Keep in mind, we already have a robust DFS in place. The new sharename has targets to the old server. However, we needed to change any logon scripts still referencing the old server by either NetBIOS or by FQDN (OldServer.domain.com).Well, with 28,000 scripts, that’s something we’re not going to do manually.
This script replaces any mappings using the old server name, “OldServer” such as either \\olderserver\sharename or \\oldserver.contoso.com\sharename, to the new DFS name, \\contoso.com\NewShareName.
Code
This works fine. Watch the word-wrap in the blog.
# First run the robocopy script to copy all data
# Then run the netlogon report script to see how many bat files in netlogon reference OldServer
# Then run this script to replace any reference to “OldServer” to the new DFS sharename in the batch files for each share.
# By Ace Fekay and a colleague, who put together the bulk of this together.
# I added counters and report to the screen.
# If you need to run it as a different users, un-remark the following
# get-credential
$Path = “\\contoso.com\NETLOGON\”
$FilesAltered = 0
$FilesProcessed = 0
# This code snippet gets all the files in $Path that end in “.bat”.
cd $Path
Get-ChildItem -Filter “*.bat” | foreach{
$file = Get-Content $_
#only modify files that contain the string “OldServer”
if (Select-String -InputObject $file “OldServer”){
$file = Get-Content $_
$file = $file -replace “\\\\OldServer\\Users”,”\\contoso.com\\OldServer-Users”
$file = $file -replace “\\\\OldServer.contoso.com\\users”,”\\contoso.com\OldServer-User”
$file = $file -replace “\\\\OldServer\\Department”,”\\contoso.com\\OldServer-Department”
$file = $file -replace “\\\\OldServer.contoso.com\\Department”,”\\contoso.com\OldServer-Departmentt”
$file = $file -replace “\\\\OldServer\\GDrive”,”\\contoso.com\OldServer-GDrive”
$file = $file -replace “\\\\OldServer\\FDrive”,”\\contoso.com\OldServer-FDrive”
$file = $file -replace “\\\\OldServer\\HDrive”,”\\contoso.com\OldServer-HDrive”
$file = $file -replace “\\\\OldServer\\Share2\$”,”\\contoso.com\OldServer-Share2$”
#comment out any net time statements, if they exist
$file = $file -replace “^net time”,”REM net time”
#write out the changes
Set-Content -Value $file -Path $_;
Write-Host $_.Name
write-host $file
Write-Host “”
$FilesAltered++
}
$FilesProcessed++
}
Write-Host $FilesAltered ” altered out of a total of” $FilesProcessed “files processed.”
Comments are welcomed.
==================================================================
Summary
I hope this helps!
Published 9/9/2015
Ace Fekay
MVP, MCT, MCSE 2012, MCITP EA & MCTS Windows 2008/R2, Exchange 2013, 2010 EA & 2007, MCSE & MCSA 2003/2000, MCSA Messaging 2003
Microsoft Certified Trainer
Microsoft MVP – Directory Services
Complete List of Technical Blogs: http://www.delawarecountycomputerconsulting.com/technicalblogs.php
This posting is provided AS-IS with no warranties or guarantees and confers no rights.