Site icon Franky's Web

Windows Server: Remove orphaned shares (server service does not start)

Zur Abwechslung heute mal wieder ein kleines Problem. Der „Server“-Dienst auf einem Windows Server 2012 R2 Fileserver lies sich nicht mehr starten, was zur Folge hatte, das keine der Freigaben mehr erreichbar war. Im Eventlog „Anwendung“ war nur diese Warnung zu finden:

Source: Server

Event ID: 2511

Der Serverdienst konnte die Freigabe Freigabename nicht wiederherstellen, da das Verzeichnis D:\Home\Freigabe nicht mehr vorhanden ist. Führen Sie den Befehl „net share Freigabe /delete“ aus, um die Freigabe zu löschen oder um das Verzeichnis D:\Home\Freigabe zu erstellen.

There are obviously shares that point to a directory that no longer exists. In fact, the directories in question have been archived and the share has not been removed. Apparently the missing directories have irritated the server service somewhat, because after a longer waiting time, starting the service resulted in an error.

It's nice that the warning in the event log also gives the solution: net share share name /delete

However, the number of these warnings in the event log was somewhat unfavorable: 988, so it would have taken a little longer to manually copy all the commands from the event log to the command line and execute them.

Ich habe dazu ein schnelles Quick & Dirty Script erstellt, welches den „net share“ Befehl aus dem Warnungstext extrahiert und ein eine Batch-Datei kopiert:

$events = Get-EventLog -LogName System -Newest 1000 -EntryType Warning -Source Server | where {$_.eventid -match "2511"}
foreach ($event in $events)
{
 $sharepath = $event.ReplacementStrings[1]
 $testpath = test-path $sharepath
 if ($testpath -match "False")
  {
   write-warning "$sharepath existiert nicht"
   $message = $event.message
   $netcmd = $message.Split("`"")[1]
   $netcmd | add-content c:\netcmd.bat
  }
}

$csv = import-csv c:\netcmd.bat -header shares
($csv | group shares).name  | set-content c:\netcmd.bat

Das kleine Script holt sich die Warnungen aus dem Eventlog und extrahiert den „net share“-Befehl und kopiert ihn in die Datei „c:\netcmd.bat“:

The batch file can then simply be executed and removes the faulty shares. The server service could be restarted immediately after the cleanup. I later adjusted the script a little: Duplicate entries are removed and it is checked whether the directory has really been removed.

Maybe it will help one or the other.

Exit mobile version