I need to verify that my app can connect to the default C$ share on a remote server on the network. Does anyone have a really bulletprrof way I can do this? Initially I was doing something like this:
Dim ProcessStartInfo As New System.Diagnostics.ProcessStartInfo
ProcessStartInfo.FileName = "net"
ProcessStartInfo.Arguments = "use \\" & strComputer & "\c$ /USER:" & strUsername & " " & strPassword
ProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden
System.Diagnostics.Process.Start(ProcessStartInfo)
But this can be flakey sometimes and hang, take too long to return etc. Whatever method I use needs to be able to supply credentials and if at all possible, when the connection fails I need to know why it failed.
I solved this another way but can't find the thread.
Dim dir As New IO.DirectoryInfo("\\10.101.1.200\c$")
dir.exists will then tell you if it was able to connect.
This will try to connect as the account the program was run from. If you need different credentials you may need to look in to Impersonation.
You could directly call the windows functions via pInvoke, but that gets more complex. An example can be found here. C#: How to logon to a share when using DirectoryInfo
Related
I have this code that find who has a file opened. The files are on network drives that use our old username as authenication. Our username use to be john.doe, it is now a number 12345. The code does not find a username. Is there something I'm overlooking or need to do to find out who has the file opened? Curious if and when we are on the actual new network that host the username 12345, that we will be able to find see the username 12345. The error message i do get is:
"The trust relationship between this workstation and the primary domain failed"
Public Shared Function GetFileOwner(ByVal strFileName)
Try
Dim objSD As Object = Nothing
Dim objWMIService = GetObject("winmgmts:")
Dim objFileSecuritySettings =
objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFileName & "'")
Dim intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)
If intRetVal = 0 Then
GetFileOwner = objSD.Owner.Name
Else
GetFileOwner = "Unknown"
End If
Catch ex As Exception
MsgBox("Error :" & Date.Today.ToString & " " & ex.Message)
GetFileOwner = "Unknown"
End Try
End Function
Do you know that this option is available on your server? You can easily do this..
Open the Computer Management snap-in on your file server (or connect to the server remotely from the management console running on your computer) and go to System Tools -> Shared Folders -> Open files. The list of files opened on the remote server is displayed on the right side of the window.
Many times i tried to access my server on the domain and it gave me that error, domains hate custom made apps :)
I'm trying to fetch the files inside a folder ordered by LastWriteTime.
The code is running very fast when accessing to a local path (C:\MyFolder), but is hanging when accessing to a remote path (\\MyServer\MyFolder)
Dim myOrderedList As List(Of String) = (From item In IO.Directory.GetFiles(strFolderSource) _
Let file = New IO.FileInfo(item) _
Order By file.LastWriteTime _
Select item).ToList()
Should this code work? Is not allowed this method to get files from a remote folder?
Which alternative code could I use to get the same result without hanging?
EDITED (2019-01-18 16:32):
Sorry guys, I've tried the proposed solution from Rango, and still the same hang. Finally I created a small logging system to catch the step that caused the problem, and realized that all is a credential problem.
Just before the code I posted I do a NET USE to grant access to the remote computer, and the net use is executed, but for any reason, the GetFiles() fails because of Logon failure: unknown user name or bad password.
So, Could I ensure the credentials with the net use before call the GetFiles()?
Maybe using a pause or something like this?
FULL CODE:
Dim processInfo As New System.Diagnostics.ProcessStartInfo()
processInfo.FileName = "C:\WINDOWS\system32\net"
processInfo.Arguments = "net use \\MyServer\IPC$ ""password"" /USER:Username"
System.Diagnostics.Process.Start(processInfo)
Dim myOrderedList As List(Of String) = (From item In IO.Directory.GetFiles("\\MyServer\g$\MyFolder") _
Let file = New IO.FileInfo(item) _
Order By file.LastWriteTime _
Select item).ToList()
You could try to use DirectoryInfo.EnumerateFiles instead wich has two advantages:
No consecutive security handshakes from the remote server necessary
Streaming the files instead of loading all into memory before you start ordering them
Dim di = new DirectoryInfo(strFolderSource)
Dim files = From fi In di.EnumerateFiles() Order By fi.LastWriteTime Select fi.FullName
Dim myOrderedList As List(Of String) = files.ToList()
Finally solved including a sleep of 5 seconds after the net use and before the GetFiles():
System.Threading.Thread.Sleep(5000)
Thanks for your time, and hope this helps anybody with a similar problem.
I am making a program that schedules the redial of a VPN connection using rasdial however I cannot get my script to pass the correct information to the rasdial.exe program.
For example
Process.Start("rasdial.exe", "VPN Connection " & ""USERNAME!" & "PASSWORD")
However when this starts it just says "Remote access error 625"
It turns out that the best solution is to concatenate the variables before storing them in a database or document because the connection only inputs one parameter.
Is it possible to include in a VB.net 2008 Project a VBScript (test.vbs) and run it if its while the processing necessary? But the main thing is it should be possible to BUILD just one .exe.
If so, can you also receive values / arguments from the VBS file?
Here is an example, although it's pointless, but it is used for unterstanding:
VB.net -> exe is running
the exe runs please_find_the_coputername.vbs
The script please_find_the_coputername.vbs -> obtained the computer name and sends this variable to VB.net
VB.Net displays the computer name via Msgbox().
Note: I know that I can read out the computer name with VB.net but this example is only for understanding my questions.
Edit:
HI #maxedev thank you for your answer.
Wow.. its nice trick.
But I want only to do this VBScript code in VB.net:
Dim strComputer
strComputer = "LP-BKR"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
Wscript.Echo "Logged-on Domain: " & objComputer.Domain
Wscript.Echo "Logged-on UserName: " & objComputer.UserName
Wscript.Echo "Logged-on ComputerName: " & objComputer.Name
Next
set objWMIService = Nothing
set colComputer = Nothing
I searched the whole day to get the same Value... but didn't find anything. That's why I decide to do that in this way. But if I think, the trick with clipboard is risky. It pushes the still clipboard text away. How can I realize it?
I'm not sure exactly what you're trying to accomplish, but you could write to a text file and then read it through vb.net - or you could do something like this post to use the clipboard to pass info ie :
VBS:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo hello world | clip", 0, TRUE
VB.NET:
MessageBox.Show(Clipboard.GetText)
--shows "hello world"
One solution would be to add a reference to the MS Script Control:
http://msdn.microsoft.com/en-us/library/aa227400(v=vs.60).aspx
Using that, you can add literally add code (VBScript) with the AddCode() method then run it and get the output back. I have a tiny example here.
Windows automatically provides the information you're looking for in environment variables:
%USERNAME% -> username of the logged in user
%USERDOMAIN% -> WINS name of the domain the user is logged into
%USERDNSDOMAIN% -> FQDN of the domain the user is logged into
%COMPUTERNAME% -> hostname of the computer
I'm trying to something pretty simple but am having headaches with it. I'm pretty sure all I have here is a permissions issue I don't know how to solve but I could be wrong.
Here is my scenario...
I have 3 servers...
Server A - Web Server (Windows 2003) - Anonymous Access Enabled
Server B - Red 5 Media Server
Server C - NAS
The website on Server A allows for recording/uploading of video to Server B where the video is processed/transcoded.
A Cron job on Server B then moves the videos over to Server C for storage.
Users can then watch their videos from Server A via a virtual directory thats been set up in IIS that points to Server C and connects as a domain user account.
I need to some how use the ASP File System Object to get the size of a folder on Server C that contains the videos.
I've mapped the parent folder of the folder on Server C where the videos are stored to a drive letter on Server A using a UNC path (\servername\videos).
I've tried using the FileSystemObject's folderExists() method for debugging purposes and it returns false if I pass in the mapped letter drive or a UNC path. It gives a path not found error. Just to be sure I did a response.write of the path being passed into the folderExists() method and it is the correct path. All of this leads me to believe this is really a permissions issue which I just don't know how to solve.
Thanks,
Ryan
Mapped network drives are no use to you in ASP on IIS6 since they are part of a user profile. User profiles are not fully loaded in services like IIS6.
Assuming a pure ASP environment (you can't install other dlls etc) then you will need the anonymous user to have access to the network share where the videos are stored and use the UNC path.
Now typically the IUSR account is local guest account which has no access to the network. You will need to create a user on the domain that the NAS belongs to and specify that users credentials as the anonymous user account for those pages that need to do your "Get Size" task ( you don't need change anonymous user for the whole application).
The foregoing assumes Server A is a member of the same domain as Server C. If not then you need to create user on the domain the Server A belongs to as well that mirrors the one on Server C including its password (or jump through trust relationship hoops).
With that in place you can go ahead and use FileSystemObject.
You need to work with the FSO.Drives collection to get to your NAS. Have a look at this article (just googled it but it seems ok):
http://www.stardeveloper.com/articles/display.html?article=2001050301&page=1
Try running this and seeing if you drives are available, should help narrow down if its a permissions problem
<%
Dim fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Dim drives
Set drives = fso.Drives
Dim isReady
For Each drive in drives
isReady = drive.IsReady
If isReady Then
Response.Write "DriveLetter: " & drive.DriveLetter & "<br>"
Response.Write "Path: " & drive.Path & "<br>"
Response.Write "FileSytem: " & drive.FileSystem & "<br>"
Response.Write "TotalSize: " & drive.TotalSize & "<br>"
Response.Write "FreeSpace: " & drive.FreeSpace & "<br>"
Else
Response.Write "Driv Letter: " & drive.DriveLetter & "<br>"
Response.Write drive.Path & "<br>"
Response.Write "No other information available."
End If
Next
%>
Have a play with that and see if you are still having problems, if so we will dig a bit deeper :)
Not sure if this is the right thing to do but to remedy this I had to map the network drive via ASP (as the IUSR_machine) account like so...
dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "X:", "UNC path", "profile", "user", "password"
objNetwork.RemoveNetworkDrive "X:"
set objNetwork = nothing
Then I was able to access the mapped drive letter (X in this case) via the FileSystemObject.