Wednesday, September 03, 2008

Remove Offline Folder Share

Continuing from my previous blog about migrating file and print server, the clients might try to sync the offline folder to both old and new server. To fix this

disable offline folders
delete all the files in c:\windows\csc
enable offline folders

Monday, September 01, 2008

Re-Mapping Shared Folders or Printers

When you are tasked to migrate a file/print server, you have 2 options - keep the same server name for the new server or use a new name.

Keeping the same server name is an easier solution - you can build a new server with a temp name, do whatever you need to do and when the cutover time, rename the old server name to something else and name the new server to the old server name.

However, this approach is not always an option for you. If you need to use a different name for your new server, you need to somehow re-map all the shared folders and printers to the new file/print server.

Here is the script to map the printers:

' Full path to text file containing list of printer share names. e.g.
' OldPrinter1,NewPrinterA

Const PRINTERS_FILE = "\\server\printers$\new-printers.csv"

'Old and new Printer Server information
Const OLD_SERVER = "\\oldservername"
Const NEW_SERVER = "\\newservername"

'Create something to store the list of printers in
Set objPrinters = CreateObject("Scripting.Dictionary")

'Open the File in Read Only mode
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.OpenTextFile(PRINTERS_FILE, 1, False, 0)

Do While Not objFile.AtEndOfStream
arrLine = Split(objFile.ReadLine, ",")

' Load each printer entry into memory (keyed list, key is the old printer name)
If Not objPrinters.Exists(arrLine(0)) Then
objPrinters.Add LCase(arrLine(0)), LCase(arrLine(1))
End If

Loop

Set objFile = Nothing
Set objFileSystem = Nothing
Set objWSH = CreateObject("WScript.Network")

On Error Resume Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Printer WHERE Network=True",,48)

For Each objPrinter in colItems
If UCase(objPrinter.ServerName) = UCase(OLD_SERVER) Then
strPrinter = LCase(objPrinter.ShareName)
strOldPrinterPath = objPrinter.ServerName & "\" & strPrinter

'Remove any printer still connected to OLD_SERVER
objWSH.RemovePrinterConnection strOldPrinterPath, True, True

'See if the old Printer exists in the file (from memory)
If objPrinters.Exists(strPrinter) Then

' Create the new Printer Path
strNewPrinterPath = NEW_SERVER & "\" & objPrinters(strPrinter)
Err.Clear

' Add the new Printer Connection
objWSH.AddWindowsPrinterConnection strNewPrinterPath

If objPrinter.Default = "True" Then
objWSH.SetDefaultPrinter strNewPrinterPath
End If
End If
End If
Next

Set colItems = Nothing
Set objWMIService = Nothing
Set objWSH = Nothing
Set objPrinters = Nothing

Here is the script to map the shared folder:

' Full path to text file containing list of share names. e.g.
' OldShared,NewShared Const
SHARES_FILE = "\\server\printers$\new-shares.csv"

' Old and new Server information
Const OLD_SERVER = "oldserver"
Const NEW_SERVER = "newserver"

' Create something to store the list of shares in
Set objShares = CreateObject("Scripting.Dictionary")

' Open the File in Read Only mode
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.OpenTextFile(SHARES_FILE, 1, False, 0)

Do While Not objFile.AtEndOfStream
arrLine = Split(objFile.ReadLine, ",")

' Load each shares entry into memory (keyed list, key is the old printer name)
If Not objShares.Exists(arrLine(0)) Then
objShares.Add LCase(arrLine(0)), LCase(arrLine(1))
End If
Loop

Set objFile = Nothing
Set objFileSystem = Nothing

'Create a filesystem object
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")

'Create a drives collection
Set Drives = FileSystemObject.Drives

Dim objNetwork, strDriveLetter
Set objNetwork = CreateObject("WScript.Network")

'step through the drive collection, and extract'the drive letter and the type of drive
'according to the Microsoft VBScript documentation'there are 6 distinct types of drive

For Each DiskDrive in Drives
DriveLetter = DiskDrive.DriveLetter
DriveType = DiskDrive.DriveType

strDriveLetter = DriveLetter & ":"

Select Case DriveType
Case "0" DriveType = "Unknown type of drive"
Case "1" DriveType = "Removable drive"
Case "2" DriveType = "Fixed drive"
Case "3" DriveType = "Network drive"
Path = DiskDrive.ShareName
'get the server name of this shared folder
Temp = Right( Path, Len( Path ) - 2 )
IndexOfSlash = InStr( Temp, "\" )

OldServerName = Left( Temp, IndexOfSlash - 1 )

'get the shared name
OldSharedName = Right( Temp, Len( Temp ) - Len(OldServerName) - 1 )

'we only touch the shared folder in the old server
If UCase( OldServerName ) = UCase( OLD_SERVER ) Then
'is this the old shared name
If objShares.Exists( OldSharedName ) Then
'delete the old mapping drive
objNetwork.RemoveNetworkDrive strDriveLetter, True, True

strNetworkPath = "\\" & NEW_SERVER & "\" & objShares(OldSharedName )

'sleeping for a while
WScript.sleep 3000

'map the new drive persistently
objNetwork.MapNetworkDrive strDriveLetter, strNetworkPath, True
End If
End If
Case "4" DriveType = "CD-ROM drive"
Case "5" DriveType = "RAM Disk"
End Select
Next

Set Drives = nothing
Set FileSystemObject = nothing
Set objNetwork = nothing
Set objShares = nothing

Good Luck