Die 1:1 VB-Konvertierung von Veasel's Snippet:
Freigaben eines RemoteRechners auslesen/ löschen
http://dotnet-snippets.de/dns/freigaben-eines-remoterechners-auslesen-loeschen-SID642.aspx
ein kleines Programm um Netzwerkfreigaben remote auszulesen und ggf. zu löschen.
Parameter:
PcName: der Name des PCs
Password: das lokale Adminpasswort
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Management
Namespace FreigabenRemoteEntfernen.Net
Class Program
Private Shared Sub Main(ByVal args As String())
If args.Length <> 2 Then
Console.WriteLine("so aufrufen: ""FreigabenRemoteEnfernen.Net.exe"" ""PCName"" ""Password""")
Else
Dim lister As New ShareLister()
Dim shares As List(Of ManagementObject) = lister.ListShares(args(0), args(0) & "\Administrator", args(1))
Console.WriteLine("Alle Freiagben:")
Console.WriteLine()
'Leerzeile
For Each share As ManagementObject In shares
Console.WriteLine(share("name").ToString())
Next
Console.WriteLine()
'Leerzeile
Console.WriteLine("Welche Freigabe soll gelöscht werden? ")
Dim shareName As String = Console.ReadLine()
Dim result As Boolean = False
'Freigabe löschen
For Each share As ManagementObject In shares
If share("name").Equals(shareName) Then
result = lister.RemoveShare(share)
End If
Next
If result Then
Console.WriteLine("Gelöscht")
Else
Console.WriteLine("nicht gelöscht")
End If
End If
Console.ReadKey()
End Sub
End Class
Class ShareLister
Public Function ListShares(ByVal computername As String, ByVal username As String, ByVal password As String) As List(Of ManagementObject)
Dim Shares As New List(Of ManagementObject)()
If computername <> String.Empty Then
Dim managementScope1 As New ManagementScope(("\\" & computername & "\root\cimv2"))
managementScope1.Options.Username = username
managementScope1.Options.Password = password
managementScope1.Options.EnablePrivileges = True
Dim objectQuery1 As New ObjectQuery("SELECT * FROM Win32_Share")
Dim managementObjectSearcher1 As New ManagementObjectSearcher(managementScope1, objectQuery1)
Using managementObjectEnumerator1 As ManagementObjectCollection.ManagementObjectEnumerator = managementObjectSearcher1.[Get]().GetEnumerator()
While managementObjectEnumerator1.MoveNext()
Dim managementObject1 As ManagementObject = DirectCast(managementObjectEnumerator1.Current, ManagementObject)
Shares.Add(managementObject1)
End While
End Using
End If
Return Shares
End Function
Public Function RemoveShare(ByVal shareToRemove As ManagementObject) As Boolean
Try
shareToRemove.InvokeMethod("Delete", New Object() {shareToRemove.Path.ToString()})
Return True
Catch
Return False
End Try
End Function
End Class
End Namespace
5 Kommentare zum Snippet