Public Sub CreateShare(ByVal strShareName As String, ByVal strPath As String, ByVal computername As String, ByVal shareinfo As String)
Try
Dim objSWbemServices As Object
Dim objSWbemObject As Object
Dim colSWbemObject As Object
Dim intRet As Integer
Dim blnExists As Boolean
Dim objSWbem As Object
objSWbemServices = GetObject("winmgmts://" + computername + "/root/cimv2")
colSWbemObject = objSWbemServices.InstancesOf("Win32_Share")
For Each objSWbem In colSWbemObject
If (objSWbem.name = strShareName) Then
blnExists = True
Exit For
Else
blnExists = False
End If
Next
If (blnExists = False) Then
objSWbemObject = objSWbemServices.Get("Win32_Share")
intRet = objSWbemObject.Create(strPath, strShareName, 0, 25, shareinfo)
System.Console.WriteLine("Share has been created on : " + computername)
System.Console.WriteLine("ShareName : " + ShareName + " SharePath : " + SharePath + " ShareInfo : " + shareinfo)
Else
System.Console.WriteLine("Share is already present on computer : " + computername)
System.Console.WriteLine("ShareName : " + ShareName + " SharePath : " + SharePath + " ShareInfo : " + shareinfo)
End If
Catch ex As Exception
System.Console.Write("Error occured while trying to create shares on remote pc : " + computername + vbCrLf + "Check if you have the necessary rights and/or that the pc is turned on." + vbCrLf + ex.ToString)
System.Console.WriteLine("ShareName : " + ShareName + " SharePath : " + SharePath + " ShareInfo : " + shareinfo)
End Try
End Sub
Public Function ShowShares(ByVal ComputerName) As Boolean
Try
Dim objSWbemServices As Object
Dim colSWbemObject As Object
objSWbemServices = GetObject("winmgmts://" + ComputerName + "/root/cimv2")
colSWbemObject = objSWbemServices.InstancesOf("Win32_Share")
' Loop through each share on the machine to see if it already exists
System.Console.WriteLine("Remote Shares on : " + ComputerName)
For Each objSWbem In colSWbemObject
System.Console.WriteLine("ShareName : " + objSWbem.name + " SharePath : " + objSWbem.path + " ShareInfo : " + objSWbem.description)
Next
Catch ex As Exception
System.Console.Write("Error occured while trying to show shares on remote pc : " + ComputerName + vbCrLf + "Check if you have the necessary rights and/or that the pc is turned on." + vbCrLf + ex.ToString)
End Try
End Function
Public Function RemoveShare(ByVal shareName As String, ByVal computername As String) As Boolean
Try
Dim objSWbemServices As Object
Dim objSWbemObject As Object
Dim colSWbemObject As Object
objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & computername & "\root\cimv2")
colSWbemObject = objSWbemServices.ExecQuery("SELECT * FROM Win32_Share WHERE Name = '" + shareName + "'")
For Each objSWbemObject In colSWbemObject
objSWbemObject.Delete()
System.Console.WriteLine("Share has been removed on : " + computername)
System.Console.WriteLine("ShareName : " + shareName)
Next
Catch ex As Exception
System.Console.Write("Error occured while trying to remove shares on remote pc." + vbCrLf + "Check if you have the necessary rights and/or that the pc is turned on." + vbCrLf + ex.ToString)
System.Console.WriteLine("Computername : " + computername + " ShareName : " + shareName)
End Try
End Function
Public Class frmRemoteSharing
Public ComputerName As String
Public ShareName As String
Public SharePath As String
Public ShareInfo As String
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Not ComputerName = "" Then
ShowShares(ComputerName)
End If
End Sub
Public Sub CreateShare(ByVal strShareName As String, ByVal strPath As String, ByVal computername As String, ByVal shareinfo As String)
Try
Dim objSWbemServices As Object
Dim objSWbemObject As Object
Dim colSWbemObject As Object
Dim intRet As Integer
Dim blnExists As Boolean
Dim objSWbem As Object
objSWbemServices = GetObject("winmgmts://" + computername + "/root/cimv2")
colSWbemObject = objSWbemServices.InstancesOf("Win32_Share")
For Each objSWbem In colSWbemObject
If (objSWbem.name = strShareName) Then
blnExists = True
Exit For
Else
blnExists = False
End If
Next
If (blnExists = False) Then
objSWbemObject = objSWbemServices.Get("Win32_Share")
intRet = objSWbemObject.Create(strPath, strShareName, 0, 25, shareinfo)
Else
MsgBox("Folder aready shared")
End If
Catch ex As Exception
MsgBox("Error occured while trying to create shares on remote pc." + vbCrLf + "Check if you have the necessary rights and/or that the pc is turned on." + vbCrLf + ex.ToString)
End Try
End Sub
'
Public Function ShowShares(ByVal ComputerName) As Boolean
Try
ListView1.Items.Clear()
Dim objSWbemServices As Object
Dim colSWbemObject As Object
objSWbemServices = GetObject("winmgmts://" + ComputerName + "/root/cimv2")
colSWbemObject = objSWbemServices.InstancesOf("Win32_Share")
' Loop through each share on the machine to see if it already exists
For Each objSWbem In colSWbemObject
Dim lvitem As New ListViewItem
With lvitem
.Text = objSWbem.name
.SubItems.Add(objSWbem.path)
.SubItems.Add(objSWbem.description)
End With
ListView1.Items.Add(lvitem)
Next
Catch ex As Exception
MsgBox("Error occured while trying to show shares on remote pc." + vbCrLf + "Check if you have the necessary rights and/or that the pc is turned on." + vbCrLf + ex.ToString)
End Try
End Function
Public Function RemoveShare(ByVal shareName As String, ByVal computername As String) As Boolean
Try
Dim objSWbemServices As Object
Dim objSWbemObject As Object
Dim colSWbemObject As Object
objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & computername & "\root\cimv2")
colSWbemObject = objSWbemServices.ExecQuery("SELECT * FROM Win32_Share WHERE Name = '" + shareName + "'")
For Each objSWbemObject In colSWbemObject
objSWbemObject.Delete()
Next
Catch ex As Exception
MsgBox("Error occured while trying to remove shares on remote pc." + vbCrLf + "Check if you have the necessary rights and/or that the pc is turned on." + vbCrLf + ex.ToString)
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not ComputerName = "" And Not ShareName = "" And Not SharePath = "" Then
CreateShare(ShareName, SharePath, ComputerName, ShareInfo)
End If
ShowShares(ComputerName)
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ComputerName = TextBox1.Text
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
ShareName = TextBox2.Text
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
SharePath = TextBox3.Text
End Sub
Private Sub TextBox4_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
ShareInfo = TextBox4.Text
End Sub
Private Sub frmRemoteSharing_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ComputerName = TextBox1.Text
ShareName = TextBox2.Text
SharePath = TextBox3.Text
ShareInfo = TextBox4.Text
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Not ListView1.SelectedItems(0).Text = "" Then
RemoveShare(ListView1.SelectedItems(0).Text, ComputerName)
End If
ShowShares(ComputerName)
End Sub
Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
Shell("explorer.exe \\" + ComputerName + "\" + ListView1.SelectedItems(0).Text, AppWinStyle.NormalFocus)
End Sub
End Class
Module modMain
Public ComputerName As String
Public ShareName As String
Public SharePath As String
Public ShareInfo As String
Sub main()
Dim i As Integer = 0
For Each Argument As String In My.Application.CommandLineArgs
If i = 0 Then
Select Case LCase(Argument)
Case "-?"
System.Console.WriteLine("Remote Share : Version : " + My.Application.Info.Version.ToString)
System.Console.WriteLine("################################")
System.Console.WriteLine(vbCrLf)
System.Console.WriteLine("Remote Share can be used to show, create and remove shares on a remote or local computer.")
System.Console.WriteLine(vbCrLf)
System.Console.WriteLine("Usage : RemoteShare -?")
System.Console.WriteLine("Displays this help information")
System.Console.WriteLine(vbCrLf)
System.Console.WriteLine("Usage : RemoteShare -show COMPUTERNAME")
System.Console.WriteLine("Displays all shares on specific computer")
System.Console.WriteLine(vbCrLf)
System.Console.WriteLine("Usage : RemoteShare -create COMPUTERNAME ShareName SharePath ShareInfo")
System.Console.WriteLine("Creates a share on remote or local computer")
System.Console.WriteLine(vbCrLf)
System.Console.WriteLine("Usage : RemoteShare -remove COMPUTERNAME ShareName")
System.Console.WriteLine("Removes a share on remote or local computer")
System.Console.WriteLine(vbCrLf)
System.Console.WriteLine("Example : RemoteShare -create COMPUTER1 C$ C:\ " + Chr(34) + "Temporary IT Share" + Chr(34))
Case "-show"
Dim j As Integer = 0
For Each arg As String In My.Application.CommandLineArgs
If j = 1 Then
ComputerName = arg
ShowShares(ComputerName)
Exit Sub
End If
j = j + 1
Next
Case "-create"
Dim j As Integer = 0
For Each arg As String In My.Application.CommandLineArgs
If j = 1 Then
ComputerName = arg
ElseIf j = 2 Then
ShareName = arg
ElseIf j = 3 Then
SharePath = arg
ElseIf j = 4 Then
ShareInfo = arg
End If
j = j + 1
Next
CreateShare(ShareName, SharePath, ComputerName, ShareInfo)
Exit Sub
Case "-remove"
Dim j As Integer = 0
For Each arg As String In My.Application.CommandLineArgs
If j = 1 Then
ComputerName = arg
ElseIf j = 2 Then
ShareName = arg
End If
j = j + 1
Next
RemoveShare(ShareName, ComputerName)
Case Else
System.Console.WriteLine("Unknown argument supplied")
Exit Sub
End Select
End If
i = i + 1
Exit Sub
Next
End Sub
Public Sub CreateShare(ByVal strShareName As String, ByVal strPath As String, ByVal computername As String, ByVal shareinfo As String)
Try
Dim objSWbemServices As Object
Dim objSWbemObject As Object
Dim colSWbemObject As Object
Dim intRet As Integer
Dim blnExists As Boolean
Dim objSWbem As Object
objSWbemServices = GetObject("winmgmts://" + computername + "/root/cimv2")
colSWbemObject = objSWbemServices.InstancesOf("Win32_Share")
For Each objSWbem In colSWbemObject
If (objSWbem.name = strShareName) Then
blnExists = True
Exit For
Else
blnExists = False
End If
Next
If (blnExists = False) Then
objSWbemObject = objSWbemServices.Get("Win32_Share")
intRet = objSWbemObject.Create(strPath, strShareName, 0, 25, shareinfo)
System.Console.WriteLine("Share has been created on : " + computername)
System.Console.WriteLine("ShareName : " + ShareName + " SharePath : " + SharePath + " ShareInfo : " + shareinfo)
Else
System.Console.WriteLine("Share is already present on computer : " + computername)
System.Console.WriteLine("ShareName : " + ShareName + " SharePath : " + SharePath + " ShareInfo : " + shareinfo)
End If
Catch ex As Exception
System.Console.Write("Error occured while trying to create shares on remote pc : " + computername + vbCrLf + "Check if you have the necessary rights and/or that the pc is turned on." + vbCrLf + ex.ToString)
System.Console.WriteLine("ShareName : " + ShareName + " SharePath : " + SharePath + " ShareInfo : " + shareinfo)
End Try
End Sub
'
Public Function ShowShares(ByVal ComputerName) As Boolean
Try
Dim objSWbemServices As Object
Dim colSWbemObject As Object
objSWbemServices = GetObject("winmgmts://" + ComputerName + "/root/cimv2")
colSWbemObject = objSWbemServices.InstancesOf("Win32_Share")
' Loop through each share on the machine to see if it already exists
System.Console.WriteLine("Remote Shares on : " + ComputerName)
For Each objSWbem In colSWbemObject
System.Console.WriteLine("ShareName : " + objSWbem.name + " SharePath : " + objSWbem.path + " ShareInfo : " + objSWbem.description)
Next
Catch ex As Exception
System.Console.Write("Error occured while trying to show shares on remote pc : " + ComputerName + vbCrLf + "Check if you have the necessary rights and/or that the pc is turned on." + vbCrLf + ex.ToString)
End Try
End Function
Public Function RemoveShare(ByVal shareName As String, ByVal computername As String) As Boolean
Try
Dim objSWbemServices As Object
Dim objSWbemObject As Object
Dim colSWbemObject As Object
objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & computername & "\root\cimv2")
colSWbemObject = objSWbemServices.ExecQuery("SELECT * FROM Win32_Share WHERE Name = '" + shareName + "'")
For Each objSWbemObject In colSWbemObject
objSWbemObject.Delete()
System.Console.WriteLine("Share has been removed on : " + computername)
System.Console.WriteLine("ShareName : " + shareName)
Next
Catch ex As Exception
System.Console.Write("Error occured while trying to remove shares on remote pc." + vbCrLf + "Check if you have the necessary rights and/or that the pc is turned on." + vbCrLf + ex.ToString)
System.Console.WriteLine("Computername : " + computername + " ShareName : " + shareName)
End Try
End Function
End Module
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
Author
Commented:But that might be a good follow up article...