none
Run function as other user RRS feed

  • Question

  • I have to copy files from my local network, so I use :


    My.Computer.FileSystem.CopyFile("C:\Users\Public\Documents\tes.vbs",    "\\Myserver\c$\Users\Public\Documents\tes.vbs")

    But I need to autenticate with pass and user

    So I want to run this function as my super user like:

     Private Sub trust()
            AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
            Dim idnt As WindowsIdentity = New WindowsIdentity("Mynetworkuser", "Mynetworkpass")
            Dim context As WindowsImpersonationContext = idnt.Impersonate()
    
            My.Computer.FileSystem.CopyFile("C:\Users\Public\Documents\tes.vbs", "\\Myserver\c$\Users\Public\Documents\tes.vbs")
            context.Undo()
        End Sub

    but I alwes get the erro : System.IO.FileNotFoundException

    Monday, March 5, 2018 12:53 PM

Answers

  • Hey, thx for help, this works for me :

        Sub CopyFileToREmoteComputer(RemoteMachine As String, File As String, user As String, PassWd As String)
            Dim p As String = PassWd
            Dim pass As SecureString = New SecureString()
            'Create pass
            For i As Integer = 0 To p.Length - 1
                pass.AppendChar(p(i))
            Next
            'Copy file
            Process.Start("cmd", "/c copy " + File + " \\" + RemoteMachine + "\c$\Users\Public\Documents", user, pass, "Domain")
            'start process
        End Sub


    • Marked as answer by Ran-jj Tuesday, March 6, 2018 1:40 PM
    • Edited by Ran-jj Tuesday, March 6, 2018 1:41 PM
    Tuesday, March 6, 2018 1:40 PM

All replies

  • Try using File.Exists with the tes.vbs file after enabling impersonation to see if the user has access to the file. If the user is a network user then public folder access must be enabled on the machine and permissions should be enabled.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, March 5, 2018 1:11 PM
  • And you are the superuser (administrator) of the network domain. Otherwise it is something like many want to do on the computer of Bill Gates. 

    Success
    Cor

    Monday, March 5, 2018 1:46 PM
  • The file exist :/
    Monday, March 5, 2018 2:16 PM
  • The file exist :/
    OK, can you copy the file to a local folder on the computer? If so, that would indicate an issue copying to the network resource.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, March 5, 2018 2:23 PM
  • The file exist :/

    Maybe the system the app is running on or the user impersonated isn't allowed to access the file for some reason. Or perhaps the app itself has to be running with admin privileges for that to work.

    La vida loca

    Tuesday, March 6, 2018 4:16 AM
  • The file exist :/

    Hi Ran_jj,

    Check folder permissions on the network drive. If it's not seeing the directory, it's either a permissions issue, or there is a typo in the path.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 6, 2018 8:28 AM
    Moderator
  • Hey, thx for help, this works for me :

        Sub CopyFileToREmoteComputer(RemoteMachine As String, File As String, user As String, PassWd As String)
            Dim p As String = PassWd
            Dim pass As SecureString = New SecureString()
            'Create pass
            For i As Integer = 0 To p.Length - 1
                pass.AppendChar(p(i))
            Next
            'Copy file
            Process.Start("cmd", "/c copy " + File + " \\" + RemoteMachine + "\c$\Users\Public\Documents", user, pass, "Domain")
            'start process
        End Sub


    • Marked as answer by Ran-jj Tuesday, March 6, 2018 1:40 PM
    • Edited by Ran-jj Tuesday, March 6, 2018 1:41 PM
    Tuesday, March 6, 2018 1:40 PM
  • That makes sense. I remember now that you can't change (impersonate) the current security context for a Windows app. You can only set it for a new process.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, March 6, 2018 2:33 PM